|
| 1 | +"""empty message |
| 2 | +
|
| 3 | +Revision ID: 661514b12ee1 |
| 4 | +Revises: 46b89d830ad8 |
| 5 | +Create Date: 2025-06-05 19:52:31.221392 |
| 6 | +
|
| 7 | +""" |
| 8 | +from alembic import op |
| 9 | +import sqlalchemy as sa |
| 10 | +from alembic.context import get_context |
| 11 | + |
| 12 | + |
| 13 | +# revision identifiers, used by Alembic. |
| 14 | +revision = '661514b12ee1' |
| 15 | +down_revision = '46b89d830ad8' |
| 16 | +branch_labels = None |
| 17 | +depends_on = None |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +def upgrade(): |
| 22 | + # Get the SQLite connection context |
| 23 | + context = get_context() |
| 24 | + naming_convention = { |
| 25 | + "fk": |
| 26 | + "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", |
| 27 | + } |
| 28 | + # Use batch operations for SQLite |
| 29 | + with op.batch_alter_table('run_tag', naming_convention=naming_convention) as batch_op: |
| 30 | + # First drop the existing foreign key |
| 31 | + batch_op.drop_constraint('fk_run_tag_run_id_run', type_='foreignkey') |
| 32 | + batch_op.drop_constraint('fk_run_tag_tag_id_tag', type_='foreignkey') |
| 33 | + |
| 34 | + # Then create a new one with CASCADE |
| 35 | + batch_op.create_foreign_key('fk_run_tag_run_id_run', 'run', ['run_id'], ['id'], ondelete='CASCADE') |
| 36 | + batch_op.create_foreign_key('fk_run_tag_tag_id_tag', 'tag', ['tag_id'], ['id'], ondelete='CASCADE') |
| 37 | + |
| 38 | + |
| 39 | + with op.batch_alter_table('note', naming_convention=naming_convention) as batch_op: |
| 40 | + # First drop the existing foreign key |
| 41 | + batch_op.drop_constraint('fk_note_run_id_run', type_='foreignkey') |
| 42 | + |
| 43 | + # Then create a new one with CASCADE |
| 44 | + batch_op.create_foreign_key('fk_note_run_id_run', 'run', ['run_id'], ['id'], ondelete='CASCADE') |
| 45 | + |
| 46 | + |
| 47 | +def downgrade(): |
| 48 | + # Use batch operations for SQLite |
| 49 | + naming_convention = { |
| 50 | + "fk": |
| 51 | + "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s", |
| 52 | + } |
| 53 | + # Use batch operations for SQLite |
| 54 | + with op.batch_alter_table('run_tag', naming_convention=naming_convention) as batch_op: |
| 55 | + # Drop the CASCADE foreign key |
| 56 | + batch_op.drop_constraint('fk_run_tag_run_id_run', type_='foreignkey') |
| 57 | + batch_op.drop_constraint('fk_run_tag_tag_id_tag', type_='foreignkey') |
| 58 | + |
| 59 | + # Then create a new one with CASCADE |
| 60 | + batch_op.create_foreign_key('fk_run_tag_run_id_run', 'run', ['run_id'], ['id'],) |
| 61 | + batch_op.create_foreign_key('fk_run_tag_tag_id_tag', 'tag', ['tag_id'], ['id'],) |
| 62 | + |
| 63 | + with op.batch_alter_table('note', naming_convention=naming_convention) as batch_op: |
| 64 | + # First drop the existing foreign key |
| 65 | + batch_op.drop_constraint('fk_note_run_id_run', type_='foreignkey') |
| 66 | + |
| 67 | + # Then create a new one with CASCADE |
| 68 | + batch_op.create_foreign_key('fk_note_run_id_run', 'run', ['run_id'], ['id'],) |
| 69 | + |
0 commit comments