Skip to content

Commit

Permalink
add alembic migration
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaupetit committed Jan 16, 2025
1 parent 4cf07ae commit a9602f0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Add statique materialized view
Revision ID: 4b99d15436b0
Revises: d3d2c20f8efd
Create Date: 2025-01-16 15:02:04.004411
"""

from typing import Sequence, Union

from alembic import op
from sqlalchemy_utils.view import CreateView, DropView

from qualicharge.schemas.core import _StatiqueMV

# revision identifiers, used by Alembic.
revision: str = "4b99d15436b0"
down_revision: Union[str, None] = "d3d2c20f8efd"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
"""Create the Statique Materialized view and related indexes."""
op.execute(
CreateView(
_StatiqueMV.__table__.fullname, _StatiqueMV.selectable, materialized=True
)
)
for idx in _StatiqueMV.__table__.indexes:
idx.create(op.get_bind())


def downgrade() -> None:
"""Delete the Statique Materialized View."""

op.execute(
DropView(_StatiqueMV.__table__.fullname, materialized=True, cascade=True)
)
16 changes: 10 additions & 6 deletions src/api/qualicharge/schemas/core.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""QualiCharge core statique and dynamique schemas."""

from enum import IntEnum
from typing import TYPE_CHECKING, List, Optional, Union, cast
from typing import TYPE_CHECKING, ClassVar, List, Optional, Union, cast
from uuid import UUID, uuid4

from geoalchemy2.functions import ST_AsGeoJSON
Expand All @@ -20,7 +20,7 @@
)
from pydantic_extra_types.coordinate import Coordinate
from shapely.geometry import mapping
from sqlalchemy import event
from sqlalchemy import Select, event
from sqlalchemy.orm import registry
from sqlalchemy.schema import Column as SAColumn
from sqlalchemy.schema import Index
Expand Down Expand Up @@ -415,9 +415,8 @@ class _StatiqueMV(SQLModel):
NOTE: This is an internal model used **ONLY** for creating the materialized view.
"""

__table__ = create_materialized_view(
name=STATIQUE_MV_TABLE_NAME,
selectable=select(
selectable: ClassVar[Select] = (
select(
# FIXME
# Should be the Enum value
PointDeCharge.accessibilite_pmr,
Expand Down Expand Up @@ -473,7 +472,12 @@ class _StatiqueMV(SQLModel):
.join(Amenageur, Station.amenageur_id == Amenageur.id)
.join(Operateur, Station.operateur_id == Operateur.id)
.join(Enseigne, Station.enseigne_id == Enseigne.id)
.join(Localisation, Station.localisation_id == Localisation.id),
.join(Localisation, Station.localisation_id == Localisation.id)
)

__table__ = create_materialized_view(
name=STATIQUE_MV_TABLE_NAME,
selectable=selectable,
metadata=SQLModel.metadata,
indexes=[
Index("idx_statique_id_pdc_itinerance", "id_pdc_itinerance", unique=True),
Expand Down

0 comments on commit a9602f0

Please sign in to comment.