Skip to content

Commit

Permalink
Fix/db migrations checkconstrainton bib_type_site.id_nomenclature (#34)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
andriacap authored and amandine-sahl committed Dec 8, 2023
1 parent a50d2ca commit d0588ac
Showing 1 changed file with 6 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
monitorings_schema = "gn_monitoring"
nomenclature_schema = "ref_nomenclatures"

TYPE_SITE = "TYPE_SITE"


def upgrade():
op.create_table(
Expand All @@ -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)

0 comments on commit d0588ac

Please sign in to comment.