diff --git a/alembic/versions/fd73601a9075_add_delete_cascade_studies.py b/alembic/versions/fd73601a9075_add_delete_cascade_studies.py index ac063fe516..3f9f42c684 100644 --- a/alembic/versions/fd73601a9075_add_delete_cascade_studies.py +++ b/alembic/versions/fd73601a9075_add_delete_cascade_studies.py @@ -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 @@ -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") @@ -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") @@ -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)