From 11068457c16b78678c98f145fe7b1e630b4696ef Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Thu, 18 Jul 2024 16:18:51 +0200 Subject: [PATCH 1/6] feat(occhab) : `is_habitat_complex` (bool) devient `id_nomenclature_habitat_complexity` ( nomenclature) --- ...793360_replace_is_complex_with_habitat_.py | 63 +++++++++++++++++++ .../backend/gn_module_occhab/models.py | 10 ++- 2 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 contrib/gn_module_occhab/backend/gn_module_occhab/migrations/c1a6b0793360_replace_is_complex_with_habitat_.py 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 0f42859217..3d83ed4e99 100644 --- a/contrib/gn_module_occhab/backend/gn_module_occhab/models.py +++ b/contrib/gn_module_occhab/backend/gn_module_occhab/models.py @@ -48,7 +48,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 76b864947826a4b244ac08c15aba0247f01dd239 Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Fri, 13 Sep 2024 15:10:21 +0200 Subject: [PATCH 2/6] 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) From 4315261021011ac01d74944dde064fd0042cda0c Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Mon, 28 Oct 2024 13:31:31 +0100 Subject: [PATCH 3/6] feat(db) : update migration down revision --- .../migrations/c1a6b0793360_replace_is_complex_with_habitat_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 254c9393e7..7d19bb3521 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 @@ -14,7 +14,7 @@ # revision identifiers, used by Alembic. revision = "c1a6b0793360" -down_revision = "295861464d84" +down_revision = "ebbe0f7ed866" branch_labels = None depends_on = None From 8d28804a5789b66f9fa3065f726715e5f7d695ed Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Wed, 30 Oct 2024 14:28:11 +0100 Subject: [PATCH 4/6] feat(migration): update revision --- ...793360_replace_is_complex_with_habitat_.py | 97 ++++++++++++++++++- 1 file changed, 93 insertions(+), 4 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 7d19bb3521..deab3a45c6 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 @@ -1,7 +1,7 @@ """replace_is_complex_with_habitat_complexity Revision ID: c1a6b0793360 -Revises: 295861464d84 +Revises: 7b6a578eccd7 Create Date: 2024-07-18 15:52:38.695575 """ @@ -14,7 +14,7 @@ # revision identifiers, used by Alembic. revision = "c1a6b0793360" -down_revision = "ebbe0f7ed866" +down_revision = "aed662bbd88a" branch_labels = None depends_on = None @@ -23,8 +23,8 @@ def upgrade(): conn = op.get_bind() metadata = sa.MetaData(bind=conn) - Station = sa.Table("t_stations", metadata, schema="pr_occhab", autoload_with=conn) + # ADD new column op.add_column( "t_stations", sa.Column( @@ -35,13 +35,23 @@ def upgrade(): ), schema="pr_occhab", ) + + # GET required nomenclature id and occhab destination id session = Session(bind=op.get_bind()) id_default_type_mosaique_habitat = session.scalar( sa.select(TNomenclatures.id_nomenclature).where( TNomenclatures.mnemonique == "Mosaïque mixte" ) ) + Destination = sa.Table("bib_destinations", metadata, schema="gn_imports", autoload_with=conn) + + id_destination_occhab = session.scalar( + sa.select(Destination.c.id_destination).where(Destination.c.code == "occhab") + ) session.close() + + # UPDATE Station table + Station = sa.Table("t_stations", metadata, schema="pr_occhab", autoload_with=conn) op.execute( sa.update(Station) .where(sa.text("pr_occhab.t_stations.is_habitat_complex = true")) @@ -55,13 +65,92 @@ def upgrade(): (ref_nomenclatures.check_nomenclature_type_by_mnemonique(id_nomenclature_type_mosaique_habitat, 'MOSAIQUE_HAB'::character varying)) NOT VALID """ ) + ## UPDATE Transient table + op.add_column( + "t_imports_occhab", + sa.Column("src_id_nomenclature_type_mosaique_habitat", sa.Unicode, nullable=True), + schema="gn_imports", + ) + op.add_column( + "t_imports_occhab", + sa.Column( + "id_nomenclature_type_mosaique_habitat", + sa.Integer, + sa.ForeignKey("ref_nomenclatures.t_nomenclatures.id_nomenclature"), + ), + schema="gn_imports", + ) + op.drop_column("t_imports_occhab", "src_is_habitat_complex", schema="gn_imports") + op.drop_column("t_imports_occhab", "is_habitat_complex", schema="gn_imports") + + # UPDATE BibFields + BibFields = sa.Table("bib_fields", metadata, schema="gn_imports", autoload_with=conn) + op.execute(sa.delete(BibFields).where(BibFields.c.name_field == "is_habitat_complex")) + op.execute( + sa.insert(BibFields).values( + name_field="id_nomenclature_type_mosaique_habitat", + fr_label="Mosaïque d'habitat", + source_field="src_id_nomenclature_type_mosaique_habitat", + dest_field="id_nomenclature_type_mosaique_habitat", + mandatory=False, + mnemonique="MOSAIQUE_HAB", + autogenerated=False, + display=True, + id_destination=id_destination_occhab, + ) + ) def downgrade(): + conn = op.get_bind() + metadata = sa.MetaData(bind=conn) + + session = Session(bind=op.get_bind()) + + Destination = sa.Table("bib_destinations", metadata, schema="gn_imports", autoload_with=conn) + id_destination_occhab = session.scalar( + sa.select(Destination.c.id_destination).where(Destination.c.code == "occhab") + ) + session.close() + 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), + sa.Column("is_habitat_complex", sa.Boolean, nullable=True), schema="pr_occhab", ) + op.drop_column("t_imports_occhab", "id_nomenclature_type_mosaique_habitat", schema="gn_imports") + op.drop_column( + "t_imports_occhab", "src_id_nomenclature_type_mosaique_habitat", schema="gn_imports" + ) + op.add_column( + "t_imports_occhab", + sa.Column("src_is_habitat_complex", sa.Unicode, nullable=True), + schema="gn_imports", + ) + op.add_column( + "t_imports_occhab", + sa.Column("is_habitat_complex", sa.Boolean, nullable=True), + schema="gn_imports", + ) + BibFields = sa.Table( + "bib_fields", sa.MetaData(), schema="gn_imports", autoload_with=op.get_bind() + ) + op.execute( + sa.delete(BibFields).where( + BibFields.c.name_field == "id_nomenclature_type_mosaique_habitat" + ) + ) + op.execute( + sa.insert(BibFields).values( + name_field="is_habitat_complex", + fr_label="Complexité d'habitat", + source_field="src_is_habitat_complex", + dest_field="is_habitat_complex", + mandatory=False, + autogenerated=False, + display=True, + id_destination=id_destination_occhab, + ) + ) From 2a759ff0ae008462b18cf0daa9a2b0934298d422 Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Wed, 30 Oct 2024 15:41:23 +0100 Subject: [PATCH 5/6] feat(db) : update revision by including bib_fields and cor_field_entity required changes --- ...793360_replace_is_complex_with_habitat_.py | 66 ++++++++++++++----- 1 file changed, 50 insertions(+), 16 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 deab3a45c6..cf28f5ed05 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 @@ -38,22 +38,34 @@ def upgrade(): # GET required nomenclature id and occhab destination id session = Session(bind=op.get_bind()) + id_default_type_mosaique_habitat = session.scalar( sa.select(TNomenclatures.id_nomenclature).where( TNomenclatures.mnemonique == "Mosaïque mixte" ) ) - Destination = sa.Table("bib_destinations", metadata, schema="gn_imports", autoload_with=conn) + destination = sa.Table("bib_destinations", metadata, schema="gn_imports", autoload_with=conn) + entity = sa.Table("bib_entities", metadata, autoload=True, schema="gn_imports") + theme = sa.Table("bib_themes", metadata, autoload=True, schema="gn_imports") + station = sa.Table("t_stations", metadata, schema="pr_occhab", autoload_with=conn) + cor_entity_field = sa.Table("cor_entity_field", metadata, autoload=True, schema="gn_imports") + id_theme_general = session.execute( + sa.select(theme.c.id_theme).where(theme.c.name_theme == "general_info") + ).scalar() id_destination_occhab = session.scalar( - sa.select(Destination.c.id_destination).where(Destination.c.code == "occhab") + sa.select(destination.c.id_destination).where(destination.c.code == "occhab") ) + id_entity_station = session.execute( + sa.select(entity.c.id_entity).where(entity.c.code == "station") + ).scalar() + session.close() # UPDATE Station table - Station = sa.Table("t_stations", metadata, schema="pr_occhab", autoload_with=conn) + op.execute( - sa.update(Station) + sa.update(station) .where(sa.text("pr_occhab.t_stations.is_habitat_complex = true")) .values(id_nomenclature_type_mosaique_habitat=id_default_type_mosaique_habitat) ) @@ -84,19 +96,41 @@ def upgrade(): op.drop_column("t_imports_occhab", "is_habitat_complex", schema="gn_imports") # UPDATE BibFields - BibFields = sa.Table("bib_fields", metadata, schema="gn_imports", autoload_with=conn) - op.execute(sa.delete(BibFields).where(BibFields.c.name_field == "is_habitat_complex")) + bib_fields = sa.Table("bib_fields", metadata, schema="gn_imports", autoload_with=conn) + op.execute(sa.delete(bib_fields).where(bib_fields.c.name_field == "is_habitat_complex")) + id_new_field = ( + op.get_bind() + .execute( + sa.insert(bib_fields) + .values( + name_field="id_nomenclature_type_mosaique_habitat", + fr_label="Mosaïque d'habitat", + source_field="src_id_nomenclature_type_mosaique_habitat", + dest_field="id_nomenclature_type_mosaique_habitat", + mandatory=False, + mnemonique="MOSAIQUE_HAB", + autogenerated=False, + display=True, + id_destination=id_destination_occhab, + ) + .returning(bib_fields.c.id_field) + ) + .scalar() + ) + op.execute( - sa.insert(BibFields).values( - name_field="id_nomenclature_type_mosaique_habitat", - fr_label="Mosaïque d'habitat", - source_field="src_id_nomenclature_type_mosaique_habitat", - dest_field="id_nomenclature_type_mosaique_habitat", - mandatory=False, - mnemonique="MOSAIQUE_HAB", - autogenerated=False, - display=True, - id_destination=id_destination_occhab, + sa.insert(cor_entity_field).values( + id_entity=id_entity_station, + id_field=id_new_field, + id_theme=id_theme_general, + comment="Correspondance champs standard: mosaiqueValue", + order_field=op.get_bind() + .execute( + sa.select(sa.func.max(cor_entity_field.c.order_field) + 1).select_from( + cor_entity_field + ) + ) + .scalar(), ) ) From e4c8aff74a1132e0c252f2e6cd6601d7b7fe0288 Mon Sep 17 00:00:00 2001 From: Jacques Fize <4259846+jacquesfize@users.noreply.github.com> Date: Wed, 30 Oct 2024 16:34:36 +0100 Subject: [PATCH 6/6] Update contrib/gn_module_occhab/backend/gn_module_occhab/migrations/c1a6b0793360_replace_is_complex_with_habitat_.py --- .../migrations/c1a6b0793360_replace_is_complex_with_habitat_.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 cf28f5ed05..e887eb98a0 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 @@ -104,7 +104,7 @@ def upgrade(): sa.insert(bib_fields) .values( name_field="id_nomenclature_type_mosaique_habitat", - fr_label="Mosaïque d'habitat", + fr_label="Mosaïque d'habitats", source_field="src_id_nomenclature_type_mosaique_habitat", dest_field="id_nomenclature_type_mosaique_habitat", mandatory=False,