Merge pull request #3034 from rommapp/fmt-alembic-files

Run formatter on alembic files
This commit is contained in:
Georges-Antoine Assi
2026-02-17 15:46:39 -05:00
committed by GitHub
26 changed files with 110 additions and 257 deletions

View File

@@ -203,14 +203,12 @@ def upgrade() -> None:
"update roms set file_name_no_ext = regexp_replace(file_name, '\\.[a-z]{2,}$', '')" "update roms set file_name_no_ext = regexp_replace(file_name, '\\.[a-z]{2,}$', '')"
) )
if is_postgresql(connection): if is_postgresql(connection):
batch_op.execute( batch_op.execute("""
"""
UPDATE roms UPDATE roms
SET platform_id = platforms.id SET platform_id = platforms.id
FROM platforms FROM platforms
WHERE roms.platform_slug = platforms.slug WHERE roms.platform_slug = platforms.slug
""" """)
)
else: else:
batch_op.execute( batch_op.execute(
"update roms inner join platforms on roms.platform_slug = platforms.slug set roms.platform_id = platforms.id" "update roms inner join platforms on roms.platform_slug = platforms.slug set roms.platform_id = platforms.id"
@@ -270,14 +268,12 @@ def downgrade() -> None:
with op.batch_alter_table("roms", schema=None) as batch_op: with op.batch_alter_table("roms", schema=None) as batch_op:
if is_postgresql(connection): if is_postgresql(connection):
batch_op.execute( batch_op.execute("""
"""
UPDATE roms UPDATE roms
SET platform_slug = platforms.slug SET platform_slug = platforms.slug
FROM platforms FROM platforms
WHERE roms.platform_id = platforms.id WHERE roms.platform_id = platforms.id
""" """)
)
else: else:
batch_op.execute( batch_op.execute(
"update roms inner join platforms on roms.platform_id = platforms.id set roms.platform_slug = platforms.slug" "update roms inner join platforms on roms.platform_id = platforms.id set roms.platform_slug = platforms.slug"

View File

@@ -84,15 +84,13 @@ def upgrade() -> None:
) )
connection.execute( connection.execute(
sa.text( sa.text("""
"""
UPDATE roms UPDATE roms
SET path_cover_s = :path_cover_s, SET path_cover_s = :path_cover_s,
path_cover_l = :path_cover_l, path_cover_l = :path_cover_l,
path_screenshots = :path_screenshots path_screenshots = :path_screenshots
WHERE id = :id WHERE id = :id
""" """),
),
{ {
"id": rom.id, "id": rom.id,
"path_cover_s": updated_path_cover_s, "path_cover_s": updated_path_cover_s,

View File

@@ -44,13 +44,11 @@ def upgrade() -> None:
sa.UniqueConstraint("rom_id", "user_id", name="unique_rom_user_props"), sa.UniqueConstraint("rom_id", "user_id", name="unique_rom_user_props"),
) )
op.execute( op.execute("""
"""
INSERT INTO rom_user (id, updated_at, note_raw_markdown, note_is_public, is_main_sibling, rom_id, user_id) INSERT INTO rom_user (id, updated_at, note_raw_markdown, note_is_public, is_main_sibling, rom_id, user_id)
SELECT id, updated_at, raw_markdown, is_public, FALSE, rom_id, user_id SELECT id, updated_at, raw_markdown, is_public, FALSE, rom_id, user_id
FROM rom_notes FROM rom_notes
""" """)
)
op.drop_table("rom_notes") op.drop_table("rom_notes")
# ### end Alembic commands ### # ### end Alembic commands ###
@@ -84,13 +82,11 @@ def downgrade() -> None:
) )
# Copy the data back from the new table to the old table # Copy the data back from the new table to the old table
op.execute( op.execute("""
"""
INSERT INTO rom_notes (id, updated_at, raw_markdown, is_public, rom_id, user_id) INSERT INTO rom_notes (id, updated_at, raw_markdown, is_public, rom_id, user_id)
SELECT id, updated_at, note_raw_markdown, note_is_public, rom_id, user_id SELECT id, updated_at, note_raw_markdown, note_is_public, rom_id, user_id
FROM rom_user FROM rom_user
""" """)
)
# Drop the new table # Drop the new table
op.drop_table("rom_user") op.drop_table("rom_user")

View File

@@ -29,8 +29,7 @@ def upgrade() -> None:
) )
connection.execute( connection.execute(
sa.text( sa.text(f"""
f"""
CREATE VIEW sibling_roms AS CREATE VIEW sibling_roms AS
SELECT SELECT
r1.id AS rom_id, r1.id AS rom_id,
@@ -52,8 +51,7 @@ def upgrade() -> None:
OR OR
(r1.moby_id = r2.moby_id AND r1.moby_id IS NOT NULL) (r1.moby_id = r2.moby_id AND r1.moby_id IS NOT NULL)
); );
""" # nosec B608 """), # nosec B608
),
) )
@@ -61,11 +59,9 @@ def downgrade() -> None:
connection = op.get_bind() connection = op.get_bind()
connection.execute( connection.execute(
sa.text( sa.text("""
"""
DROP VIEW sibling_roms; DROP VIEW sibling_roms;
""" """),
),
) )
with op.batch_alter_table("roms", schema=None) as batch_op: with op.batch_alter_table("roms", schema=None) as batch_op:

View File

@@ -82,8 +82,7 @@ def upgrade() -> None:
) )
if is_postgresql(connection): if is_postgresql(connection):
op.execute( op.execute("""
"""
INSERT INTO rom_files ( INSERT INTO rom_files (
rom_id, rom_id,
file_name, file_name,
@@ -107,11 +106,9 @@ def upgrade() -> None:
CROSS JOIN jsonb_array_elements(r.files) AS file_data CROSS JOIN jsonb_array_elements(r.files) AS file_data
WHERE file_data->>'filename' IS NOT NULL WHERE file_data->>'filename' IS NOT NULL
AND file_data->>'filename' <> ''; AND file_data->>'filename' <> '';
""" """)
)
else: else:
op.execute( op.execute("""
"""
INSERT INTO rom_files ( INSERT INTO rom_files (
rom_id, rom_id,
file_name, file_name,
@@ -142,8 +139,7 @@ def upgrade() -> None:
) AS extracted_files ) AS extracted_files
WHERE JSON_UNQUOTE(JSON_EXTRACT(file_data, '$.filename')) IS NOT NULL WHERE JSON_UNQUOTE(JSON_EXTRACT(file_data, '$.filename')) IS NOT NULL
AND JSON_UNQUOTE(JSON_EXTRACT(file_data, '$.filename')) <> ''; AND JSON_UNQUOTE(JSON_EXTRACT(file_data, '$.filename')) <> '';
""" """)
)
with op.batch_alter_table("roms", schema=None) as batch_op: with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.alter_column( batch_op.alter_column(
@@ -210,8 +206,7 @@ def downgrade() -> None:
) )
if is_postgresql(connection): if is_postgresql(connection):
op.execute( op.execute("""
"""
WITH aggregated_data AS ( WITH aggregated_data AS (
SELECT SELECT
rom_id, rom_id,
@@ -234,11 +229,9 @@ def downgrade() -> None:
file_size_bytes = aggregated_data.total_size file_size_bytes = aggregated_data.total_size
FROM aggregated_data FROM aggregated_data
WHERE roms.id = aggregated_data.rom_id; WHERE roms.id = aggregated_data.rom_id;
""" """)
)
else: else:
op.execute( op.execute("""
"""
UPDATE roms UPDATE roms
JOIN ( JOIN (
SELECT SELECT
@@ -260,8 +253,7 @@ def downgrade() -> None:
roms.files = aggregated_data.files, roms.files = aggregated_data.files,
roms.multi = aggregated_data.multi, roms.multi = aggregated_data.multi,
roms.file_size_bytes = aggregated_data.total_size; roms.file_size_bytes = aggregated_data.total_size;
""" """)
)
op.drop_table("rom_files") op.drop_table("rom_files")

View File

@@ -46,21 +46,16 @@ def upgrade() -> None:
connection = op.get_bind() connection = op.get_bind()
if is_postgresql(connection): if is_postgresql(connection):
connection.execute( connection.execute(sa.text("""
sa.text(
"""
INSERT INTO collections_roms (collection_id, rom_id, created_at, updated_at) INSERT INTO collections_roms (collection_id, rom_id, created_at, updated_at)
SELECT c.id, rom_id::INT, NOW(), NOW() SELECT c.id, rom_id::INT, NOW(), NOW()
FROM collections c, FROM collections c,
LATERAL jsonb_array_elements_text(c.roms) AS rom_id LATERAL jsonb_array_elements_text(c.roms) AS rom_id
LEFT JOIN roms r ON rom_id::INT = r.id LEFT JOIN roms r ON rom_id::INT = r.id
WHERE r.id IS NOT NULL; WHERE r.id IS NOT NULL;
""" """))
)
)
connection.execute( connection.execute(
sa.text( sa.text("""
"""
CREATE OR REPLACE VIEW virtual_collections AS CREATE OR REPLACE VIEW virtual_collections AS
WITH genres_collection AS ( WITH genres_collection AS (
SELECT SELECT
@@ -135,25 +130,19 @@ def upgrade() -> None:
GROUP BY collection_type, collection_name GROUP BY collection_type, collection_name
HAVING COUNT(DISTINCT rom_id) > 2 HAVING COUNT(DISTINCT rom_id) > 2
ORDER BY collection_type, collection_name; ORDER BY collection_type, collection_name;
""" # nosec B608 """), # nosec B608
),
) )
else: else:
connection.execute( connection.execute(sa.text("""
sa.text(
"""
INSERT INTO collections_roms (collection_id, rom_id, created_at, updated_at) INSERT INTO collections_roms (collection_id, rom_id, created_at, updated_at)
SELECT c.id, jt.rom_id, NOW(), NOW() SELECT c.id, jt.rom_id, NOW(), NOW()
FROM collections c FROM collections c
JOIN JSON_TABLE(c.roms, '$[*]' COLUMNS (rom_id INT PATH '$')) AS jt JOIN JSON_TABLE(c.roms, '$[*]' COLUMNS (rom_id INT PATH '$')) AS jt
LEFT JOIN roms r ON jt.rom_id = r.id LEFT JOIN roms r ON jt.rom_id = r.id
WHERE r.id IS NOT NULL; WHERE r.id IS NOT NULL;
""" """))
)
)
connection.execute( connection.execute(
sa.text( sa.text("""
"""
CREATE OR REPLACE VIEW virtual_collections AS CREATE OR REPLACE VIEW virtual_collections AS
WITH genres AS ( WITH genres AS (
SELECT SELECT
@@ -259,8 +248,7 @@ def upgrade() -> None:
GROUP BY collection_type, collection_name GROUP BY collection_type, collection_name
HAVING COUNT(DISTINCT rom_id) > 2 HAVING COUNT(DISTINCT rom_id) > 2
ORDER BY collection_type, collection_name; ORDER BY collection_type, collection_name;
""" """),
),
) )
op.drop_column("collections", "roms") op.drop_column("collections", "roms")
@@ -272,9 +260,7 @@ def downgrade() -> None:
connection = op.get_bind() connection = op.get_bind()
if is_postgresql(connection): if is_postgresql(connection):
connection.execute( connection.execute(sa.text("""
sa.text(
"""
UPDATE collections c UPDATE collections c
SET roms = COALESCE( SET roms = COALESCE(
(SELECT jsonb_agg(rom_id) (SELECT jsonb_agg(rom_id)
@@ -282,13 +268,9 @@ def downgrade() -> None:
WHERE cr.collection_id = c.id), WHERE cr.collection_id = c.id),
'[]'::jsonb '[]'::jsonb
); );
""" """))
)
)
else: else:
connection.execute( connection.execute(sa.text("""
sa.text(
"""
UPDATE collections c UPDATE collections c
JOIN ( JOIN (
SELECT collection_id, IFNULL(JSON_ARRAYAGG(rom_id), JSON_ARRAY()) AS roms SELECT collection_id, IFNULL(JSON_ARRAYAGG(rom_id), JSON_ARRAY()) AS roms
@@ -298,9 +280,7 @@ def downgrade() -> None:
ON c.id = cr.collection_id ON c.id = cr.collection_id
SET c.roms = cr.roms; SET c.roms = cr.roms;
""" """))
)
)
with op.batch_alter_table("collections", schema=None) as batch_op: with op.batch_alter_table("collections", schema=None) as batch_op:
batch_op.alter_column("roms", existing_type=CustomJSON(), nullable=False) batch_op.alter_column("roms", existing_type=CustomJSON(), nullable=False)
@@ -308,9 +288,7 @@ def downgrade() -> None:
op.drop_table("collections_roms") op.drop_table("collections_roms")
connection.execute( connection.execute(
sa.text( sa.text("""
"""
DROP VIEW virtual_collections; DROP VIEW virtual_collections;
""" """),
),
) )

View File

@@ -21,9 +21,7 @@ depends_on = None
def upgrade(): def upgrade():
connection = op.get_bind() connection = op.get_bind()
if is_postgresql(connection): if is_postgresql(connection):
connection.execute( connection.execute(sa.text("""
sa.text(
"""
CREATE OR REPLACE VIEW roms_metadata AS CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id AS rom_id, r.id AS rom_id,
@@ -118,13 +116,9 @@ def upgrade():
END AS ss_rating END AS ss_rating
FROM roms r FROM roms r
) AS r; ) AS r;
""" """))
)
)
else: else:
connection.execute( connection.execute(sa.text("""CREATE OR REPLACE VIEW roms_metadata AS
sa.text(
"""CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id as rom_id, r.id as rom_id,
NOW() AS created_at, NOW() AS created_at,
@@ -220,9 +214,7 @@ def upgrade():
END AS ss_rating END AS ss_rating
FROM roms FROM roms
) AS r; ) AS r;
""" """))
)
)
def downgrade(): def downgrade():

View File

@@ -25,8 +25,7 @@ def upgrade() -> None:
) )
connection.execute( connection.execute(
sa.text( sa.text(f"""
f"""
CREATE OR REPLACE VIEW sibling_roms AS CREATE OR REPLACE VIEW sibling_roms AS
SELECT SELECT
r1.id AS rom_id, r1.id AS rom_id,
@@ -51,8 +50,7 @@ def upgrade() -> None:
OR OR
(r1.ss_id = r2.ss_id AND r1.ss_id IS NOT NULL) (r1.ss_id = r2.ss_id AND r1.ss_id IS NOT NULL)
); );
""" # nosec B608 """), # nosec B608
),
) )
@@ -65,8 +63,7 @@ def downgrade() -> None:
connection.execute(sa.text("DROP VIEW IF EXISTS sibling_roms;")) connection.execute(sa.text("DROP VIEW IF EXISTS sibling_roms;"))
connection.execute( connection.execute(
sa.text( sa.text(f"""
f"""
CREATE VIEW sibling_roms AS CREATE VIEW sibling_roms AS
SELECT SELECT
r1.id AS rom_id, r1.id AS rom_id,
@@ -88,6 +85,5 @@ def downgrade() -> None:
OR OR
(r1.moby_id = r2.moby_id AND r1.moby_id IS NOT NULL) (r1.moby_id = r2.moby_id AND r1.moby_id IS NOT NULL)
); );
""" # nosec B608 """), # nosec B608
),
) )

