-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add trigger to prevent overlapping land use areas
- Loading branch information
Showing
5 changed files
with
165 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...grations/versions/2024_11_27_1633-9c28ba93d9be_add_land_use_overlap_prevention_trigger.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""add land use overlap prevention trigger | ||
Revision ID: 9c28ba93d9be | ||
Revises: 84142a5b86b6 | ||
Create Date: 2024-11-27 16:33:53.394187 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
import geoalchemy2 | ||
import sqlalchemy as sa | ||
from alembic import op | ||
from alembic_utils.pg_function import PGFunction | ||
from alembic_utils.pg_trigger import PGTrigger | ||
from sqlalchemy import text as sql_text | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "9c28ba93d9be" | ||
down_revision: Union[str, None] = "84142a5b86b6" | ||
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_land_use_area_prevent_overlap = PGFunction( | ||
schema="hame", | ||
signature="trgfunc_land_use_area_prevent_overlap()", | ||
definition="RETURNS TRIGGER AS $$\n DECLARE\n overlapping_id UUID;\n BEGIN\n SELECT id INTO overlapping_id\n FROM hame.land_use_area\n WHERE\n plan_id = NEW.plan_id\n AND ST_Overlaps(geom, NEW.geom)\n ;\n IF overlapping_id IS NOT NULL THEN\n RAISE EXCEPTION 'Geometries overlap\\: % - %',\n NEW.id, overlapping_id\n USING HINT = 'Two land use areas cannot overlap';\n END IF;\n RETURN NEW;\n END;\n $$ language 'plpgsql'", | ||
) | ||
op.create_entity(hame_trgfunc_land_use_area_prevent_overlap) | ||
|
||
hame_land_use_area_trg_land_use_area_prevent_overlap = PGTrigger( | ||
schema="hame", | ||
signature="trg_land_use_area_prevent_overlap", | ||
on_entity="hame.land_use_area", | ||
is_constraint=False, | ||
definition="BEFORE INSERT OR UPDATE ON land_use_area\n FOR EACH ROW\n EXECUTE FUNCTION hame.trgfunc_land_use_area_prevent_overlap()", | ||
) | ||
op.create_entity(hame_land_use_area_trg_land_use_area_prevent_overlap) | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
hame_land_use_area_trg_land_use_area_prevent_overlap = PGTrigger( | ||
schema="hame", | ||
signature="trg_land_use_area_prevent_overlap", | ||
on_entity="hame.land_use_area", | ||
is_constraint=False, | ||
definition="BEFORE INSERT OR UPDATE ON land_use_area\n FOR EACH ROW\n EXECUTE FUNCTION hame.trgfunc_land_use_area_prevent_overlap()", | ||
) | ||
op.drop_entity(hame_land_use_area_trg_land_use_area_prevent_overlap) | ||
|
||
hame_trgfunc_land_use_area_prevent_overlap = PGFunction( | ||
schema="hame", | ||
signature="trgfunc_land_use_area_prevent_overlap()", | ||
definition="RETURNS TRIGGER AS $$\n DECLARE\n overlapping_id UUID;\n BEGIN\n SELECT id INTO overlapping_id\n FROM hame.land_use_area\n WHERE\n plan_id = NEW.plan_id\n AND ST_Overlaps(geom, NEW.geom)\n ;\n IF overlapping_id IS NOT NULL THEN\n RAISE EXCEPTION 'Geometries overlap\\: % - %',\n NEW.id, overlapping_id\n USING HINT = 'Two land use areas cannot overlap';\n END IF;\n RETURN NEW;\n END;\n $$ language 'plpgsql'", | ||
) | ||
op.drop_entity(hame_trgfunc_land_use_area_prevent_overlap) | ||
|
||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters