Skip to content

Commit

Permalink
Merge pull request #1645 from rommapp/hotfix-collections-migration
Browse files Browse the repository at this point in the history
[HOTFIX] Fix collections migration in both directions
  • Loading branch information
gantoine authored Feb 22, 2025
2 parents 4e8c107 + f3a7ed0 commit fd40011
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
10 changes: 5 additions & 5 deletions .trunk/trunk.yaml
19 changes: 14 additions & 5 deletions backend/alembic/versions/0034_virtual_collections_db_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def upgrade() -> None:
SELECT c.id, rom_id::INT, NOW(), NOW()
FROM collections c,
LATERAL jsonb_array_elements_text(c.roms) AS rom_id
LEFT JOIN roms r ON rom_id::INT = r.id
WHERE r.id IS NOT NULL;
"""
)
)
Expand Down Expand Up @@ -143,6 +145,8 @@ def upgrade() -> None:
SELECT c.id, jt.rom_id, NOW(), NOW()
FROM collections c
JOIN JSON_TABLE(c.roms, '$[*]' COLUMNS (rom_id INT PATH '$')) AS jt
LEFT JOIN roms r ON jt.rom_id = r.id
WHERE r.id IS NOT NULL;
"""
)
)
Expand Down Expand Up @@ -263,18 +267,19 @@ def upgrade() -> None:

def downgrade() -> None:
with op.batch_alter_table("collections", schema=None) as batch_op:
batch_op.add_column(sa.Column("roms", CustomJSON(), nullable=False))
batch_op.add_column(sa.Column("roms", CustomJSON(), nullable=True))

connection = op.get_bind()
if is_postgresql(connection):
connection.execute(
sa.text(
"""
UPDATE collections c
SET roms = (
SELECT jsonb_agg(rom_id)
SET roms = COALESCE(
(SELECT jsonb_agg(rom_id)
FROM collections_roms cr
WHERE cr.collection_id = c.id
WHERE cr.collection_id = c.id),
'[]'::jsonb
);
"""
)
Expand All @@ -285,16 +290,20 @@ def downgrade() -> None:
"""
UPDATE collections c
JOIN (
SELECT collection_id, JSON_ARRAYAGG(rom_id) as roms
SELECT collection_id, IFNULL(JSON_ARRAYAGG(rom_id), JSON_ARRAY()) AS roms
FROM collections_roms
GROUP BY collection_id
) cr
ON c.id = cr.collection_id
SET c.roms = cr.roms;
"""
)
)

with op.batch_alter_table("collections", schema=None) as batch_op:
batch_op.alter_column("roms", existing_type=CustomJSON(), nullable=False)

op.drop_table("collections_roms")

connection.execute(
Expand Down
5 changes: 2 additions & 3 deletions backend/alembic/versions/0035_screenscraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def upgrade() -> None:
with op.batch_alter_table("roms", schema=None) as batch_op:
batch_op.add_column(sa.Column("ss_id", sa.Integer(), nullable=True))
batch_op.add_column(sa.Column("ss_metadata", CustomJSON(), nullable=True))
batch_op.add_column(sa.Column("url_manual", sa.Text(), nullable=True)),
batch_op.add_column(sa.Column("path_manual", sa.Text(), nullable=True)),
batch_op.add_column(sa.Column("url_manual", sa.Text(), nullable=True))
batch_op.add_column(sa.Column("path_manual", sa.Text(), nullable=True))
batch_op.create_index("idx_roms_ss_id", ["ss_id"], unique=False)
# ### end Alembic commands ###

Expand All @@ -39,7 +39,6 @@ def downgrade() -> None:
batch_op.drop_column("ss_metadata")
batch_op.drop_column("url_manual")
batch_op.drop_column("path_manual")
batch_op.drop_index("idx_roms_ss_id")

with op.batch_alter_table("platforms", schema=None) as batch_op:
batch_op.drop_column("ss_id")
Expand Down

0 comments on commit fd40011

Please sign in to comment.