View File

@@ -43,15 +43,11 @@ def upgrade() -> None:
# Build query based on whether emulator exists # Build query based on whether emulator exists
select_cols = "id, file_path, rom_id" + (", emulator" if has_emulator else "") select_cols = "id, file_path, rom_id" + (", emulator" if has_emulator else "")
results = conn.execute( results = conn.execute(text(f"""
text(
f"""
SELECT {select_cols} SELECT {select_cols}
FROM {table} FROM {table}
WHERE {like_clause} WHERE {like_clause}
""" # nosec B608 """)).fetchall() # nosec B608
)
).fetchall()
for row in results: for row in results:
item_id = row.id item_id = row.id
@@ -102,13 +98,11 @@ def upgrade() -> None:
# Update DB # Update DB
conn.execute( conn.execute(
text( text(f"""
f"""
UPDATE {table} UPDATE {table}
SET file_path = :new_path SET file_path = :new_path
WHERE id = :item_id WHERE id = :item_id
""" # nosec B608 """), # nosec B608
),
{"new_path": new_path, "item_id": item_id}, {"new_path": new_path, "item_id": item_id},
) )

View File

@@ -23,20 +23,14 @@ depends_on = None
def upgrade() -> None: def upgrade() -> None:
conn = op.get_bind() conn = op.get_bind()
conn.execute( conn.execute(text("""
text(
"""
UPDATE roms UPDATE roms
SET url_cover = replace(url_cover, 't_thumb', 't_1080p') SET url_cover = replace(url_cover, 't_thumb', 't_1080p')
WHERE url_cover LIKE '%t_thumb%' WHERE url_cover LIKE '%t_thumb%'
""" """))
)
)
if is_postgresql(conn): if is_postgresql(conn):
conn.execute( conn.execute(text("""
text(
"""
UPDATE roms UPDATE roms
SET url_screenshots = ( SET url_screenshots = (
SELECT jsonb_agg( SELECT jsonb_agg(
@@ -46,19 +40,13 @@ def upgrade() -> None:
) )
WHERE url_screenshots IS NOT NULL WHERE url_screenshots IS NOT NULL
AND url_screenshots::text LIKE '%t_thumb%'; AND url_screenshots::text LIKE '%t_thumb%';
""" """))
)
)
else: else:
result = conn.execute( result = conn.execute(text("""
text(
"""
SELECT id, url_screenshots SELECT id, url_screenshots
FROM roms FROM roms
WHERE JSON_SEARCH(url_screenshots, 'one', '%t_thumb%') IS NOT NULL WHERE JSON_SEARCH(url_screenshots, 'one', '%t_thumb%') IS NOT NULL
""" """))
)
)
for row in result: for row in result:
row_id, screenshots_json = row row_id, screenshots_json = row
@@ -75,13 +63,11 @@ def upgrade() -> None:
for url in screenshots for url in screenshots
] ]
conn.execute( conn.execute(
text( text("""
"""
UPDATE roms UPDATE roms
SET url_screenshots = :screenshots SET url_screenshots = :screenshots
WHERE id = :id WHERE id = :id
""" """),
),
{ {
"screenshots": json.dumps(updated_screenshots), "screenshots": json.dumps(updated_screenshots),
"id": row_id, "id": row_id,

View File

@@ -21,9 +21,7 @@ depends_on = None
def upgrade(): def upgrade():
connection = op.get_bind() connection = op.get_bind()
if is_postgresql(connection): if is_postgresql(connection):
connection.execute( connection.execute(sa.text("""
sa.text(
"""
CREATE OR REPLACE VIEW roms_metadata AS CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id AS rom_id, r.id AS rom_id,
@@ -157,13 +155,9 @@ def upgrade():
END AS launchbox_rating END AS launchbox_rating
FROM roms r FROM roms r
) AS r; ) AS r;
""" """))
)
)
else: else:
connection.execute( connection.execute(sa.text("""CREATE OR REPLACE VIEW roms_metadata AS
sa.text(
"""CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id as rom_id, r.id as rom_id,
NOW() AS created_at, NOW() AS created_at,
@@ -294,9 +288,7 @@ def upgrade():
END AS launchbox_rating END AS launchbox_rating
FROM roms FROM roms
) AS r; ) AS r;
""" """))
)
)
def downgrade(): def downgrade():

