From 25c89c10c3185fff2c85321e9c63a5374b4ab151 Mon Sep 17 00:00:00 2001 From: Victor Perron Date: Fri, 30 Aug 2024 18:05:28 +0200 Subject: [PATCH] chore(api) : Add communes migration --- ...854_e3f3dfa4ad01_modified_api__communes.py | 75 +++++++++++++++++++ .../api/inclusion_data/models.py | 10 ++- 2 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 api/src/alembic/versions/20240830_175854_e3f3dfa4ad01_modified_api__communes.py diff --git a/api/src/alembic/versions/20240830_175854_e3f3dfa4ad01_modified_api__communes.py b/api/src/alembic/versions/20240830_175854_e3f3dfa4ad01_modified_api__communes.py new file mode 100644 index 00000000..65da281b --- /dev/null +++ b/api/src/alembic/versions/20240830_175854_e3f3dfa4ad01_modified_api__communes.py @@ -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") diff --git a/api/src/data_inclusion/api/inclusion_data/models.py b/api/src/data_inclusion/api/inclusion_data/models.py index ee8f1133..a41dedc4 100644 --- a/api/src/data_inclusion/api/inclusion_data/models.py +++ b/api/src/data_inclusion/api/inclusion_data/models.py @@ -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""