Skip to content

Commit

Permalink
Merge pull request #11 from GispoCoding/6-add-other-tables-to-postgis
Browse files Browse the repository at this point in the history
Add plan regulation group table
  • Loading branch information
Rikuoja authored Feb 8, 2024
2 parents 3c47c21 + c5f660e commit 7e83b1a
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 3 deletions.
2 changes: 1 addition & 1 deletion database/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class VersionedBase(Base):
"""

__abstract__ = True
__table_args__ = {"schema": "hame"}

id: Mapped[uuid_pk]
created_at: Mapped[timestamp]
Expand Down Expand Up @@ -105,7 +106,6 @@ class PlanBase(VersionedBase):
"""

__abstract__ = True
__table_args__ = {"schema": "hame"}

exported_at: Mapped[Optional[datetime]]
valid_from: Mapped[Optional[datetime]]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""add_plan_regulation_group_table
Revision ID: c882368ce708
Revises: 84c37bffeca0
Create Date: 2024-02-05 18:07:16.739686
"""
from typing import Sequence, Union

# import geoalchemy2
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "c882368ce708"
down_revision: Union[str, None] = "84c37bffeca0"
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.create_table(
"plan_regulation_group",
sa.Column("short_name", sa.String(), nullable=False),
sa.Column(
"name",
postgresql.JSONB(astext_type=sa.Text()),
server_default='{"fin": "", "swe": "", "eng": ""}',
nullable=False,
),
sa.Column(
"id", sa.UUID(), server_default=sa.text("gen_random_uuid()"), nullable=False
),
sa.Column(
"created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
),
sa.Column(
"modified_at",
sa.DateTime(),
server_default=sa.text("now()"),
nullable=False,
),
sa.PrimaryKeyConstraint("id"),
schema="hame",
)
op.create_index(
op.f("ix_hame_plan_regulation_group_short_name"),
"plan_regulation_group",
["short_name"],
unique=True,
schema="hame",
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
op.f("ix_hame_plan_regulation_group_short_name"),
table_name="plan_regulation_group",
schema="hame",
)
op.drop_table("plan_regulation_group", schema="hame")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Edit plan regulation group table
Revision ID: 811b23178cef
Revises: c882368ce708
Create Date: 2024-02-08 14:23:00.747678
"""
from typing import Sequence, Union

# import geoalchemy2
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "811b23178cef"
down_revision: Union[str, None] = "c882368ce708"
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.add_column(
"plan_regulation_group",
sa.Column("exported_at", sa.DateTime(), nullable=True),
schema="hame",
)
op.add_column(
"plan_regulation_group",
sa.Column("valid_from", sa.DateTime(), nullable=True),
schema="hame",
)
op.add_column(
"plan_regulation_group",
sa.Column("valid_to", sa.DateTime(), nullable=True),
schema="hame",
)
op.add_column(
"plan_regulation_group",
sa.Column("repealed_at", sa.DateTime(), nullable=True),
schema="hame",
)
op.add_column(
"plan_regulation_group",
sa.Column("lifecycle_status_id", sa.UUID(), nullable=False),
schema="hame",
)
op.create_foreign_key(
"plan_lifecycle_status_id_fkey",
"plan_regulation_group",
"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",
"plan_regulation_group",
schema="hame",
type_="foreignkey",
)
op.drop_column("plan_regulation_group", "lifecycle_status_id", schema="hame")
op.drop_column("plan_regulation_group", "repealed_at", schema="hame")
op.drop_column("plan_regulation_group", "valid_to", schema="hame")
op.drop_column("plan_regulation_group", "valid_from", schema="hame")
op.drop_column("plan_regulation_group", "exported_at", schema="hame")
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""edit_plan_regulation_group_base_class
Revision ID: 6ee06a6e634a
Revises: 811b23178cef
Create Date: 2024-02-08 16:13:11.875724
"""
from typing import Sequence, Union

# import geoalchemy2
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "6ee06a6e634a"
down_revision: Union[str, None] = "811b23178cef"
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",
"plan_regulation_group",
schema="hame",
type_="foreignkey",
)
op.drop_column("plan_regulation_group", "valid_to", schema="hame")
op.drop_column("plan_regulation_group", "valid_from", schema="hame")
op.drop_column("plan_regulation_group", "exported_at", schema="hame")
op.drop_column("plan_regulation_group", "lifecycle_status_id", schema="hame")
op.drop_column("plan_regulation_group", "repealed_at", schema="hame")
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"plan_regulation_group",
sa.Column(
"repealed_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
),
schema="hame",
)
op.add_column(
"plan_regulation_group",
sa.Column(
"lifecycle_status_id", sa.UUID(), autoincrement=False, nullable=False
),
schema="hame",
)
op.add_column(
"plan_regulation_group",
sa.Column(
"exported_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
),
schema="hame",
)
op.add_column(
"plan_regulation_group",
sa.Column(
"valid_from", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
),
schema="hame",
)
op.add_column(
"plan_regulation_group",
sa.Column(
"valid_to", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
),
schema="hame",
)
op.create_foreign_key(
"plan_lifecycle_status_id_fkey",
"plan_regulation_group",
"lifecycle_status",
["lifecycle_status_id"],
["id"],
source_schema="hame",
referent_schema="codes",
)
# ### end Alembic commands ###
13 changes: 12 additions & 1 deletion database/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from typing import Optional

from base import PlanBase, language_str
from base import PlanBase, VersionedBase, language_str, unique_str
from shapely.geometry import Polygon
from sqlalchemy.orm import Mapped

Expand All @@ -16,3 +16,14 @@ class Plan(PlanBase):
name: Mapped[language_str]
approved_at: Mapped[Optional[datetime]]
geom: Mapped[Polygon]


class PlanRegulationGroup(VersionedBase):
"""
Kaavamääräysryhmä
"""

__tablename__ = "plan_regulation_group"

short_name: Mapped[unique_str]
name: Mapped[language_str]
10 changes: 9 additions & 1 deletion database/test/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import psycopg2

hame_count: int = 1 # adjust me when adding tables
hame_count: int = 2 # adjust me when adding tables
codes_count: int = 8 # adjust me when adding tables
matview_count: int = 0 # adjust me when adding views

Expand Down Expand Up @@ -80,6 +80,14 @@ def assert_database_is_alright(
None,
f"CREATE INDEX idx_{table_name}_geom ON hame.{table_name} USING gist (geom)",
) in indexes
if ("short_name",) in columns:
assert (
"hame",
table_name,
f"ix_hame_{table_name}_short_name",
None,
f"CREATE UNIQUE INDEX ix_hame_{table_name}_short_name ON hame.{table_name} USING btree (short_name)",
) in indexes

# Check code tables
cur.execute("SELECT tablename, tableowner FROM pg_tables WHERE schemaname='codes';")
Expand Down

0 comments on commit 7e83b1a

Please sign in to comment.