From d0588acf996885dd1f9aa5d343a645d279cfe78e Mon Sep 17 00:00:00 2001 From: andriacap <111564663+andriacap@users.noreply.github.com> Date: Mon, 16 Jan 2023 16:29:48 +0100 Subject: [PATCH] Fix/db migrations checkconstrainton bib_type_site.id_nomenclature (#34) * fix(db): change trigger to constraint (migrations) Delete the trigger and create check constraint on id_nomenclature column Reviewed-by: andriacap [Refs ticket]: #3 * fix(db) : apply black on migration file Apply black [Refs_ticket]: #3 --- .../b53bafb13ce8_create_bib_type_site.py | 35 ++++--------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/backend/gn_module_monitoring/migrations/b53bafb13ce8_create_bib_type_site.py b/backend/gn_module_monitoring/migrations/b53bafb13ce8_create_bib_type_site.py index a064d26bb..91c8de459 100644 --- a/backend/gn_module_monitoring/migrations/b53bafb13ce8_create_bib_type_site.py +++ b/backend/gn_module_monitoring/migrations/b53bafb13ce8_create_bib_type_site.py @@ -17,8 +17,6 @@ monitorings_schema = "gn_monitoring" nomenclature_schema = "ref_nomenclatures" -TYPE_SITE = "TYPE_SITE" - def upgrade(): op.create_table( @@ -38,34 +36,13 @@ def upgrade(): 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) + op.create_check_constraint( + "ck_bib_type_site_id_nomenclature", + "bib_type_site", + f"{nomenclature_schema}.check_nomenclature_type_by_mnemonique(id_nomenclature,'TYPE_SITE')", + schema=monitorings_schema, + ) 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)