View File

@@ -25,8 +25,7 @@ def upgrade() -> None:
) )
connection.execute( connection.execute(
sa.text( sa.text(f"""
f"""
CREATE OR REPLACE VIEW sibling_roms AS CREATE OR REPLACE VIEW sibling_roms AS
SELECT SELECT
r1.id AS rom_id, r1.id AS rom_id,
@@ -63,8 +62,7 @@ def upgrade() -> None:
OR OR
(r1.tgdb_id = r2.tgdb_id AND r1.tgdb_id IS NOT NULL) (r1.tgdb_id = r2.tgdb_id AND r1.tgdb_id IS NOT NULL)
); );
""" # nosec B608 """), # nosec B608
),
) )
@@ -77,8 +75,7 @@ def downgrade() -> None:
connection.execute(sa.text("DROP VIEW IF EXISTS sibling_roms;")) connection.execute(sa.text("DROP VIEW IF EXISTS sibling_roms;"))
connection.execute( connection.execute(
sa.text( sa.text(f"""
f"""
CREATE VIEW sibling_roms AS CREATE VIEW sibling_roms AS
SELECT SELECT
r1.id AS rom_id, r1.id AS rom_id,
@@ -103,6 +100,5 @@ def downgrade() -> None:
OR OR
(r1.ss_id = r2.ss_id AND r1.ss_id IS NOT NULL) (r1.ss_id = r2.ss_id AND r1.ss_id IS NOT NULL)
); );
""" # nosec B608 """), # nosec B608
),
) )

