Skip to content

Commit f69c8c1

Browse files
jessielwCopilot
andcommitted
refactor: improve idempotency of emby_season_id migration #109
Co-authored-by: Copilot <copilot@github.com>
1 parent 45af3a2 commit f69c8c1

1 file changed

Lines changed: 20 additions & 6 deletions

File tree

backend/alembic/versions/20666951d75d_add_emby_support_and_emby_season_id_to_.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,30 @@
1919

2020

2121
def upgrade() -> None:
22-
# ### commands auto generated by Alembic - please adjust! ###
23-
with op.batch_alter_table('seasons', schema=None) as batch_op:
24-
batch_op.add_column(sa.Column('emby_season_id', sa.String(length=100), nullable=True))
22+
### commands auto generated by Alembic - please adjust! ###
23+
conn = op.get_bind()
24+
inspector = sa.inspect(conn)
25+
existing_cols = [c['name'] for c in inspector.get_columns('seasons')]
26+
27+
# only add the column if it doesn't already exist (makes migration idempotent)
28+
if 'emby_season_id' not in existing_cols:
29+
with op.batch_alter_table('seasons', schema=None) as batch_op:
30+
batch_op.add_column(
31+
sa.Column('emby_season_id', sa.String(length=100), nullable=True)
32+
)
2533

2634
# ### end Alembic commands ###
2735

2836

2937
def downgrade() -> None:
30-
# ### commands auto generated by Alembic - please adjust! ###
31-
with op.batch_alter_table('seasons', schema=None) as batch_op:
32-
batch_op.drop_column('emby_season_id')
38+
### commands auto generated by Alembic - please adjust! ###
39+
conn = op.get_bind()
40+
inspector = sa.inspect(conn)
41+
existing_cols = [c['name'] for c in inspector.get_columns('seasons')]
42+
43+
# only drop the column if it exists
44+
if 'emby_season_id' in existing_cols:
45+
with op.batch_alter_table('seasons', schema=None) as batch_op:
46+
batch_op.drop_column('emby_season_id')
3347

3448
# ### end Alembic commands ###

0 commit comments

Comments
 (0)