From 8a4813ead53ca578eeaf9f4c9e854f1f21437f3b Mon Sep 17 00:00:00 2001 From: Julien Maupetit Date: Wed, 16 Oct 2024 17:48:17 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=97=83=EF=B8=8F(api)=20add=20geo=20schema?= =?UTF-8?q?s=20missing=20constraints?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some constraints linked to recent database schema changes were missing: no primary keys and foreign keys was defined. --- src/api/CHANGELOG.md | 10 ++-- .../versions/6e610f62bcc6_add_missing_fk.py | 47 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/api/qualicharge/migrations/versions/6e610f62bcc6_add_missing_fk.py diff --git a/src/api/CHANGELOG.md b/src/api/CHANGELOG.md index 3f962fc9..cde19817 100644 --- a/src/api/CHANGELOG.md +++ b/src/api/CHANGELOG.md @@ -13,11 +13,11 @@ and this project adheres to - Upgrade fastapi to `0.115.2` - Upgrade pyinstrument to `5.0.0` -## [0.13.0] - 2024-10-11 - ### Fixed -- Migrate administrative boundaries tables schema and data +- Add missing City / Department table foreign key constraints + +## [0.13.0] - 2024-10-11 ### Changed @@ -32,6 +32,10 @@ and this project adheres to - Upgrade python-multipart to `0.0.12` - Upgrade sentry-sdk to `74.1.2` +### Fixed + +- Migrate administrative boundaries tables schema and data + ## [0.12.1] - 2024-09-09 ### Fixed diff --git a/src/api/qualicharge/migrations/versions/6e610f62bcc6_add_missing_fk.py b/src/api/qualicharge/migrations/versions/6e610f62bcc6_add_missing_fk.py new file mode 100644 index 00000000..5a964d7a --- /dev/null +++ b/src/api/qualicharge/migrations/versions/6e610f62bcc6_add_missing_fk.py @@ -0,0 +1,47 @@ +"""add missing fk + +Revision ID: 6e610f62bcc6 +Revises: b69ed697b446 +Create Date: 2024-10-16 15:44:28.265424 + +""" + +from typing import Sequence, Union + +from alembic import op + +# revision identifiers, used by Alembic. +revision: str = "6e610f62bcc6" +down_revision: Union[str, None] = "b69ed697b446" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # Create missing PKs + op.create_primary_key("region_pkey", "region", ["id"]) + op.create_primary_key("department_pkey", "department", ["id"]) + op.create_primary_key("epci_pkey", "epci", ["id"]) + op.create_primary_key("city_pkey", "city", ["id"]) + + # And FKs + op.create_foreign_key( + "city_department_id_fkey", "city", "department", ["department_id"], ["id"] + ) + op.create_foreign_key("city_epci_id_fkey", "city", "epci", ["epci_id"], ["id"]) + op.create_foreign_key( + "department_region_id_fkey", "department", "region", ["region_id"], ["id"] + ) + + +def downgrade() -> None: + # Delete PKs + op.drop_constraint("region_pkey", "region", type_="primary") + op.drop_constraint("department_pkey", "department", type_="primary") + op.drop_constraint("epci_pkey", "epci", type_="primary") + op.drop_constraint("city_pkey", "city", type_="primary") + + # Delete FKs + op.drop_constraint("department_region_id_fkey", "department", type_="foreignkey") + op.drop_constraint("city_epci_id_fkey", "city", type_="foreignkey") + op.drop_constraint("city_department_id_fkey", "city", type_="foreignkey")