View File

@@ -25,15 +25,11 @@ def upgrade() -> None:
# Get all firmware records with their hash information # Get all firmware records with their hash information
connection = op.get_bind() connection = op.get_bind()
result = connection.execute( result = connection.execute(sa.text("""
sa.text(
"""
SELECT f.id, p.slug as platform_slug, f.file_name, f.file_size_bytes, f.md5_hash, f.sha1_hash, f.crc_hash SELECT f.id, p.slug as platform_slug, f.file_name, f.file_size_bytes, f.md5_hash, f.sha1_hash, f.crc_hash
FROM firmware f FROM firmware f
JOIN platforms p ON f.platform_id = p.id JOIN platforms p ON f.platform_id = p.id
""" """))
)
)
all_firmware = result.fetchall() all_firmware = result.fetchall()
verified_firmware_ids = [] verified_firmware_ids = []

View File

@@ -21,9 +21,7 @@ depends_on = None
def upgrade(): def upgrade():
connection = op.get_bind() connection = op.get_bind()
if is_postgresql(connection): if is_postgresql(connection):
connection.execute( connection.execute(sa.text("""
sa.text(
"""
CREATE OR REPLACE VIEW roms_metadata AS CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id AS rom_id, r.id AS rom_id,
@@ -165,13 +163,9 @@ def upgrade():
END AS launchbox_rating END AS launchbox_rating
FROM roms r FROM roms r
) AS r; ) AS r;
""" """))
)
)
else: else:
connection.execute( connection.execute(sa.text("""CREATE OR REPLACE VIEW roms_metadata AS
sa.text(
"""CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id as rom_id, r.id as rom_id,
NOW() AS created_at, NOW() AS created_at,
@@ -312,9 +306,7 @@ def upgrade():
END AS launchbox_rating END AS launchbox_rating
FROM roms FROM roms
) AS r; ) AS r;
""" """))
)
)
def downgrade(): def downgrade():

