|
| 1 | +"""Drop thing_id from NMA_Radionuclides |
| 2 | +
|
| 3 | +Revision ID: d9f1e2c3b4a5 |
| 4 | +Revises: 71a4c6b3d2e8 |
| 5 | +Create Date: 2026-02-04 15:32:00.000000 |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +from typing import Sequence, Union |
| 10 | + |
| 11 | +import sqlalchemy as sa |
| 12 | +from alembic import op |
| 13 | + |
| 14 | +# revision identifiers, used by Alembic. |
| 15 | +revision: str = "d9f1e2c3b4a5" |
| 16 | +down_revision: Union[str, Sequence[str], None] = "71a4c6b3d2e8" |
| 17 | +branch_labels: Union[str, Sequence[str], None] = None |
| 18 | +depends_on: Union[str, Sequence[str], None] = None |
| 19 | + |
| 20 | + |
| 21 | +def _drop_thing_id_fk_and_indexes(inspector) -> None: |
| 22 | + fks = inspector.get_foreign_keys("NMA_Radionuclides") |
| 23 | + for fk in fks: |
| 24 | + if "thing_id" in (fk.get("constrained_columns") or []): |
| 25 | + op.drop_constraint(fk["name"], "NMA_Radionuclides", type_="foreignkey") |
| 26 | + |
| 27 | + indexes = inspector.get_indexes("NMA_Radionuclides") |
| 28 | + for idx in indexes: |
| 29 | + if "thing_id" in (idx.get("column_names") or []): |
| 30 | + op.drop_index(idx["name"], table_name="NMA_Radionuclides") |
| 31 | + |
| 32 | + |
| 33 | +def upgrade() -> None: |
| 34 | + """Upgrade schema.""" |
| 35 | + bind = op.get_bind() |
| 36 | + inspector = sa.inspect(bind) |
| 37 | + columns = [col["name"] for col in inspector.get_columns("NMA_Radionuclides")] |
| 38 | + if "thing_id" in columns: |
| 39 | + _drop_thing_id_fk_and_indexes(inspector) |
| 40 | + op.drop_column("NMA_Radionuclides", "thing_id") |
| 41 | + |
| 42 | + |
| 43 | +def downgrade() -> None: |
| 44 | + """Downgrade schema.""" |
| 45 | + bind = op.get_bind() |
| 46 | + inspector = sa.inspect(bind) |
| 47 | + columns = [col["name"] for col in inspector.get_columns("NMA_Radionuclides")] |
| 48 | + if "thing_id" not in columns: |
| 49 | + op.add_column( |
| 50 | + "NMA_Radionuclides", |
| 51 | + sa.Column("thing_id", sa.Integer(), nullable=True), |
| 52 | + ) |
| 53 | + op.create_foreign_key( |
| 54 | + "fk_nma_radionuclides_thing_id", |
| 55 | + "NMA_Radionuclides", |
| 56 | + "thing", |
| 57 | + ["thing_id"], |
| 58 | + ["id"], |
| 59 | + ondelete="CASCADE", |
| 60 | + ) |
0 commit comments