-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage models in geonature not gn_module_monitoring
- Loading branch information
1 parent
4a3148e
commit 816296f
Showing
5 changed files
with
339 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
backend/geonature/migrations/versions/a54bafb13ce8_create_cor_module_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""create_cor_module_type | ||
Revision ID: a54bafb13ce8 | ||
Revises: ce54ba49ce5c | ||
Create Date: 2022-12-06 16:18:24.512562 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "a54bafb13ce8" | ||
down_revision = "ce54ba49ce5c" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
monitorings_schema = "gn_monitoring" | ||
referent_schema = "gn_commons" | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"cor_module_type", | ||
sa.Column( | ||
"id_type_site", | ||
sa.Integer(), | ||
sa.ForeignKey( | ||
f"{monitorings_schema}.bib_type_site.id_nomenclature_type_site", | ||
name="fk_cor_module_type_id_nomenclature", | ||
ondelete="CASCADE", | ||
onupdate="CASCADE", | ||
), | ||
nullable=False, | ||
), | ||
sa.Column( | ||
"id_module", | ||
sa.Integer(), | ||
sa.ForeignKey( | ||
f"{referent_schema}.t_modules.id_module", | ||
name="fk_cor_module_type_id_module", | ||
ondelete="CASCADE", | ||
onupdate="CASCADE", | ||
), | ||
nullable=False, | ||
), | ||
sa.PrimaryKeyConstraint("id_type_site", "id_module", name="pk_cor_module_type"), | ||
schema=monitorings_schema, | ||
) | ||
|
||
# Insertion des données a partir de cor_site_module | ||
op.execute( | ||
""" | ||
INSERT INTO gn_monitoring.cor_module_type (id_module, id_type_site ) | ||
SELECT DISTINCT csm.id_module, cts.id_type_site | ||
FROM gn_monitoring.cor_site_module AS csm | ||
JOIN gn_monitoring.cor_type_site AS cts | ||
ON Cts.id_base_site = csm.id_base_site ; | ||
""" | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table("cor_module_type", schema=monitorings_schema) |
76 changes: 76 additions & 0 deletions
76
backend/geonature/migrations/versions/b53bafb13ce8_create_bib_type_site.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
"""create_bib_type_site | ||
Revision ID: b53bafb13ce8 | ||
Revises: 446e902a14e7 | ||
Create Date: 2022-12-06 16:18:24.512562 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "b53bafb13ce8" | ||
down_revision = "446e902a14e7" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
monitorings_schema = "gn_monitoring" | ||
nomenclature_schema = "ref_nomenclatures" | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"bib_type_site", | ||
sa.Column( | ||
"id_nomenclature_type_site", | ||
sa.Integer(), | ||
sa.ForeignKey( | ||
f"{nomenclature_schema}.t_nomenclatures.id_nomenclature", | ||
name="fk_t_nomenclatures_id_nomenclature_type_site", | ||
), | ||
nullable=False, | ||
unique=True, | ||
), | ||
sa.PrimaryKeyConstraint("id_nomenclature_type_site"), | ||
sa.Column("config", sa.JSON(), nullable=True), | ||
schema=monitorings_schema, | ||
) | ||
|
||
# FIXME: if sqlalchemy >= 1.4.32, it should work with postgresql_not_valid=True: cleaner | ||
# op.create_check_constraint( | ||
# "ck_bib_type_site_id_nomenclature_type_site", | ||
# "bib_type_site", | ||
# f"{nomenclature_schema}.check_nomenclature_type_by_mnemonique(id_nomenclature_type_site,'TYPE_SITE')", | ||
# schema=monitorings_schema, | ||
# postgresql_not_valid=True | ||
# ) | ||
statement = sa.text( | ||
f""" | ||
ALTER TABLE {monitorings_schema}.bib_type_site | ||
ADD | ||
CONSTRAINT ck_bib_type_site_id_nomenclature_type_site CHECK ( | ||
{nomenclature_schema}.check_nomenclature_type_by_mnemonique( | ||
id_nomenclature_type_site, 'TYPE_SITE' :: character varying | ||
) | ||
) NOT VALID | ||
""" | ||
) | ||
op.execute(statement) | ||
op.create_table_comment( | ||
"bib_type_site", | ||
"Table de définition des champs associés aux types de sites", | ||
schema=monitorings_schema, | ||
) | ||
|
||
# Récupération de la liste des types de site avec ceux déja présents dans la table t_base_site | ||
op.execute( | ||
""" | ||
INSERT INTO gn_monitoring.bib_type_site AS bts (id_nomenclature_type_site) | ||
SELECT DISTINCT id_nomenclature_type_site | ||
FROM gn_monitoring.t_base_sites AS tbs ; | ||
""" | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table("bib_type_site", schema=monitorings_schema) |
Oops, something went wrong.