View File

@@ -21,13 +21,11 @@ def upgrade() -> None:
batch_op.add_column(sa.Column("is_favorite", sa.Boolean(), nullable=True)) batch_op.add_column(sa.Column("is_favorite", sa.Boolean(), nullable=True))
op.execute("UPDATE collections SET is_favorite = FALSE WHERE is_favorite IS NULL") op.execute("UPDATE collections SET is_favorite = FALSE WHERE is_favorite IS NULL")
op.execute( op.execute("""
"""
UPDATE collections UPDATE collections
SET is_favorite = TRUE SET is_favorite = TRUE
WHERE LOWER(name) IN ('favourites', 'favorites') WHERE LOWER(name) IN ('favourites', 'favorites')
""" """)
)
with op.batch_alter_table("collections", schema=None) as batch_op: with op.batch_alter_table("collections", schema=None) as batch_op:
batch_op.alter_column("is_favorite", existing_type=sa.Boolean(), nullable=False) batch_op.alter_column("is_favorite", existing_type=sa.Boolean(), nullable=False)

View File

@@ -75,23 +75,17 @@ def upgrade() -> None:
# Migrate existing notes from rom_user to rom_notes table # Migrate existing notes from rom_user to rom_notes table
# Both note_raw_markdown and note_is_public columns exist from previous migrations # Both note_raw_markdown and note_is_public columns exist from previous migrations
connection = op.get_bind() connection = op.get_bind()
result = connection.execute( result = connection.execute(text("""
text(
"""
SELECT id, rom_id, user_id, note_raw_markdown, note_is_public, updated_at SELECT id, rom_id, user_id, note_raw_markdown, note_is_public, updated_at
FROM rom_user FROM rom_user
""" """))
)
)
for row in result: for row in result:
connection.execute( connection.execute(
text( text("""
"""
INSERT INTO rom_notes (title, content, is_public, tags, created_at, updated_at, rom_id, user_id) INSERT INTO rom_notes (title, content, is_public, tags, created_at, updated_at, rom_id, user_id)
VALUES (:title, :content, :is_public, :tags, :created_at, :updated_at, :rom_id, :user_id) VALUES (:title, :content, :is_public, :tags, :created_at, :updated_at, :rom_id, :user_id)
""" """),
),
{ {
"title": "My Note", "title": "My Note",
"content": row.note_raw_markdown or "", # Handle potential NULL content "content": row.note_raw_markdown or "", # Handle potential NULL content
@@ -125,26 +119,20 @@ def downgrade() -> None:
# Migrate notes back to rom_user (take first note per user/rom) # Migrate notes back to rom_user (take first note per user/rom)
connection = op.get_bind() connection = op.get_bind()
result = connection.execute( result = connection.execute(text("""
text(
"""
SELECT DISTINCT rom_id, user_id, SELECT DISTINCT rom_id, user_id,
FIRST_VALUE(content) OVER (PARTITION BY rom_id, user_id ORDER BY updated_at DESC) as content, FIRST_VALUE(content) OVER (PARTITION BY rom_id, user_id ORDER BY updated_at DESC) as content,
FIRST_VALUE(is_public) OVER (PARTITION BY rom_id, user_id ORDER BY updated_at DESC) as is_public FIRST_VALUE(is_public) OVER (PARTITION BY rom_id, user_id ORDER BY updated_at DESC) as is_public
FROM rom_notes FROM rom_notes
""" """))
)
)
for row in result: for row in result:
connection.execute( connection.execute(
text( text("""
"""
UPDATE rom_user UPDATE rom_user
SET note_raw_markdown = :content, note_is_public = :is_public SET note_raw_markdown = :content, note_is_public = :is_public
WHERE rom_id = :rom_id AND user_id = :user_id WHERE rom_id = :rom_id AND user_id = :user_id
""" """),
),
{ {
"content": row.content, "content": row.content,
"is_public": row.is_public, "is_public": row.is_public,

View File

@@ -21,9 +21,7 @@ depends_on = None
def upgrade(): def upgrade():
connection = op.get_bind() connection = op.get_bind()
if is_postgresql(connection): if is_postgresql(connection):
connection.execute( connection.execute(sa.text("""
sa.text(
"""
CREATE OR REPLACE VIEW roms_metadata AS CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id AS rom_id, r.id AS rom_id,
@@ -185,13 +183,9 @@ def upgrade():
END AS gamelist_rating END AS gamelist_rating
FROM roms r FROM roms r
) AS r; ) AS r;
""" """))
)
)
else: else:
connection.execute( connection.execute(sa.text("""CREATE OR REPLACE VIEW roms_metadata AS
sa.text(
"""CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id as rom_id, r.id as rom_id,
NOW() AS created_at, NOW() AS created_at,
@@ -354,9 +348,7 @@ def upgrade():
END AS gamelist_rating END AS gamelist_rating
FROM roms FROM roms
) AS r; ) AS r;
""" """))
)
)
def downgrade(): def downgrade():

View File

@@ -34,9 +34,7 @@ def upgrade():
) )
if is_postgresql(connection): if is_postgresql(connection):
connection.execute( connection.execute(sa.text("""
sa.text(
"""
CREATE OR REPLACE VIEW roms_metadata AS CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id AS rom_id, r.id AS rom_id,
@@ -209,13 +207,9 @@ def upgrade():
END AS gamelist_rating END AS gamelist_rating
FROM roms r FROM roms r
) AS r; ) AS r;
""" """))
)
)
else: else:
connection.execute( connection.execute(sa.text("""CREATE OR REPLACE VIEW roms_metadata AS
sa.text(
"""CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id as rom_id, r.id as rom_id,
NOW() AS created_at, NOW() AS created_at,
@@ -389,9 +383,7 @@ def upgrade():
END AS gamelist_rating END AS gamelist_rating
FROM roms FROM roms
) AS r; ) AS r;
""" """))
)
)
def downgrade(): def downgrade():

