Skip to content

Commit

Permalink
chore(api) : Add communes migration
Browse files Browse the repository at this point in the history
  • Loading branch information
vperron committed Aug 30, 2024
1 parent 07ff7d6 commit 25c89c1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Modified api__communes
Revision ID: e3f3dfa4ad01
Revises: 517603187775
Create Date: 2024-08-30 17:58:54.747630
"""

import geoalchemy2
import sqlalchemy as sa
from alembic import op

from data_inclusion.api.core.db import SortedTextArray

# revision identifiers, used by Alembic.
revision = "e3f3dfa4ad01"
down_revision = "517603187775"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.add_column(
"api__communes",
sa.Column(
"codes_postaux",
SortedTextArray(sa.Text()),
nullable=True,
),
)
op.add_column(
"api__communes",
sa.Column(
"centre",
geoalchemy2.types.Geometry(
srid=4326, from_text="ST_GeomFromEWKT", name="geometry"
),
nullable=True,
),
)
op.drop_index("ix_api__communes__geography", table_name="api__communes")
op.drop_column("api__communes", "geom")


def downgrade() -> None:
op.add_column(
"api__communes",
sa.Column(
"geom",
geoalchemy2.types.Geometry(
srid=4326,
spatial_index=False,
from_text="ST_GeomFromEWKT",
name="geometry",
_spatial_index_reflected=True,
),
autoincrement=False,
nullable=True,
),
)
op.drop_index(
"idx_api__communes_centre", table_name="api__communes", postgresql_using="gist"
)
op.create_index(
"ix_api__communes__geography",
"api__communes",
[
sa.text(
"(st_simplify(geom, 0.01::double precision)::geography(Geometry,4326))"
)
],
unique=False,
)
op.drop_column("api__communes", "centre")
op.drop_column("api__communes", "codes_postaux")
10 changes: 6 additions & 4 deletions api/src/data_inclusion/api/inclusion_data/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class Commune(Base):
departement: Mapped[str]
region: Mapped[str]
siren_epci: Mapped[str]
centre = mapped_column(
geoalchemy2.Geometry("Geometry", srid=4326, spatial_index=True)
)
codes_postaux: Mapped[list[str]]
# FIXME(vperron) : This column should have an index (spatial_index=True)
# but let's do it in a future migration
centre = mapped_column(geoalchemy2.Geometry("Geometry", srid=4326))
# FIXME(vperron) : This should not be nullable but at the time of migration
# it's necessary as the info is not there yet. Let's clean up later.
codes_postaux: Mapped[list[str] | None]

def __repr__(self) -> str:
return f"<Commune(code={self.code}, nom={self.nom})>"
Expand Down

0 comments on commit 25c89c1

Please sign in to comment.