Skip to content

Commit

Permalink
WIP add MV refresh command
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaupetit committed Jan 16, 2025
1 parent 9140e2b commit 4cf07ae
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/api/qualicharge/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from rich.table import Table
from sqlalchemy import Column as SAColumn
from sqlalchemy.exc import IntegrityError, OperationalError, ProgrammingError
from sqlalchemy_utils import refresh_materialized_view
from sqlmodel import Session as SMSession
from sqlmodel import select

Expand All @@ -23,7 +24,7 @@
from .db import get_session
from .exceptions import IntegrityError as QCIntegrityError
from .fixtures.operational_units import prefixes
from .schemas.core import OperationalUnit
from .schemas.core import OperationalUnit, STATIQUE_MV_TABLE_NAME
from .schemas.sql import StatiqueImporter

logging.basicConfig(
Expand Down Expand Up @@ -456,6 +457,18 @@ def import_static(ctx: typer.Context, input_file: Path):
console.log("Saved (or updated) all entries successfully.")


@app.command()
def refresh_static(ctx: typer.Context, concurrently: bool = False):
"""Refresh the Statique materialized view."""
session: SMSession = ctx.obj

# Refresh the database
console.log("Refreshing database…")
refresh_materialized_view(
session, STATIQUE_MV_TABLE_NAME, concurrently=concurrently
)


@app.callback()
def main(ctx: typer.Context):
"""Attach database session to the context object."""
Expand Down
8 changes: 5 additions & 3 deletions src/api/qualicharge/schemas/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
)
from pydantic_extra_types.coordinate import Coordinate
from shapely.geometry import mapping
from sqlalchemy import cast as SA_cast
from sqlalchemy import event
from sqlalchemy.orm import registry
from sqlalchemy.schema import Column as SAColumn
Expand Down Expand Up @@ -59,6 +58,9 @@ class OperationalUnitTypeEnum(IntEnum):
MOBILITY = 2


STATIQUE_MV_TABLE_NAME: str = "statique"


class Amenageur(BaseTimestampedSQLModel, table=True):
"""Amenageur table."""

Expand Down Expand Up @@ -402,7 +404,7 @@ def id_pdc_itinerance(self) -> str:
class StatiqueMV(Statique, SQLModel):
"""Statique Materialized View."""

__tablename__ = "statique"
__tablename__ = STATIQUE_MV_TABLE_NAME

model_config = SQLModel.model_config

Expand All @@ -414,7 +416,7 @@ class _StatiqueMV(SQLModel):
"""

__table__ = create_materialized_view(
name="statique",
name=STATIQUE_MV_TABLE_NAME,
selectable=select(
# FIXME
# Should be the Enum value
Expand Down

0 comments on commit 4cf07ae

Please sign in to comment.