Skip to content

Commit

Permalink
🗃️(api) make location address/coordinates unique together
Browse files Browse the repository at this point in the history
Instead of having a unique `Localisation.adresse_station` field, let's
be less pedantic and allow address to be unique while combined with
specific coordinates. This allows to define a sparse address for a
station with precise coordinates. Two stations located in the same area
with a partial address will have two separated Localisation entries when
their coordinates differ.
  • Loading branch information
jmaupetit committed Jan 9, 2025
1 parent 9745e22 commit 9a29c19
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ and this project adheres to
command
- Decrease the number of database queries for dynamic endpoints
- Cache the "get PointDeCharge id from its `id_pdc_itinerance`" database query
- Updated `Localisation` table constraint: `adresse_station` is no longer
unique, but the `adresse_station`/`coordonneesXY` couple should be
- Upgrade pydantic to `2.10.4`
- Upgrade pydantic-settings to `2.7.1`
- Upgrade python-multipart to `0.0.20`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Update localisation uniqueness criterions
Revision ID: 32b3d0e269a2
Revises: c09664a85912
Create Date: 2025-01-09 16:20:52.578289
"""

from typing import Sequence, Union

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "32b3d0e269a2"
down_revision: Union[str, None] = "c09664a85912"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
op.drop_constraint(
"localisation_adresse_station_key", "localisation", type_="unique"
)
op.create_unique_constraint(
"localisation_adresse_station_coordonneesXY_key", "localisation", ["adresse_station", "coordonneesXY"]
)


def downgrade() -> None:
op.drop_constraint("localisation_adresse_station_coordonneesXY_key", "localisation", type_="unique")
op.create_unique_constraint(
"localisation_adresse_station_key", "localisation", ["adresse_station"]
)
6 changes: 5 additions & 1 deletion src/api/qualicharge/schemas/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,16 @@ def __eq__(self, other) -> bool:
class Localisation(BaseTimestampedSQLModel, table=True):
"""Localisation table."""

__table_args__ = BaseTimestampedSQLModel.__table_args__ + (
UniqueConstraint("adresse_station", "coordonneesXY"),
)

model_config = SQLModelConfig(
validate_assignment=True, arbitrary_types_allowed=True
)

id: UUID = Field(default_factory=uuid4, primary_key=True)
adresse_station: str = Field(unique=True)
adresse_station: str
code_insee_commune: str = Field(regex=r"^([013-9]\d|2[AB1-9])\d{3}$")
coordonneesXY: DataGouvCoordinate = Field(
sa_type=Geometry(
Expand Down
2 changes: 1 addition & 1 deletion src/api/qualicharge/schemas/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def save(self):
self._save_schema(
self.localisation,
Localisation,
constraint="localisation_adresse_station_key",
constraint="localisation_adresse_station_coordonneesXY_key",
)
self._save_schema(
self.station,
Expand Down

0 comments on commit 9a29c19

Please sign in to comment.