Skip to content

Commit

Permalink
feat(migration): update revision
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Oct 30, 2024
1 parent 4315261 commit 8d28804
Showing 1 changed file with 93 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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
"""
Expand All @@ -14,7 +14,7 @@

# revision identifiers, used by Alembic.
revision = "c1a6b0793360"
down_revision = "ebbe0f7ed866"
down_revision = "aed662bbd88a"
branch_labels = None
depends_on = None

Expand All @@ -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(
Expand All @@ -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"))
Expand All @@ -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,
)
)

0 comments on commit 8d28804

Please sign in to comment.