From 76b864947826a4b244ac08c15aba0247f01dd239 Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Fri, 13 Sep 2024 15:10:21 +0200 Subject: [PATCH] rename new column for mosaique type --- ...793360_replace_is_complex_with_habitat_.py | 20 +++++++++++-------- .../backend/gn_module_occhab/models.py | 6 +++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/contrib/gn_module_occhab/backend/gn_module_occhab/migrations/c1a6b0793360_replace_is_complex_with_habitat_.py b/contrib/gn_module_occhab/backend/gn_module_occhab/migrations/c1a6b0793360_replace_is_complex_with_habitat_.py index 2b34ac0b0d..254c9393e7 100644 --- a/contrib/gn_module_occhab/backend/gn_module_occhab/migrations/c1a6b0793360_replace_is_complex_with_habitat_.py +++ b/contrib/gn_module_occhab/backend/gn_module_occhab/migrations/c1a6b0793360_replace_is_complex_with_habitat_.py @@ -7,7 +7,6 @@ """ from alembic import op -from gn_module_occhab.models import Station from pypnnomenclature.models import TNomenclatures import sqlalchemy as sa from sqlalchemy.orm.session import Session @@ -21,10 +20,15 @@ def upgrade(): + + conn = op.get_bind() + metadata = sa.MetaData(bind=conn) + Station = sa.Table("t_stations", metadata, schema="pr_occhab", autoload_with=conn) + op.add_column( "t_stations", sa.Column( - "id_nomenclature_habitat_complexity", + "id_nomenclature_type_mosaique_habitat", sa.Integer(), sa.ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"), nullable=True, @@ -32,7 +36,7 @@ def upgrade(): schema="pr_occhab", ) session = Session(bind=op.get_bind()) - id_habitat_complexity_true = session.scalar( + id_default_type_mosaique_habitat = session.scalar( sa.select(TNomenclatures.id_nomenclature).where( TNomenclatures.mnemonique == "Mosaïque mixte" ) @@ -41,21 +45,21 @@ def upgrade(): op.execute( sa.update(Station) .where(sa.text("pr_occhab.t_stations.is_habitat_complex = true")) - .values(id_nomenclature_habitat_complexity=id_habitat_complexity_true) + .values(id_nomenclature_type_mosaique_habitat=id_default_type_mosaique_habitat) ) op.drop_column("t_stations", "is_habitat_complex", schema="pr_occhab") op.execute( """ ALTER TABLE pr_occhab.t_stations ADD CONSTRAINT - check_t_stations_habitat_complexity CHECK - (ref_nomenclatures.check_nomenclature_type_by_mnemonique(id_nomenclature_habitat_complexity, 'MOSAIQUE_HAB'::character varying)) NOT VALID + check_t_stations_type_mosaique_habitat CHECK + (ref_nomenclatures.check_nomenclature_type_by_mnemonique(id_nomenclature_type_mosaique_habitat, 'MOSAIQUE_HAB'::character varying)) NOT VALID """ ) def downgrade(): - op.drop_constraint("check_t_stations_habitat_complexity", "t_stations", schema="pr_occhab") - op.drop_column("t_stations", "id_nomenclature_habitat_complexity", schema="pr_occhab") + op.drop_constraint("check_t_stations_type_mosaique_habitat", "t_stations", schema="pr_occhab") + op.drop_column("t_stations", "id_nomenclature_type_mosaique_habitat", schema="pr_occhab") op.add_column( "t_stations", sa.Column("is_habitat_complex", sa.Boolean(), nullable=True), diff --git a/contrib/gn_module_occhab/backend/gn_module_occhab/models.py b/contrib/gn_module_occhab/backend/gn_module_occhab/models.py index 3d83ed4e99..bbe5d23ec4 100644 --- a/contrib/gn_module_occhab/backend/gn_module_occhab/models.py +++ b/contrib/gn_module_occhab/backend/gn_module_occhab/models.py @@ -49,13 +49,13 @@ class Station(NomenclaturesMixin, db.Model): observers_txt = db.Column(db.Unicode(length=500)) station_name = db.Column(db.Unicode(length=1000)) # is_habitat_complex = db.Column(db.Boolean) - id_nomenclature_habitat_complexity = db.Column( + id_nomenclature_type_mosaique_habitat = db.Column( db.Integer, ForeignKey(Nomenclature.id_nomenclature), ) - nomenclature_habitat_complexity = db.relationship( + type_mosaique_habitat = db.relationship( Nomenclature, - foreign_keys=[id_nomenclature_habitat_complexity], + foreign_keys=[id_nomenclature_type_mosaique_habitat], ) altitude_min = db.Column(db.Integer) altitude_max = db.Column(db.Integer)