Skip to content

Commit

Permalink
perf(study-db): change migration script to add foreign key constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
laurent-laporte-pro committed Feb 14, 2024
1 parent 778a122 commit a9ca976
Showing 1 changed file with 49 additions and 34 deletions.
83 changes: 49 additions & 34 deletions alembic/versions/fd73601a9075_add_delete_cascade_studies.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,69 @@
"""Add delete cascade studies
"""
Add delete cascade constraint to study foreign keys
Revision ID: fd73601a9075
Revises: 3c70366b10ea
Create Date: 2024-02-12 17:27:37.314443
"""
from alembic import op

# revision identifiers, used by Alembic.
revision = 'fd73601a9075'
down_revision = '3c70366b10ea'
revision = "fd73601a9075"
down_revision = "3c70366b10ea"
branch_labels = None
depends_on = None

# noinspection SpellCheckingInspection
RAWSTUDY_FK = "rawstudy_id_fkey"

# noinspection SpellCheckingInspection
VARIANTSTUDY_FK = "variantstudy_id_fkey"

# noinspection SpellCheckingInspection
STUDY_ADDITIONAL_DATA_FK = "study_additional_data_study_id_fkey"


def upgrade() -> None:
dialect_name: str = op.get_context().dialect.name
if dialect_name == "postgresql":
with op.batch_alter_table("rawstudy", schema=None) as batch_op:
batch_op.drop_constraint(RAWSTUDY_FK, type_="foreignkey")
batch_op.create_foreign_key(RAWSTUDY_FK, "study", ["id"], ["id"], ondelete="CASCADE")

with op.batch_alter_table("study_additional_data", schema=None) as batch_op:
batch_op.drop_constraint(STUDY_ADDITIONAL_DATA_FK, type_="foreignkey")
batch_op.create_foreign_key(STUDY_ADDITIONAL_DATA_FK, "study", ["study_id"], ["id"], ondelete="CASCADE")

def upgrade():
# connexion: Connection = op.get_bind()
# connexion.execute("DROP TABLE IF EXISTS _alembic_tmp_rawstudy")
print(op.get_context().dialect.name)
if op.get_context().dialect.name == 'postgresql':
print(op.get_context().dialect.name)
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('rawstudy', schema=None) as batch_op:
batch_op.drop_constraint('rawstudy_id_fkey', type_='foreignkey')
batch_op.create_foreign_key('rawstudy_id_fkey', 'study', ['id'], ['id'], ondelete='CASCADE')
with op.batch_alter_table("variantstudy", schema=None) as batch_op:
batch_op.drop_constraint(VARIANTSTUDY_FK, type_="foreignkey")
batch_op.create_foreign_key(VARIANTSTUDY_FK, "study", ["id"], ["id"], ondelete="CASCADE")

with op.batch_alter_table('study_additional_data', schema=None) as batch_op:
batch_op.drop_constraint('study_additional_data_study_id_fkey', type_='foreignkey')
batch_op.create_foreign_key('study_additional_data_study_id_fkey', 'study', ['study_id'], ['id'], ondelete='CASCADE')
elif dialect_name == "sqlite":
# Adding ondelete="CASCADE" to a foreign key in sqlite is not supported
pass

with op.batch_alter_table('variantstudy', schema=None) as batch_op:
batch_op.drop_constraint('variantstudy_id_fkey', type_='foreignkey')
batch_op.create_foreign_key('variantstudy_id_fkey', 'study', ['id'], ['id'], ondelete='CASCADE')
else:
raise NotImplementedError(f"{dialect_name=} not implemented")

# ### end Alembic commands ###

def downgrade() -> None:
dialect_name: str = op.get_context().dialect.name
if dialect_name == "postgresql":
with op.batch_alter_table("rawstudy", schema=None) as batch_op:
batch_op.drop_constraint(RAWSTUDY_FK, type_="foreignkey")
batch_op.create_foreign_key(RAWSTUDY_FK, "study", ["id"], ["id"])

def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
if op.get_context().dialect.name == 'postgresql':
with op.batch_alter_table('variantstudy', schema=None) as batch_op:
batch_op.drop_constraint('rawstudy_id_fkey', type_='foreignkey')
batch_op.create_foreign_key('rawstudy_id_fkey', 'study', ['id'], ['id'])
with op.batch_alter_table("study_additional_data", schema=None) as batch_op:
batch_op.drop_constraint(STUDY_ADDITIONAL_DATA_FK, type_="foreignkey")
batch_op.create_foreign_key(STUDY_ADDITIONAL_DATA_FK, "study", ["study_id"], ["id"])

with op.batch_alter_table('study_additional_data', schema=None) as batch_op:
batch_op.drop_constraint('study_additional_data_study_id_fkey', type_='foreignkey')
batch_op.create_foreign_key('study_additional_data_study_id_fkey', 'study', ['study_id'], ['id'])
with op.batch_alter_table("variantstudy", schema=None) as batch_op:
batch_op.drop_constraint(VARIANTSTUDY_FK, type_="foreignkey")
batch_op.create_foreign_key(VARIANTSTUDY_FK, "study", ["id"], ["id"])

with op.batch_alter_table('rawstudy', schema=None) as batch_op:
batch_op.drop_constraint('variantstudy_id_fkey', type_='foreignkey')
batch_op.create_foreign_key('variantstudy_id_fkey', 'study', ['id'], ['id'])
elif dialect_name == "sqlite":
# Removing ondelete="CASCADE" to a foreign key in sqlite is not supported
pass

# ### end Alembic commands ###
else:
raise NotImplementedError(f"{dialect_name=} not implemented")

0 comments on commit a9ca976

Please sign in to comment.