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

Add plan regulation table #13

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 5 additions & 2 deletions database/base.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import uuid
from datetime import datetime
from typing import Optional
from typing import Optional, Tuple

from geoalchemy2 import Geometry
from shapely.geometry import Polygon
from sqlalchemy import ForeignKey
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.dialects.postgresql import JSONB, NUMRANGE, UUID
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
Expand All @@ -28,6 +28,7 @@ class Base(DeclarativeBase):
uuid.UUID: UUID(as_uuid=True),
dict[str, str]: JSONB,
Polygon: Geometry(geometry_type="POLYGON", srid=PROJECT_SRID),
Tuple[float, float]: NUMRANGE,
}


Expand All @@ -43,6 +44,8 @@ class Base(DeclarativeBase):
]
timestamp = Annotated[datetime, mapped_column(server_default=func.now())]

autoincrement_int = Annotated[int, mapped_column(autoincrement=True)]
msorvoja marked this conversation as resolved.
Show resolved Hide resolved

metadata = Base.metadata


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""add_plan_regulation_table

Revision ID: 776ca8ea5a68
Revises: 6ee06a6e634a
Create Date: 2024-02-08 16:48:20.346350

"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

# import geoalchemy2
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "776ca8ea5a68"
down_revision: Union[str, None] = "6ee06a6e634a"
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",
sa.Column("plan_regulation_group_id", sa.UUID(), nullable=False),
sa.Column("type_of_plan_regulation_id", sa.UUID(), nullable=False),
sa.Column("type_of_verbal_plan_regulation_id", sa.UUID(), nullable=False),
sa.Column("numeric_range", postgresql.NUMRANGE(), nullable=False),
sa.Column("unit", sa.String(), nullable=False),
sa.Column(
"text_value",
postgresql.JSONB(astext_type=sa.Text()),
server_default='{"fin": "", "swe": "", "eng": ""}',
nullable=False,
),
sa.Column("numeric_value", sa.Float(), nullable=False),
sa.Column(
"regulation_number", sa.Integer(), autoincrement=True, nullable=False
),
sa.Column("exported_at", sa.DateTime(), nullable=True),
sa.Column("valid_from", sa.DateTime(), nullable=True),
sa.Column("valid_to", sa.DateTime(), nullable=True),
sa.Column("repealed_at", sa.DateTime(), nullable=True),
sa.Column("lifecycle_status_id", sa.UUID(), 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.ForeignKeyConstraint(
["lifecycle_status_id"],
["codes.lifecycle_status.id"],
name="plan_lifecycle_status_id_fkey",
),
sa.ForeignKeyConstraint(
["plan_regulation_group_id"],
["hame.plan_regulation_group.id"],
name="plan_regulation_group_id_fkey",
),
sa.ForeignKeyConstraint(
["type_of_plan_regulation_id"],
["codes.type_of_plan_regulation.id"],
name="type_of_plan_regulation_id_fkey",
),
sa.ForeignKeyConstraint(
["type_of_verbal_plan_regulation_id"],
["codes.type_of_verbal_plan_regulation.id"],
name="type_of_verbal_plan_regulation_id_fkey",
),
sa.PrimaryKeyConstraint("id"),
schema="hame",
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table("plan_regulation", schema="hame")
# ### end Alembic commands ###
59 changes: 56 additions & 3 deletions database/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import uuid
from datetime import datetime
from typing import Optional
from typing import Optional, Tuple

from base import PlanBase, VersionedBase, language_str, unique_str
from base import PlanBase, VersionedBase, autoincrement_int, language_str, unique_str
from shapely.geometry import Polygon
from sqlalchemy.orm import Mapped
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship


class Plan(PlanBase):
Expand All @@ -27,3 +29,54 @@ class PlanRegulationGroup(VersionedBase):

short_name: Mapped[unique_str]
name: Mapped[language_str]
# värikoodi?
# group_type: oma koodilista


class PlanRegulation(PlanBase):
"""
Kaavamääräys
"""

__tablename__ = "plan_regulation"

plan_regulation_group_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey(
"hame.plan_regulation_group.id", name="plan_regulation_group_id_fkey"
)
)

type_of_plan_regulation_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey(
"codes.type_of_plan_regulation.id", name="type_of_plan_regulation_id_fkey"
)
)
type_of_verbal_plan_regulation_id: Mapped[uuid.UUID] = mapped_column(
msorvoja marked this conversation as resolved.
Show resolved Hide resolved
ForeignKey(
"codes.type_of_verbal_plan_regulation.id",
name="type_of_verbal_plan_regulation_id_fkey",
)
)
# type_of_additional_information_id: Mapped[uuid.UUID] = mapped_column(
# ForeignKey(
# "codes.type_of_additional_information.id",
# name="type_of_additional_information_id_fkey",
# )
# )

plan_regulation_group = relationship(
"PlanRegulationGroup", back_populates="plan_regulations"
)
type_of_plan_regulation = relationship(
"TypeOfPlanRegulation", back_populates="plan_regulations"
)
# plan_theme: kaavoitusteema-koodilista
type_of_verbal_plan_regulation = relationship(
"TypeOfVerbalPlanRegulation", back_populates="plan_regulations"
)
numeric_range: Mapped[Tuple[float, float]]
msorvoja marked this conversation as resolved.
Show resolved Hide resolved
unit: Mapped[str]
msorvoja marked this conversation as resolved.
Show resolved Hide resolved
text_value: Mapped[language_str]
numeric_value: Mapped[float]
msorvoja marked this conversation as resolved.
Show resolved Hide resolved
regulation_number: Mapped[autoincrement_int]
# ElinkaaritilaX_pvm?
2 changes: 1 addition & 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 = 2 # adjust me when adding tables
hame_count: int = 3 # 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
Loading