Skip to content

Commit 80a985f

Browse files
authored
Merge pull request #3767 from rommapp/posthog-code/fix-migration-idempotency
fix(migrations): make DDL idempotent on partial-failure reruns
2 parents 030952b + 4485116 commit 80a985f

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

backend/alembic/versions/0033_rom_file_and_hashes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def upgrade() -> None:
3737
name="romfilecategory",
3838
create_type=False,
3939
)
40-
rom_file_category_enum.create(connection, checkfirst=False)
40+
rom_file_category_enum.create(connection, checkfirst=True)
4141
else:
4242
rom_file_category_enum = sa.Enum(
4343
"GAME",
@@ -263,4 +263,4 @@ def downgrade() -> None:
263263
op.drop_table("rom_files", if_exists=True)
264264

265265
if is_postgresql(connection):
266-
ENUM(name="romfilecategory").drop(connection, checkfirst=False)
266+
ENUM(name="romfilecategory").drop(connection, checkfirst=True)

backend/alembic/versions/0084_add_roms_search_index.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,18 @@ def upgrade() -> None:
4747

4848
# 1. DB-specific search indexes on roms.name and roms.fs_name.
4949
if is_mysql(bind) or is_mariadb(bind):
50-
op.execute(
51-
sa.text(
52-
f"CREATE FULLTEXT INDEX {FULLTEXT_INDEX_NAME} "
53-
"ON roms (name, fs_name)"
50+
# MySQL/MariaDB have no portable "IF NOT EXISTS" for FULLTEXT indexes, so
51+
# guard the create to stay idempotent if a prior run partially applied.
52+
existing_indexes = {
53+
index["name"] for index in sa.inspect(bind).get_indexes("roms")
54+
}
55+
if FULLTEXT_INDEX_NAME not in existing_indexes:
56+
op.execute(
57+
sa.text(
58+
f"CREATE FULLTEXT INDEX {FULLTEXT_INDEX_NAME} "
59+
"ON roms (name, fs_name)"
60+
)
5461
)
55-
)
5662
elif is_postgresql(bind):
5763
# pg_trgm is a trusted extension since PostgreSQL 13, so a non-superuser
5864
# with CREATE on the database can install it.
@@ -130,7 +136,12 @@ def downgrade() -> None:
130136

131137
# 1. DB-specific search indexes.
132138
if is_mysql(bind) or is_mariadb(bind):
133-
op.execute(sa.text(f"DROP INDEX {FULLTEXT_INDEX_NAME} ON roms"))
139+
# MySQL has no portable "IF EXISTS" for DROP INDEX, so guard the drop.
140+
existing_indexes = {
141+
index["name"] for index in sa.inspect(bind).get_indexes("roms")
142+
}
143+
if FULLTEXT_INDEX_NAME in existing_indexes:
144+
op.execute(sa.text(f"DROP INDEX {FULLTEXT_INDEX_NAME} ON roms"))
134145
elif is_postgresql(bind):
135146
# Leave the pg_trgm extension in place; other objects may depend on it.
136147
op.execute(sa.text(f"DROP INDEX IF EXISTS {PG_FS_NAME_INDEX}"))

0 commit comments

Comments
 (0)