Skip to content

Commit

Permalink
fix(db): add a migration script to correct the many-to-many relations…
Browse files Browse the repository at this point in the history
…hip between `Study` and `Group`
  • Loading branch information
laurent-laporte-pro committed Feb 16, 2024
1 parent 403087e commit 7c3c5cd
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions alembic/versions/fd73601a9075_add_delete_cascade_studies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
Revises: 3c70366b10ea
Create Date: 2024-02-12 17:27:37.314443
"""
import sqlalchemy as sa # type: ignore
from alembic import op

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

Expand All @@ -25,6 +26,8 @@

def upgrade() -> None:
dialect_name: str = op.get_context().dialect.name

# SQLite doesn't support dropping foreign keys, so we need to ignore it here
if dialect_name == "postgresql":
with op.batch_alter_table("rawstudy", schema=None) as batch_op:
batch_op.drop_constraint(RAWSTUDY_FK, type_="foreignkey")
Expand All @@ -38,16 +41,25 @@ def upgrade() -> None:
batch_op.drop_constraint(VARIANTSTUDY_FK, type_="foreignkey")
batch_op.create_foreign_key(VARIANTSTUDY_FK, "study", ["id"], ["id"], ondelete="CASCADE")

elif dialect_name == "sqlite":
# Adding ondelete="CASCADE" to a foreign key in sqlite is not supported
pass

else:
raise NotImplementedError(f"{dialect_name=} not implemented")
with op.batch_alter_table("group_metadata", schema=None) as batch_op:
batch_op.alter_column("group_id", existing_type=sa.VARCHAR(length=36), nullable=False)
batch_op.alter_column("study_id", existing_type=sa.VARCHAR(length=36), nullable=False)
batch_op.create_index(batch_op.f("ix_group_metadata_group_id"), ["group_id"], unique=False)
batch_op.create_index(batch_op.f("ix_group_metadata_study_id"), ["study_id"], unique=False)
if dialect_name == "postgresql":
batch_op.drop_constraint("group_metadata_group_id_fkey", type_="foreignkey")
batch_op.drop_constraint("group_metadata_study_id_fkey", type_="foreignkey")
batch_op.create_foreign_key(
"group_metadata_group_id_fkey", "groups", ["group_id"], ["id"], ondelete="CASCADE"
)
batch_op.create_foreign_key(
"group_metadata_study_id_fkey", "study", ["study_id"], ["id"], ondelete="CASCADE"
)


def downgrade() -> None:
dialect_name: str = op.get_context().dialect.name
# SQLite doesn't support dropping foreign keys, so we need to ignore it here
if dialect_name == "postgresql":
with op.batch_alter_table("rawstudy", schema=None) as batch_op:
batch_op.drop_constraint(RAWSTUDY_FK, type_="foreignkey")
Expand All @@ -61,9 +73,14 @@ def downgrade() -> None:
batch_op.drop_constraint(VARIANTSTUDY_FK, type_="foreignkey")
batch_op.create_foreign_key(VARIANTSTUDY_FK, "study", ["id"], ["id"])

elif dialect_name == "sqlite":
# Removing ondelete="CASCADE" to a foreign key in sqlite is not supported
pass

else:
raise NotImplementedError(f"{dialect_name=} not implemented")
with op.batch_alter_table("group_metadata", schema=None) as batch_op:
# SQLite doesn't support dropping foreign keys, so we need to ignore it here
if dialect_name == "postgresql":
batch_op.drop_constraint("group_metadata_study_id_fkey", type_="foreignkey")
batch_op.drop_constraint("group_metadata_group_id_fkey", type_="foreignkey")
batch_op.create_foreign_key("group_metadata_study_id_fkey", "study", ["study_id"], ["id"])
batch_op.create_foreign_key("group_metadata_group_id_fkey", "groups", ["group_id"], ["id"])
batch_op.drop_index(batch_op.f("ix_group_metadata_study_id"))
batch_op.drop_index(batch_op.f("ix_group_metadata_group_id"))
batch_op.alter_column("study_id", existing_type=sa.VARCHAR(length=36), nullable=True)
batch_op.alter_column("group_id", existing_type=sa.VARCHAR(length=36), nullable=True)

0 comments on commit 7c3c5cd

Please sign in to comment.