View File

@@ -40,23 +40,19 @@ def upgrade() -> None:
rom_file_category_enum.create(connection, checkfirst=True) rom_file_category_enum.create(connection, checkfirst=True)
# remove bad values before alter # remove bad values before alter
op.execute( op.execute("""
"""
UPDATE rom_files UPDATE rom_files
SET category = NULL SET category = NULL
WHERE category IS NOT NULL WHERE category IS NOT NULL
AND category NOT IN ('GAME', 'DLC', 'HACK', 'MANUAL', 'PATCH', 'UPDATE', 'MOD', 'DEMO', 'TRANSLATION', 'PROTOTYPE') AND category NOT IN ('GAME', 'DLC', 'HACK', 'MANUAL', 'PATCH', 'UPDATE', 'MOD', 'DEMO', 'TRANSLATION', 'PROTOTYPE')
""" """)
)
# postgres being picky about needing USING # postgres being picky about needing USING
op.execute( op.execute("""
"""
ALTER TABLE rom_files ALTER TABLE rom_files
ALTER COLUMN category TYPE romfilecategory ALTER COLUMN category TYPE romfilecategory
USING category::text::romfilecategory USING category::text::romfilecategory
""" """)
)
else: else:
rom_file_category_enum = sa.Enum( rom_file_category_enum = sa.Enum(

View File

@@ -21,9 +21,7 @@ depends_on = None
def upgrade(): def upgrade():
connection = op.get_bind() connection = op.get_bind()
if is_postgresql(connection): if is_postgresql(connection):
connection.execute( connection.execute(sa.text("""
sa.text(
"""
CREATE OR REPLACE VIEW roms_metadata AS CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id AS rom_id, r.id AS rom_id,
@@ -204,13 +202,9 @@ def upgrade():
END AS gamelist_rating END AS gamelist_rating
FROM roms r FROM roms r
) AS r; ) AS r;
""" """))
)
)
else: else:
connection.execute( connection.execute(sa.text("""
sa.text(
"""
CREATE OR REPLACE VIEW roms_metadata AS CREATE OR REPLACE VIEW roms_metadata AS
SELECT SELECT
r.id as rom_id, r.id as rom_id,
@@ -393,9 +387,7 @@ def upgrade():
END AS gamelist_rating END AS gamelist_rating
FROM roms FROM roms
) AS r; ) AS r;
""" """))
)
)
def downgrade(): def downgrade():

View File

@@ -29,8 +29,7 @@ def upgrade() -> None:
) )
connection.execute( connection.execute(
sa.text( sa.text(f"""
f"""
CREATE OR REPLACE VIEW sibling_roms AS CREATE OR REPLACE VIEW sibling_roms AS
SELECT SELECT
r1.id AS rom_id, r1.id AS rom_id,
@@ -69,8 +68,7 @@ def upgrade() -> None:
OR OR
(r1.fs_name_no_tags = r2.fs_name_no_tags) (r1.fs_name_no_tags = r2.fs_name_no_tags)
); );
""" # nosec B608 """), # nosec B608
),
) )
@@ -82,8 +80,7 @@ def downgrade() -> None:
# Restore previous view without fs_name_no_tags fallback # Restore previous view without fs_name_no_tags fallback
connection.execute( connection.execute(
sa.text( sa.text(f"""
f"""
CREATE OR REPLACE VIEW sibling_roms AS CREATE OR REPLACE VIEW sibling_roms AS
SELECT SELECT
r1.id AS rom_id, r1.id AS rom_id,
@@ -120,8 +117,7 @@ def downgrade() -> None:
OR OR
(r1.tgdb_id = r2.tgdb_id AND r1.tgdb_id IS NOT NULL) (r1.tgdb_id = r2.tgdb_id AND r1.tgdb_id IS NOT NULL)
); );
""" # nosec B608 """), # nosec B608
),
) )
# Remove the index # Remove the index

