From 36818c4b4fd0425a6374e2be01587348d862a108 Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Thu, 18 Jul 2024 16:18:51 +0200 Subject: [PATCH 1/3] feat(occhab) : `is_habitat_complex` (bool) devient `id_nomenclature_habitat_complexity` ( nomenclature) --- backend/dependencies/Nomenclature-api-module | 2 +- ...793360_replace_is_complex_with_habitat_.py | 63 +++++++++++++++++++ .../backend/gn_module_occhab/models.py | 10 ++- 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 contrib/gn_module_occhab/backend/gn_module_occhab/migrations/c1a6b0793360_replace_is_complex_with_habitat_.py diff --git a/backend/dependencies/Nomenclature-api-module b/backend/dependencies/Nomenclature-api-module index e692f07af8..8e1eb31a80 160000 --- a/backend/dependencies/Nomenclature-api-module +++ b/backend/dependencies/Nomenclature-api-module @@ -1 +1 @@ -Subproject commit e692f07af863fb9ad67d380b338ec5981ccd1d03 +Subproject commit 8e1eb31a801c40b9089c5b2939ca52d6ac888af3 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 new file mode 100644 index 0000000000..2b34ac0b0d --- /dev/null +++ b/contrib/gn_module_occhab/backend/gn_module_occhab/migrations/c1a6b0793360_replace_is_complex_with_habitat_.py @@ -0,0 +1,63 @@ +"""replace_is_complex_with_habitat_complexity + +Revision ID: c1a6b0793360 +Revises: 295861464d84 +Create Date: 2024-07-18 15:52:38.695575 + +""" + +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 + + +# revision identifiers, used by Alembic. +revision = "c1a6b0793360" +down_revision = "295861464d84" +branch_labels = None +depends_on = None + + +def upgrade(): + op.add_column( + "t_stations", + sa.Column( + "id_nomenclature_habitat_complexity", + sa.Integer(), + sa.ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"), + nullable=True, + ), + schema="pr_occhab", + ) + session = Session(bind=op.get_bind()) + id_habitat_complexity_true = session.scalar( + sa.select(TNomenclatures.id_nomenclature).where( + TNomenclatures.mnemonique == "Mosaïque mixte" + ) + ) + session.close() + 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) + ) + 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 + """ + ) + + +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.add_column( + "t_stations", + sa.Column("is_habitat_complex", sa.Boolean(), nullable=True), + schema="pr_occhab", + ) 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 1ca9e4cfea..de4c98d832 100644 --- a/contrib/gn_module_occhab/backend/gn_module_occhab/models.py +++ b/contrib/gn_module_occhab/backend/gn_module_occhab/models.py @@ -41,7 +41,15 @@ class Station(NomenclaturesMixin, db.Model): date_max = db.Column(db.DateTime, server_default=FetchedValue()) observers_txt = db.Column(db.Unicode(length=500)) station_name = db.Column(db.Unicode(length=1000)) - is_habitat_complex = db.Column(db.Boolean) + # is_habitat_complex = db.Column(db.Boolean) + id_nomenclature_habitat_complexity = db.Column( + db.Integer, + ForeignKey(Nomenclature.id_nomenclature), + ) + nomenclature_habitat_complexity = db.relationship( + Nomenclature, + foreign_keys=[id_nomenclature_habitat_complexity], + ) altitude_min = db.Column(db.Integer) altitude_max = db.Column(db.Integer) depth_min = db.Column(db.Integer) From 221fc83dd06c03c18cfeba2ca42d13ed669ffc44 Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Fri, 13 Sep 2024 15:10:21 +0200 Subject: [PATCH 2/3] 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 de4c98d832..23dbdfb44f 100644 --- a/contrib/gn_module_occhab/backend/gn_module_occhab/models.py +++ b/contrib/gn_module_occhab/backend/gn_module_occhab/models.py @@ -42,13 +42,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) From 0ffe56457b5d27739da754fbe077dd3a97ab3569 Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Fri, 13 Sep 2024 15:14:52 +0200 Subject: [PATCH 3/3] update Nomenclature-api-module --- backend/dependencies/Nomenclature-api-module | 2 +- backend/requirements-dependencies.in | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/dependencies/Nomenclature-api-module b/backend/dependencies/Nomenclature-api-module index 8e1eb31a80..ee9c44f616 160000 --- a/backend/dependencies/Nomenclature-api-module +++ b/backend/dependencies/Nomenclature-api-module @@ -1 +1 @@ -Subproject commit 8e1eb31a801c40b9089c5b2939ca52d6ac888af3 +Subproject commit ee9c44f61674f8ffabb9a252369f3ab85e38b3df diff --git a/backend/requirements-dependencies.in b/backend/requirements-dependencies.in index eccc6681d5..af333de8b0 100644 --- a/backend/requirements-dependencies.in +++ b/backend/requirements-dependencies.in @@ -1,5 +1,5 @@ pypnusershub>=3.0.0,<4 -pypnnomenclature>=1.6.3,<2 +pypnnomenclature>=1.6.4,<2 pypn_habref_api>=0.4.1,<1 utils-flask-sqlalchemy-geo>=0.3.2,<1 utils-flask-sqlalchemy>=0.4.1,<1