From fa55ac017d42de3d46dd96ee0d6f4b78b9f8583b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 2 Aug 2023 17:16:55 +0200 Subject: [PATCH] fix check_constraint in database --- ereuse_devicehub/migrations/script.py.mako | 2 +- ..._reset_check_constraint_of_placeholders.py | 65 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 ereuse_devicehub/migrations/versions/57e6201f280c_reset_check_constraint_of_placeholders.py diff --git a/ereuse_devicehub/migrations/script.py.mako b/ereuse_devicehub/migrations/script.py.mako index cabe65db..b3bec959 100644 --- a/ereuse_devicehub/migrations/script.py.mako +++ b/ereuse_devicehub/migrations/script.py.mako @@ -5,7 +5,7 @@ Revises: ${down_revision | comma,n} Create Date: ${create_date} """ -from alembic import op +from alembic import op, context import sqlalchemy as sa import sqlalchemy_utils import citext diff --git a/ereuse_devicehub/migrations/versions/57e6201f280c_reset_check_constraint_of_placeholders.py b/ereuse_devicehub/migrations/versions/57e6201f280c_reset_check_constraint_of_placeholders.py new file mode 100644 index 00000000..ab55de57 --- /dev/null +++ b/ereuse_devicehub/migrations/versions/57e6201f280c_reset_check_constraint_of_placeholders.py @@ -0,0 +1,65 @@ +"""reset check_constraint of placeholders + +Revision ID: 57e6201f280c +Revises: 8ccba3cb37c2 +Create Date: 2023-08-02 15:56:12.484340 + +""" +import sqlalchemy as sa +from alembic import context, op + +# revision identifiers, used by Alembic. +revision = '57e6201f280c' +down_revision = '8ccba3cb37c2' +branch_labels = None +depends_on = None + + +def get_inv(): + INV = context.get_x_argument(as_dictionary=True).get('inventory') + if not INV: + raise ValueError("Inventory value is not specified") + return INV + + +def upgrade(): + op.drop_constraint( + 'device_depth_check', "device", type_="check", schema=f'{get_inv()}' + ) + op.drop_constraint( + 'device_height_check', "device", type_="check", schema=f'{get_inv()}' + ) + op.drop_constraint( + 'device_width_check', "device", type_="check", schema=f'{get_inv()}' + ) + op.drop_constraint( + 'device_weight_check', "device", type_="check", schema=f'{get_inv()}' + ) + op.create_check_constraint( + "device_depth_check", + "device", + sa.Column("depth") >= (0.1), + schema=f'{get_inv()}', + ) + op.create_check_constraint( + "device_height_check", + "device", + sa.Column("depth") >= (0.1), + schema=f'{get_inv()}', + ) + op.create_check_constraint( + "device_width_check", + "device", + sa.Column("depth") >= (0.1), + schema=f'{get_inv()}', + ) + op.create_check_constraint( + "device_weight_check", + "device", + sa.Column("depth") >= (0.1), + schema=f'{get_inv()}', + ) + + +def downgrade(): + pass