-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Given latest assigned codes, we've updated 4 existing operational units and created 34 new ones.
- Loading branch information
Showing
4 changed files
with
720 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/api/qualicharge/migrations/versions/3eaef66b7629_ou_data_update.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
"""Update operational units data | ||
Revision ID: 3eaef66b7629 | ||
Revises: 37189f57d370 | ||
Create Date: 2025-02-06 15:37:03.934737 | ||
""" | ||
|
||
from datetime import datetime, timezone | ||
from typing import Sequence, Union | ||
from uuid import uuid4 | ||
|
||
from alembic import op | ||
from sqlalchemy import MetaData | ||
|
||
from qualicharge.fixtures.operational_units import data as operational_units | ||
from qualicharge.schemas.core import OperationalUnit | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "3eaef66b7629" | ||
down_revision: Union[str, None] = "37189f57d370" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
# Legacy names | ||
LEGACY = { | ||
"FRALL": "Allego Group", | ||
"FRECP": "EcoPhi", | ||
"FREVE": "EVE CAR PLUG", | ||
"FRORV": "OBORNES", | ||
} | ||
|
||
# New operational units | ||
NEW = { | ||
"FR0CU", | ||
"FR190", | ||
"FRALN", | ||
"FRBCF", | ||
"FRBEZ", | ||
"FRBFC", | ||
"FRBLR", | ||
"FRBPE", | ||
"FRCG1", | ||
"FRCVT", | ||
"FREKL", | ||
"FRELE", | ||
"FRELT", | ||
"FREMO", | ||
"FRENT", | ||
"FRERA", | ||
"FRFZD", | ||
"FRHDA", | ||
"FRHDT", | ||
"FRHOP", | ||
"FRJBA", | ||
"FRMBX", | ||
"FRMEI", | ||
"FRPLM", | ||
"FRPY2", | ||
"FRRVE", | ||
"FRSLM", | ||
"FRUB2", | ||
"FRUSC", | ||
"FRVAY", | ||
"FRVIR", | ||
"FRWII", | ||
"FRYAW", | ||
"FRZMR", | ||
} | ||
|
||
|
||
def get_table(): | ||
"""Get operational units table.""" | ||
# Get OperationalUnit table | ||
metadata = MetaData() | ||
metadata.reflect(bind=op.get_bind()) | ||
return metadata.tables["operationalunit"] | ||
|
||
|
||
def upgrade() -> None: | ||
"""Create new entries and update existing ones.""" | ||
ou_table = get_table() | ||
now = datetime.now(timezone.utc) | ||
|
||
# Bulk insert | ||
op.bulk_insert( | ||
ou_table, | ||
[ | ||
{ | ||
"id": uuid4().hex, | ||
"created_at": now, | ||
"updated_at": now, | ||
"type": "CHARGING", | ||
} | ||
| ou._asdict() | ||
for ou in operational_units | ||
if ou.code in NEW | ||
], | ||
) | ||
|
||
# Updates | ||
to_update = [ou for ou in operational_units if ou.code in LEGACY.keys()] | ||
for ou in to_update: | ||
op.execute( | ||
ou_table.update() | ||
.where(ou_table.c.code == op.inline_literal(ou.code)) | ||
.values({"name": op.inline_literal(ou.name), "updated_at": now}) | ||
) | ||
|
||
|
||
def downgrade() -> None: | ||
"""Remove new entries and restore legacy names.""" | ||
ou_table = get_table() | ||
|
||
# Revert updates | ||
to_update = [ou for ou in operational_units if ou.code in LEGACY.keys()] | ||
for ou in to_update: | ||
op.execute( | ||
ou_table.update() | ||
.where(ou_table.c.code == op.inline_literal(ou.code)) | ||
.values({"name": op.inline_literal(LEGACY[ou.code])}) | ||
) | ||
|
||
# Delete new entries | ||
op.execute(ou_table.delete().where(ou_table.c.code.in_(NEW))) |
Oops, something went wrong.