From 9ea0a05de6119a9de062ba5aa492f9881def05cc Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 5 Oct 2021 14:00:55 +0200 Subject: [PATCH] migration file --- .../8571fb32c912_adding_active_in_device.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 ereuse_devicehub/migrations/versions/8571fb32c912_adding_active_in_device.py diff --git a/ereuse_devicehub/migrations/versions/8571fb32c912_adding_active_in_device.py b/ereuse_devicehub/migrations/versions/8571fb32c912_adding_active_in_device.py new file mode 100644 index 00000000..91a746e4 --- /dev/null +++ b/ereuse_devicehub/migrations/versions/8571fb32c912_adding_active_in_device.py @@ -0,0 +1,43 @@ +"""adding active in device + +Revision ID: 8571fb32c912 +Revises: 3ac2bc1897ce +Create Date: 2021-10-05 12:27:09.685227 + +""" +from alembic import op, context +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '8571fb32c912' +down_revision = '3ac2bc1897ce' +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_data(): + con = op.get_bind() + sql = f"update {get_inv()}.device set active='t';" + con.execute(sql) + + +def upgrade(): + op.add_column('device', sa.Column('active', sa.Boolean(), + default=True, + nullable=True), + schema=f'{get_inv()}') + + upgrade_data() + op.alter_column('device', 'active', nullable=False, schema=f'{get_inv()}') + + +def downgrade(): + op.drop_column('device', 'active', schema=f'{get_inv()}')