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

Index fixes #405

Merged
merged 3 commits into from
Dec 3, 2024
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
21 changes: 17 additions & 4 deletions database/base.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import uuid
from datetime import datetime
from typing import Dict, List, Optional
from typing import Any, Dict, List, Optional

from geoalchemy2 import Geometry
from shapely.geometry import MultiLineString, MultiPoint, MultiPolygon
from sqlalchemy import ForeignKey
from sqlalchemy import ForeignKey, Index
from sqlalchemy.dialects.postgresql import JSONB, NUMRANGE, UUID, Range
from sqlalchemy.orm import (
DeclarativeBase,
Expand Down Expand Up @@ -56,7 +56,7 @@ class VersionedBase(Base):
"""

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

# Go figure. We have to *explicitly state* id is a mapped column, because id will
# have to be defined inside all the subclasses for relationship remote_side
Expand Down Expand Up @@ -152,11 +152,24 @@ class PlanObjectBase(PlanBase):

__abstract__ = True

@declared_attr.directive
@classmethod
def __table_args__(cls):
return (
Index(
f"ix_{cls.__tablename__}_plan_id_ordering",
"plan_id",
"ordering",
unique=True,
),
PlanBase.__table_args__,
)

description: Mapped[language_str]
source_data_object: Mapped[str] = mapped_column(nullable=True)
height_range: Mapped[numeric_range]
height_unit: Mapped[str] = mapped_column(nullable=True)
ordering: Mapped[Optional[int]] = mapped_column(index=True)
ordering: Mapped[Optional[int]]
type_of_underground_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey("codes.type_of_underground.id", name="type_of_underground_id_fkey"),
index=True,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
"""ordering index fixes

Revision ID: 09f44e0e7110
Revises: 4e3c0868ea98
Create Date: 2024-12-02 15:22:07.740316

"""
from typing import Sequence, Union

import geoalchemy2
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "09f44e0e7110"
down_revision: Union[str, None] = "4e3c0868ea98"
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_index(
"ix_hame_land_use_area_ordering",
table_name="land_use_area",
schema="hame",
)
op.create_index(
"ix_land_use_area_plan_id_ordering",
"land_use_area",
["plan_id", "ordering"],
unique=True,
schema="hame",
)
op.drop_index(
"ix_hame_land_use_point_ordering",
table_name="land_use_point",
schema="hame",
)
op.create_index(
"ix_land_use_point_plan_id_ordering",
"land_use_point",
["plan_id", "ordering"],
unique=True,
schema="hame",
)
op.drop_index("ix_hame_line_ordering", table_name="line", schema="hame")
op.create_index(
"ix_line_plan_id_ordering",
"line",
["plan_id", "ordering"],
unique=True,
schema="hame",
)
op.drop_index("ix_hame_other_area_ordering", table_name="other_area", schema="hame")
op.create_index(
"ix_other_area_plan_id_ordering",
"other_area",
["plan_id", "ordering"],
unique=True,
schema="hame",
)
op.drop_index(
"ix_hame_other_point_ordering", table_name="other_point", schema="hame"
)
op.create_index(
"ix_other_point_plan_id_ordering",
"other_point",
["plan_id", "ordering"],
unique=True,
schema="hame",
)
op.drop_index(
"ix_hame_plan_proposition_ordering",
table_name="plan_proposition",
schema="hame",
)
op.create_index(
"ix_plan_proposition_plan_regulation_group_id_ordering",
"plan_proposition",
["plan_regulation_group_id", "ordering"],
unique=True,
schema="hame",
)
op.drop_index(
"ix_hame_plan_regulation_ordering",
table_name="plan_regulation",
schema="hame",
)
op.create_index(
"ix_plan_regulation_plan_regulation_group_id_ordering",
"plan_regulation",
["plan_regulation_group_id", "ordering"],
unique=True,
schema="hame",
)
op.drop_index(
"ix_hame_plan_regulation_group_ordering",
table_name="plan_regulation_group",
schema="hame",
)
op.create_index(
"ix_plan_regulation_group_plan_id_ordering",
"plan_regulation_group",
["plan_id", "ordering"],
unique=False,
schema="hame",
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
"ix_plan_regulation_group_plan_id_ordering",
table_name="plan_regulation_group",
schema="hame",
)
op.create_index(
"ix_hame_plan_regulation_group_ordering",
"plan_regulation_group",
["ordering"],
unique=False,
schema="hame",
)
op.drop_index(
"ix_plan_regulation_plan_regulation_group_id_ordering",
table_name="plan_regulation",
schema="hame",
)
op.create_index(
"ix_hame_plan_regulation_ordering",
"plan_regulation",
["ordering"],
unique=False,
schema="hame",
)
op.drop_index(
"ix_plan_proposition_plan_regulation_group_id_ordering",
table_name="plan_proposition",
schema="hame",
)
op.create_index(
"ix_hame_plan_proposition_ordering",
"plan_proposition",
["ordering"],
unique=False,
schema="hame",
)
op.drop_index(
"ix_other_point_plan_id_ordering",
table_name="other_point",
schema="hame",
)
op.create_index(
"ix_hame_other_point_ordering",
"other_point",
["ordering"],
unique=False,
schema="hame",
)
op.drop_index(
"ix_other_area_plan_id_ordering",
table_name="other_area",
schema="hame",
)
op.create_index(
"ix_hame_other_area_ordering",
"other_area",
["ordering"],
unique=False,
schema="hame",
)
op.drop_index("ix_line_plan_id_ordering", table_name="line", schema="hame")
op.create_index(
"ix_hame_line_ordering",
"line",
["ordering"],
unique=False,
schema="hame",
)
op.drop_index(
"ix_land_use_point_plan_id_ordering",
table_name="land_use_point",
schema="hame",
)
op.create_index(
"ix_hame_land_use_point_ordering",
"land_use_point",
["ordering"],
unique=False,
schema="hame",
)
op.drop_index(
"ix_land_use_area_plan_id_ordering",
table_name="land_use_area",
schema="hame",
)
op.create_index(
"ix_hame_land_use_area_ordering",
"land_use_area",
["ordering"],
unique=False,
schema="hame",
)
# ### end Alembic commands ###
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""short name uniqueness fix

Revision ID: e9a045c08304
Revises: 09f44e0e7110
Create Date: 2024-12-02 13:54:23.903091

"""
from typing import Sequence, Union

import geoalchemy2
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "e9a045c08304"
down_revision: Union[str, None] = "09f44e0e7110"
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.alter_column(
"plan_regulation_group",
"short_name",
existing_type=sa.VARCHAR(),
nullable=True,
schema="hame",
)
op.drop_index(
"ix_hame_plan_regulation_group_short_name",
table_name="plan_regulation_group",
schema="hame",
)
op.create_index(
"ix_plan_regulation_group_plan_id_short_name",
"plan_regulation_group",
["plan_id", "short_name"],
unique=True,
schema="hame",
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
"ix_plan_regulation_group_plan_id_short_name",
table_name="plan_regulation_group",
schema="hame",
)
op.create_index(
"ix_hame_plan_regulation_group_short_name",
"plan_regulation_group",
["short_name"],
unique=True,
schema="hame",
)
op.alter_column(
"plan_regulation_group",
"short_name",
existing_type=sa.VARCHAR(),
nullable=False,
schema="hame",
)
# ### end Alembic commands ###
Loading
Loading