From a6a95aa06d418f0d2f1993ae2ba402855d7434db Mon Sep 17 00:00:00 2001 From: Maxime Vergez <85738261+mvergez@users.noreply.github.com> Date: Tue, 17 Jan 2023 10:02:58 +0100 Subject: [PATCH] fix(db): add NOT VALID in constraint for bib_type_site (#36) Cannot use alembic to do this because need sqlalchemy >= 1.4.32 --- .../b53bafb13ce8_create_bib_type_site.py | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 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 91c8de459..f0a4f26f3 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 @@ -36,12 +36,26 @@ def upgrade(): schema=monitorings_schema, ) - 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, + # 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", + # "bib_type_site", + # f"{nomenclature_schema}.check_nomenclature_type_by_mnemonique(id_nomenclature,'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 CHECK ( + {nomenclature_schema}.check_nomenclature_type_by_mnemonique( + id_nomenclature, 'TYPE_SITE' :: character varying + ) + ) NOT VALID + """ ) + op.execute(statement) def downgrade():