diff --git a/backend/alembic/versions/0062_rom_file_category_enum.py b/backend/alembic/versions/0062_rom_file_category_enum.py index 75ae8d08f..d184a32c9 100644 --- a/backend/alembic/versions/0062_rom_file_category_enum.py +++ b/backend/alembic/versions/0062_rom_file_category_enum.py @@ -38,6 +38,26 @@ def upgrade() -> None: create_type=False, ) rom_file_category_enum.create(connection, checkfirst=True) + + # remove bad values before alter + op.execute( + """ + UPDATE rom_files + SET category = NULL + WHERE category IS NOT NULL + AND category NOT IN ('GAME', 'DLC', 'HACK', 'MANUAL', 'PATCH', 'UPDATE', 'MOD', 'DEMO', 'TRANSLATION', 'PROTOTYPE') + """ + ) + + # postgres being picky about needing USING + op.execute( + """ + ALTER TABLE rom_files + ALTER COLUMN category TYPE romfilecategory + USING category::text::romfilecategory + """ + ) + else: rom_file_category_enum = sa.Enum( "GAME", @@ -53,8 +73,10 @@ def upgrade() -> None: name="romfilecategory", ) - with op.batch_alter_table("rom_files", schema=None) as batch_op: - batch_op.alter_column("category", type_=rom_file_category_enum, nullable=True) + with op.batch_alter_table("rom_files", schema=None) as batch_op: + batch_op.alter_column( + "category", type_=rom_file_category_enum, nullable=True + ) def downgrade() -> None: