From 8d28804a5789b66f9fa3065f726715e5f7d695ed Mon Sep 17 00:00:00 2001 From: jacquesfize Date: Wed, 30 Oct 2024 14:28:11 +0100 Subject: [PATCH] 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, + ) + )