Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redefine on delete cascades #450

Merged
merged 7 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions database/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ryhti_debug/
7 changes: 6 additions & 1 deletion database/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ version_path_separator = os # Use os.pathsep. Default configuration used for ne
# detail and examples

# format using "black" - use the console_scripts runner, against the "black" entrypoint
hooks = black
hooks = isort,black

isort.type = console_scripts
isort.entrypoint = isort
isort.options = REVISION_SCRIPT_FILENAME

black.type = console_scripts
black.entrypoint = black
black.options = -l 79 REVISION_SCRIPT_FILENAME
Expand Down
6 changes: 5 additions & 1 deletion database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ def lifecycle_status(cls) -> Mapped[VersionedBase]: # noqa
@declared_attr
def lifecycle_dates(cls): # noqa
return relationship(
"LifeCycleDate", back_populates=f"{cls.__tablename__}", lazy="joined"
"LifeCycleDate",
back_populates=f"{cls.__tablename__}",
lazy="joined",
cascade="all, delete-orphan",
passive_deletes=True,
)


Expand Down
3 changes: 2 additions & 1 deletion database/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class LifeCycleStatus(CodeBase):
code_list_uri = "http://uri.suomi.fi/codelist/rytj/kaavaelinkaari"

lifecycle_dates = relationship(
"LifeCycleDate", back_populates="lifecycle_status", cascade="delete"
"LifeCycleDate",
back_populates="lifecycle_status",
)
allowed_interaction_events: Mapped[List["TypeOfInteractionEvent"]] = relationship(
secondary="codes.allowed_events", back_populates="allowed_statuses"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""Remove cascade del on a code table

Revision ID: 08c795658444
Revises: b1f6c870e220
Create Date: 2025-02-19 09:00:29.688004

"""

from typing import Sequence, Union

import geoalchemy2
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "08c795658444"
down_revision: Union[str, None] = "b1f6c870e220"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(
"plan_lifecycle_status_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.create_foreign_key(
"plan_lifecycle_status_id_fkey",
"lifecycle_date",
"lifecycle_status",
["lifecycle_status_id"],
["id"],
source_schema="hame",
referent_schema="codes",
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(
"plan_lifecycle_status_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.create_foreign_key(
"plan_lifecycle_status_id_fkey",
"lifecycle_date",
"lifecycle_status",
["lifecycle_status_id"],
["id"],
source_schema="hame",
referent_schema="codes",
ondelete="CASCADE",
)
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
"""Cascade delete lifecycle_dates

Revision ID: a58453037fcd
Revises: 08c795658444
Create Date: 2025-02-19 09:03:43.553448

"""

from typing import Sequence, Union

import geoalchemy2
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "a58453037fcd"
down_revision: Union[str, None] = "08c795658444"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(
"land_use_area_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"land_use_point_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"other_area_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"plan_regulation_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"plan_id_fkey", "lifecycle_date", schema="hame", type_="foreignkey"
)
op.drop_constraint(
"plan_proposition_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"line_id_fkey", "lifecycle_date", schema="hame", type_="foreignkey"
)
op.drop_constraint(
"other_point_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.create_foreign_key(
"plan_id_fkey",
"lifecycle_date",
"plan",
["plan_id"],
["id"],
source_schema="hame",
referent_schema="hame",
ondelete="CASCADE",
)
op.create_foreign_key(
"land_use_point_id_fkey",
"lifecycle_date",
"land_use_point",
["land_use_point_id"],
["id"],
source_schema="hame",
referent_schema="hame",
ondelete="CASCADE",
)
op.create_foreign_key(
"plan_proposition_id_fkey",
"lifecycle_date",
"plan_proposition",
["plan_proposition_id"],
["id"],
source_schema="hame",
referent_schema="hame",
ondelete="CASCADE",
)
op.create_foreign_key(
"line_id_fkey",
"lifecycle_date",
"line",
["line_id"],
["id"],
source_schema="hame",
referent_schema="hame",
ondelete="CASCADE",
)
op.create_foreign_key(
"plan_regulation_id_fkey",
"lifecycle_date",
"plan_regulation",
["plan_regulation_id"],
["id"],
source_schema="hame",
referent_schema="hame",
ondelete="CASCADE",
)
op.create_foreign_key(
"other_point_id_fkey",
"lifecycle_date",
"other_point",
["other_point_id"],
["id"],
source_schema="hame",
referent_schema="hame",
ondelete="CASCADE",
)
op.create_foreign_key(
"other_area_id_fkey",
"lifecycle_date",
"other_area",
["other_area_id"],
["id"],
source_schema="hame",
referent_schema="hame",
ondelete="CASCADE",
)
op.create_foreign_key(
"land_use_area_id_fkey",
"lifecycle_date",
"land_use_area",
["land_use_area_id"],
["id"],
source_schema="hame",
referent_schema="hame",
ondelete="CASCADE",
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(
"land_use_area_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"other_area_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"other_point_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"plan_regulation_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"line_id_fkey", "lifecycle_date", schema="hame", type_="foreignkey"
)
op.drop_constraint(
"plan_proposition_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"land_use_point_id_fkey",
"lifecycle_date",
schema="hame",
type_="foreignkey",
)
op.drop_constraint(
"plan_id_fkey", "lifecycle_date", schema="hame", type_="foreignkey"
)
op.create_foreign_key(
"other_point_id_fkey",
"lifecycle_date",
"other_point",
["other_point_id"],
["id"],
source_schema="hame",
referent_schema="hame",
)
op.create_foreign_key(
"line_id_fkey",
"lifecycle_date",
"line",
["line_id"],
["id"],
source_schema="hame",
referent_schema="hame",
)
op.create_foreign_key(
"plan_proposition_id_fkey",
"lifecycle_date",
"plan_proposition",
["plan_proposition_id"],
["id"],
source_schema="hame",
referent_schema="hame",
)
op.create_foreign_key(
"plan_id_fkey",
"lifecycle_date",
"plan",
["plan_id"],
["id"],
source_schema="hame",
referent_schema="hame",
)
op.create_foreign_key(
"plan_regulation_id_fkey",
"lifecycle_date",
"plan_regulation",
["plan_regulation_id"],
["id"],
source_schema="hame",
referent_schema="hame",
)
op.create_foreign_key(
"other_area_id_fkey",
"lifecycle_date",
"other_area",
["other_area_id"],
["id"],
source_schema="hame",
referent_schema="hame",
)
op.create_foreign_key(
"land_use_point_id_fkey",
"lifecycle_date",
"land_use_point",
["land_use_point_id"],
["id"],
source_schema="hame",
referent_schema="hame",
)
op.create_foreign_key(
"land_use_area_id_fkey",
"lifecycle_date",
"land_use_area",
["land_use_area_id"],
["id"],
source_schema="hame",
referent_schema="hame",
)
# ### end Alembic commands ###
Loading