Skip to content

Commit

Permalink
Update lifecycle status of regulations after plan update
Browse files Browse the repository at this point in the history
  • Loading branch information
LKajan committed Nov 28, 2024
1 parent 919d7df commit 85797c9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""triggers, update regulation lifecycle after plan
Revision ID: 4e3c0868ea98
Revises: 385ba899bb2e
Create Date: 2024-11-27 14:50:27.178556
"""
from typing import Sequence, Union

import geoalchemy2
import sqlalchemy as sa
from alembic import op
from alembic_utils.pg_function import PGFunction
from sqlalchemy import text as sql_text

# revision identifiers, used by Alembic.
revision: str = "4e3c0868ea98"
down_revision: Union[str, None] = "385ba899bb2e"
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! ###
hame_trgfunc_plan_plan_regulation_update_lifecycle_status = PGFunction(
schema="hame",
signature="trgfunc_plan_plan_regulation_update_lifecycle_status()",
definition="RETURNS TRIGGER AS $$\n BEGIN\n UPDATE hame.plan_regulation rt\n SET lifecycle_status_id = NEW.lifecycle_status_id\n WHERE\n EXISTS (\n SELECT 1\n FROM hame.plan_regulation_group prg\n WHERE\n prg.id = rt.plan_regulation_group_id\n AND prg.plan_id = NEW.id\n )\n AND lifecycle_status_id = OLD.lifecycle_status_id\n ;\n RETURN NEW;\n END;\n $$ language 'plpgsql'",
)
op.replace_entity(hame_trgfunc_plan_plan_regulation_update_lifecycle_status)

hame_trgfunc_plan_plan_proposition_update_lifecycle_status = PGFunction(
schema="hame",
signature="trgfunc_plan_plan_proposition_update_lifecycle_status()",
definition="RETURNS TRIGGER AS $$\n BEGIN\n UPDATE hame.plan_proposition rt\n SET lifecycle_status_id = NEW.lifecycle_status_id\n WHERE\n EXISTS (\n SELECT 1\n FROM hame.plan_regulation_group prg\n WHERE\n prg.id = rt.plan_regulation_group_id\n AND prg.plan_id = NEW.id\n )\n AND lifecycle_status_id = OLD.lifecycle_status_id\n ;\n RETURN NEW;\n END;\n $$ language 'plpgsql'",
)
op.replace_entity(hame_trgfunc_plan_plan_proposition_update_lifecycle_status)

# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
hame_trgfunc_plan_plan_proposition_update_lifecycle_status = PGFunction(
schema="hame",
signature="trgfunc_plan_plan_proposition_update_lifecycle_status()",
definition="returns trigger\n LANGUAGE plpgsql\nAS $function$\n BEGIN\n UPDATE hame.plan_proposition\n SET lifecycle_status_id = NEW.lifecycle_status_id\n WHERE\n lifecycle_status_id = OLD.lifecycle_status_id\n AND EXISTS ( -- Only update in case of general regulation group\n SELECT 1\n FROM\n hame.plan_regulation_group prg\n JOIN codes.type_of_plan_regulation_group tprg\n ON prg.type_of_plan_regulation_group_id = tprg.id\n WHERE\n prg.plan_id = NEW.id\n AND prg.id = plan_regulation_group_id\n AND tprg.value = 'generalRegulations'\n );\n RETURN NEW;\n END;\n $function$",
)
op.replace_entity(hame_trgfunc_plan_plan_proposition_update_lifecycle_status)
hame_trgfunc_plan_plan_regulation_update_lifecycle_status = PGFunction(
schema="hame",
signature="trgfunc_plan_plan_regulation_update_lifecycle_status()",
definition="returns trigger\n LANGUAGE plpgsql\nAS $function$\n BEGIN\n UPDATE hame.plan_regulation\n SET lifecycle_status_id = NEW.lifecycle_status_id\n WHERE\n lifecycle_status_id = OLD.lifecycle_status_id\n AND EXISTS ( -- Only update in case of general regulation group\n SELECT 1\n FROM\n hame.plan_regulation_group prg\n JOIN codes.type_of_plan_regulation_group tprg\n ON prg.type_of_plan_regulation_group_id = tprg.id\n WHERE\n prg.plan_id = NEW.id\n AND prg.id = plan_regulation_group_id\n AND tprg.value = 'generalRegulations'\n );\n RETURN NEW;\n END;\n $function$",
)
op.replace_entity(hame_trgfunc_plan_plan_regulation_update_lifecycle_status)
# ### end Alembic commands ###
22 changes: 9 additions & 13 deletions database/triggers.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,24 @@ def generate_update_lifecycle_status_triggers():
)
trgs.append(trg)

# Finally, we want to update regulations belonging to a general regulation
# groups as well:
# Update lifecycle status of regulations after a lifecycle status change of a plan
for regulation_table in plan_regulation_tables:
trgfunc_signature = f"trgfunc_plan_{regulation_table}_update_lifecycle_status()"
trgfunc_definition = f"""
RETURNS TRIGGER AS $$
BEGIN
UPDATE hame.{regulation_table}
UPDATE hame.{regulation_table} rt
SET lifecycle_status_id = NEW.lifecycle_status_id
WHERE
lifecycle_status_id = OLD.lifecycle_status_id
AND EXISTS ( -- Only update in case of general regulation group
EXISTS (
SELECT 1
FROM
hame.plan_regulation_group prg
JOIN codes.type_of_plan_regulation_group tprg
ON prg.type_of_plan_regulation_group_id = tprg.id
FROM hame.plan_regulation_group prg
WHERE
prg.plan_id = NEW.id
AND prg.id = plan_regulation_group_id
AND tprg.value = 'generalRegulations'
);
prg.id = rt.plan_regulation_group_id
AND prg.plan_id = NEW.id
)
AND lifecycle_status_id = OLD.lifecycle_status_id
;
RETURN NEW;
END;
$$ language 'plpgsql'
Expand Down

0 comments on commit 85797c9

Please sign in to comment.