Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/db migrations #31

Merged
merged 21 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/gn_module_monitoring/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from geonature.core.admin.admin import admin as flask_admin
from geonature.utils.env import DB

from gn_module_monitoring.monitoring.admin import BibCategorieSiteView
from gn_module_monitoring.monitoring.admin import BibTypeSiteView
from .command.cmd import commands

blueprint = Blueprint("monitorings", __name__)
Expand All @@ -17,4 +17,4 @@
for cmd in commands:
blueprint.cli.add_command(cmd)

flask_admin.add_view(BibCategorieSiteView(DB.session, name="Catégories de sites", category="Monitorings"))
flask_admin.add_view(BibTypeSiteView(DB.session, name="Types de site", category="Monitorings"))
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""remove_id_module_from_sites_complements

Revision ID: 6673266fb79c
Revises:
Revises: a54bafb13ce8
Create Date: 2022-12-13 16:00:00.512562

"""
Expand All @@ -12,7 +12,7 @@

# revision identifiers, used by Alembic.
revision = "6673266fb79c"
down_revision = "e64bafb13ce8"
down_revision = "a54bafb13ce8"
branch_labels = None
depends_on = None

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""create_cor_module_category
"""create_cor_module_type

Revision ID: a54bafb13ce8
Revises:
Revises: ce54ba49ce5c
Create Date: 2022-12-06 16:18:24.512562

"""
Expand All @@ -10,7 +10,7 @@

# revision identifiers, used by Alembic.
revision = "a54bafb13ce8"
down_revision = "f24adb481f54"
down_revision = "ce54ba49ce5c"
branch_labels = None
depends_on = None

Expand All @@ -20,28 +20,33 @@

def upgrade():
op.create_table(
"cor_module_categorie",
"cor_module_type",
sa.Column(
"id_categorie",
"id_type_site",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.bib_categorie_site.id_categorie",
name="fk_cor_module_categorie_id_categorie",
f"{monitorings_schema}.bib_type_site.id_nomenclature",
name="fk_cor_module_type_id_nomenclature",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.Column("id_module", sa.Integer(),sa.ForeignKey(
sa.Column(
"id_module",
sa.Integer(),
sa.ForeignKey(
f"{referent_schema}.t_modules.id_module",
name="fk_cor_module_categorie_id_module",
name="fk_cor_module_type_id_module",
ondelete="CASCADE",
onupdate="CASCADE",
), nullable=False),
sa.PrimaryKeyConstraint("id_categorie", "id_module", name="pk_cor_module_categorie"),
),
nullable=False,
),
sa.PrimaryKeyConstraint("id_type_site", "id_module", name="pk_cor_module_type"),
schema=monitorings_schema,
)


def downgrade():
op.drop_table("cor_module_categorie", schema=monitorings_schema)
op.drop_table("cor_module_type", schema=monitorings_schema)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""create_bib_type_site

Revision ID: b53bafb13ce8
Revises: e78003460441
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 = "e78003460441"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"
nomenclature_schema = "ref_nomenclatures"

TYPE_SITE = "TYPE_SITE"


def upgrade():
op.create_table(
"bib_type_site",
sa.Column(
"id_nomenclature",
sa.Integer(),
sa.ForeignKey(
f"{nomenclature_schema}.t_nomenclatures.id_nomenclature",
name="fk_t_nomenclatures_id_nomenclature",
),
nullable=False,
unique=True,
),
sa.PrimaryKeyConstraint("id_nomenclature"),
sa.Column("config", sa.JSON(), nullable=True),
schema=monitorings_schema,
)

statement = sa.text(
f"""
CREATE OR REPLACE FUNCTION {monitorings_schema}.ck_bib_type_site_id_nomenclature()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
perform {nomenclature_schema}.check_nomenclature_type_by_mnemonique(NEW.id_nomenclature, :mnemonique );
RETURN NEW;
END;
$function$
;
DROP TRIGGER IF EXISTS ck_bib_type_site_id_nomenclature on gn_monitoring.bib_type_site;
CREATE TRIGGER ck_bib_type_site_id_nomenclature BEFORE
INSERT
OR
UPDATE ON {monitorings_schema}.bib_type_site FOR EACH ROW EXECUTE PROCEDURE {monitorings_schema}.ck_bib_type_site_id_nomenclature();
"""
).bindparams(mnemonique=TYPE_SITE)
op.execute(statement)


def downgrade():

op.drop_table("bib_type_site", schema=monitorings_schema)
statement = sa.text(
f"""
DROP FUNCTION IF EXISTS {monitorings_schema}.ck_bib_type_site_id_nomenclature;
"""
)
op.execute(statement)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""create_cor_type_site

Revision ID: ce54ba49ce5c
Revises: b53bafb13ce8
Create Date: 2022-12-06 16:18:24.512562

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = "ce54ba49ce5c"
down_revision = "b53bafb13ce8"
branch_labels = None
depends_on = None

monitorings_schema = "gn_monitoring"


def upgrade():
op.create_table(
"cor_type_site",
sa.Column(
"id_type_site",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.bib_type_site.id_nomenclature",
name="fk_cor_type_site_id_nomenclature",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.Column(
"id_base_site",
sa.Integer(),
sa.ForeignKey(
f"{monitorings_schema}.t_base_sites.id_base_site",
name="fk_cor_type_site_id_base_site",
ondelete="CASCADE",
onupdate="CASCADE",
),
nullable=False,
),
sa.PrimaryKeyConstraint("id_type_site", "id_base_site", name="pk_cor_type_site"),
schema=monitorings_schema,
)


def downgrade():
op.drop_table("cor_type_site", schema=monitorings_schema)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""remove_id_module_from_sites_groups

Revision ID: f24adb481f54
Revises:
Revises: 6673266fb79c
Create Date: 2022-12-13 16:00:00.512562

"""
Expand All @@ -12,7 +12,7 @@

# revision identifiers, used by Alembic.
revision = "f24adb481f54"
down_revision = "b53bafb13ce8"
down_revision = "6673266fb79c"
branch_labels = None
depends_on = None

Expand Down Expand Up @@ -47,8 +47,8 @@ def downgrade():
update {monitorings_schema}.t_sites_groups
set id_module = (select id_module
from gn_commons.t_modules tm
where module_code = '\:module_code');
where module_code = :module_code);
"""
)
op.execute(statement, module_code=MODULE_CODE)
op.alter_column("t_sites_groups", "id_module", nullable=False)
).bindparams(module_code=MODULE_CODE)
op.execute(statement)
op.alter_column("t_sites_groups", "id_module", nullable=False, schema=monitorings_schema)
Loading