|
| 1 | +"""add cascade delete to measurements.sensorid foreign key |
| 2 | +
|
| 3 | +Revision ID: 778a9dbdeb5e |
| 4 | +Revises: add_unique_constraint_to_values |
| 5 | +Create Date: 2025-08-02 15:47:56.175249 |
| 6 | +
|
| 7 | +""" |
| 8 | +from typing import Sequence, Union |
| 9 | + |
| 10 | +from alembic import op |
| 11 | +import sqlalchemy as sa |
| 12 | + |
| 13 | + |
| 14 | +# revision identifiers, used by Alembic. |
| 15 | +revision: str = '778a9dbdeb5e' |
| 16 | +down_revision: Union[str, None] = 'add_unique_constraint_to_values' |
| 17 | +branch_labels: Union[str, Sequence[str], None] = None |
| 18 | +depends_on: Union[str, Sequence[str], None] = None |
| 19 | + |
| 20 | + |
| 21 | +def upgrade() -> None: |
| 22 | + """Upgrade schema.""" |
| 23 | + # Drop the existing foreign key constraint |
| 24 | + op.drop_constraint('measurements_sensorid_fkey', 'measurements', type_='foreignkey') |
| 25 | + |
| 26 | + # Add the new foreign key constraint with CASCADE delete |
| 27 | + op.create_foreign_key( |
| 28 | + 'measurements_sensorid_fkey', |
| 29 | + 'measurements', |
| 30 | + 'sensors', |
| 31 | + ['sensorid'], |
| 32 | + ['sensorid'], |
| 33 | + ondelete='CASCADE' |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def downgrade() -> None: |
| 38 | + """Downgrade schema.""" |
| 39 | + # Drop the CASCADE foreign key constraint |
| 40 | + op.drop_constraint('measurements_sensorid_fkey', 'measurements', type_='foreignkey') |
| 41 | + |
| 42 | + # Recreate the original foreign key constraint without CASCADE |
| 43 | + op.create_foreign_key( |
| 44 | + 'measurements_sensorid_fkey', |
| 45 | + 'measurements', |
| 46 | + 'sensors', |
| 47 | + ['sensorid'], |
| 48 | + ['sensorid'] |
| 49 | + ) |
0 commit comments