Skip to content

Commit

Permalink
rename new column for mosaique type
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesfize committed Sep 13, 2024
1 parent c884e65 commit 894f18d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,18 +20,23 @@


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,
),
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"
)
Expand All @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions contrib/gn_module_occhab/backend/gn_module_occhab/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 894f18d

Please sign in to comment.