Skip to content

Commit

Permalink
Fix/db migrations (#31)
Browse files Browse the repository at this point in the history
* feat(db): upgrade down_revision following rebase

Since rebase with develop: changed the down_revision number

* fix(db): fix bind params enabling downgrade

Beforehand the downgrade was not possible...

* refactor(db): removed cor_site_type_category

* refactor(db): changed category into type in cor

* refactor(db): create cor_type_site

* fix(db): renamed column

* refactor(api): update models to fit migrations

* fix(db):change bib_categorie_site to bib_type_site

Adding :
cor_site_module
cor_site_type
revision alembic to create function and trigger in order to add
bib_type_site but only with nomenclature 'TYPE_SITE'
upgrade and downgrade works

[Refs ticket]: #3
Reviewed-by: andriac

* fix(api): updated models from migrations

* fix(api): wip: fix admin following migrations

* fix(api): update routes and tests

To match migration changes

* feat: flask admin bib_type_site

Change bib_categories to bib_type_site into flask admin
Adding filtering in list label_fr of type_site to secure the unique
constraint

Reviewed-by: andriac
[Refs ticket]: #3

* fix(api): updated schema to match models

* fix(api): module edition

* style(api): uniformize type_site

* style(api): change relationship name for type_site

* feat(api): validator admin

* fix(api): make unique BibTypeSite in admin

* test(api): fix test when existing nomenclatures

In database

Co-authored-by: Andria Capai <[email protected]>
  • Loading branch information
mvergez and andriacap committed Aug 23, 2023
1 parent eaac7c9 commit 72653b6
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 240 deletions.
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(
Expand All @@ -19,4 +19,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"))
12 changes: 6 additions & 6 deletions backend/gn_module_monitoring/config/generic/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,18 @@
"definition": "Afficher le module dans le menu de GeoNature. (Recharger la page pour voir les modifications)."
},

"categories": {
"types_site": {
"type_widget": "datalist",
"attribut_label": "Catégories de sites",
"type_util": "categorie",
"keyValue": "id_categorie",
"attribut_label": "Types de sites",
"type_util": "types_site",
"keyValue": "id_nomenclature",
"keyLabel": "label",
"multiple": true,
"api" : "__MONITORINGS_PATH/sites/categories",
"api" : "__MONITORINGS_PATH/sites/types",
"application": "GeoNature",
"required": true,
"data_path": "items",
"definition": "Permet de paramétrer la compatibilité de ce module avec les catégories de sites"
"definition": "Permet de paramétrer la compatibilité de ce module avec les types de sites"
},

"medias": {
Expand Down
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

0 comments on commit 72653b6

Please sign in to comment.