View File

@@ -86,8 +86,7 @@ def upgrade() -> None:
sa.Column("url_cover", sa.Text(), nullable=True), sa.Column("url_cover", sa.Text(), nullable=True),
sa.PrimaryKeyConstraint("id"), sa.PrimaryKeyConstraint("id"),
) )
batch_op.execute( batch_op.execute("INSERT INTO roms(\
"INSERT INTO roms(\
r_igdb_id, p_igdb_id, r_sgdb_id, p_sgdb_id, p_slug, p_name, \ r_igdb_id, p_igdb_id, r_sgdb_id, p_sgdb_id, p_slug, p_name, \
file_name, file_name_no_tags, file_extension, file_path, file_size, \ file_name, file_name_no_tags, file_extension, file_path, file_size, \
file_size_units, r_name, r_slug, summary, path_cover_s, path_cover_l, has_cover, \ file_size_units, r_name, r_slug, summary, path_cover_s, path_cover_l, has_cover, \
@@ -97,8 +96,7 @@ def upgrade() -> None:
file_name, file_name_no_tags, file_extension, file_path, file_size, \ file_name, file_name_no_tags, file_extension, file_path, file_size, \
file_size_units, r_name, r_slug, summary, path_cover_s, path_cover_l, has_cover, \ file_size_units, r_name, r_slug, summary, path_cover_s, path_cover_l, has_cover, \
region, revision, tags, multi, files, url_cover \ region, revision, tags, multi, files, url_cover \
FROM old_roms" FROM old_roms")
)
op.drop_table("old_roms") op.drop_table("old_roms")
# ### end Alembic commands ### # ### end Alembic commands ###

View File

@@ -131,7 +131,7 @@ async def token(form_data: Annotated[OAuth2RequestForm, Depends()]) -> TokenResp
return { return {
"access_token": access_token, "access_token": access_token,
"token_type": "bearer", "token_type": "bearer", # trunk-ignore(bandit/B105)
"expires": ACCESS_TOKEN_EXPIRE_MINUTES * 60, "expires": ACCESS_TOKEN_EXPIRE_MINUTES * 60,
} }
@@ -199,7 +199,7 @@ async def token(form_data: Annotated[OAuth2RequestForm, Depends()]) -> TokenResp
return { return {
"access_token": access_token, "access_token": access_token,
"refresh_token": refresh_token, "refresh_token": refresh_token,
"token_type": "bearer", "token_type": "bearer", # trunk-ignore(bandit/B105)
"expires": ACCESS_TOKEN_EXPIRE_MINUTES * 60, "expires": ACCESS_TOKEN_EXPIRE_MINUTES * 60,
} }

View File

@@ -275,7 +275,7 @@ class MetadataHandler(abc.ABC):
return search_term return search_term
def _mask_sensitive_values(self, values: dict[str, str | None]) -> dict[str, str]: def _mask_sensitive_values(self, values: dict[str, str]) -> dict[str, str]:
""" """
Mask sensitive values (headers or params), leaving only the first 2 and last 2 characters of the token. Mask sensitive values (headers or params), leaving only the first 2 and last 2 characters of the token.
""" """

View File

@@ -103,6 +103,9 @@ def extract_metadata_from_igdb_rom(rom: dict[str, Any]) -> IGDBMetadata:
"remasters": [], "remasters": [],
"similar_games": [], "similar_games": [],
"expanded_games": [], "expanded_games": [],
# TODO: extract multiplayer modes and derive player count
"multiplayer_modes": [],
"player_count": "1",
} }
) )

View File

@@ -68,10 +68,8 @@ if __name__ == "__main__":
sorted(supported_platforms.items(), key=lambda item: item[1]["name"].lower()) sorted(supported_platforms.items(), key=lambda item: item[1]["name"].lower())
) )
print( print("""|Platform Name|Folder Name|Metadata Providers|
"""|Platform Name|Folder Name|Metadata Providers| |---|---|---|""")
|---|---|---|"""
)
for platform in supported_platforms.values(): for platform in supported_platforms.values():
print( print(