Skip to content

Commit

Permalink
fix: drop table insee_regions
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Corny committed Dec 13, 2024
1 parent 20a5e41 commit cd1ed7a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""drop ref_geo.insee_regions
Revision ID: da5b95b24f06
Revises: 72a8378567pa
Create Date: 2024-12-13 16:16:43.446953
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "da5b95b24f06"
down_revision = "72a8378567pa"
branch_labels = None
depends_on = None


def upgrade():
op.drop_table(table_name="insee_regions", schema="ref_geo")


def downgrade():
op.execute(
"""
CREATE TABLE IF NOT EXISTS ref_geo.insee_regions (
insee_reg varchar(2) NOT NULL,
region_name varchar(50) NOT NULL,
CONSTRAINT pk_insee_regions_insee_code PRIMARY KEY ( insee_reg ),
CONSTRAINT unq_insee_region_name UNIQUE ( region_name )
);
INSERT INTO ref_geo.insee_regions(insee_reg,region_name) VALUES
('01','Guadeloupe'),
('02','Martinique'),
('03','Guyane'),
('04','La Réunion'),
('06','Mayotte'),
('11','Île-de-France'),
('24','Centre-Val de Loire'),
('27','Bourgogne-Franche-Comté'),
('28','Normandie'),
('32','Hauts-de-France'),
('44','Grand Est'),
('52','Pays de la Loire'),
('53','Bretagne'),
('75','Nouvelle-Aquitaine'),
('76','Occitanie'),
('84','Auvergne-Rhône-Alpes'),
('93','Provence-Alpes-Côte d''Azur'),
('94','Corse')
ON CONFLICT (insee_reg) DO NOTHING
;
"""
)
13 changes: 10 additions & 3 deletions backend/gn_module_zh/model/zh.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# instance de la BDD
from geonature.utils.env import DB
from sqlalchemy import and_
from sqlalchemy.sql import select
from ref_geo.models import BibAreasTypes, LAreas

from .zh_schema import (
TZH,
Expand All @@ -14,7 +16,6 @@
CorZhLimFs,
CorZhProtection,
CorZhRef,
InseeRegions,
TActions,
TActivity,
TFunctions,
Expand Down Expand Up @@ -255,9 +256,15 @@ def get_regions(self, query):
if municipality.LiMunicipalities.insee_reg not in region_list:
region_list.append(municipality.LiMunicipalities.insee_reg)
q_region = DB.session.scalars(
select(InseeRegions).where(InseeRegions.insee_reg.in_(region_list))
select(LAreas)
.select_from(LAreas, BibAreasTypes)
.where(
and_(LAreas.area_code.in_(region_list)),
LAreas.id_type == BibAreasTypes.id_type,
BibAreasTypes.type_name == "Régions",
)
).all()
regions = [region.region_name for region in q_region]
regions = [region.area_name for region in q_region]
return regions

def get_area(self):
Expand Down
7 changes: 0 additions & 7 deletions backend/gn_module_zh/model/zh_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,13 +885,6 @@ class CorZhProtection(DB.Model):
id_zh = DB.Column(DB.Integer, ForeignKey(TZH.id_zh), primary_key=True)


class InseeRegions(DB.Model):
__tablename__ = "insee_regions"
__table_args__ = {"schema": "ref_geo"}
insee_reg = DB.Column(DB.Unicode(length=2), primary_key=True)
region_name = DB.Column(DB.Unicode(length=50), nullable=False)


class TManagementStructures(DB.Model):
__tablename__ = "t_management_structures"
__table_args__ = {"schema": "pr_zh"}
Expand Down

0 comments on commit cd1ed7a

Please sign in to comment.