From b2918852b6e4f9aaf0382e5410b8202d5e59eaa9 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 27 Sep 2022 12:51:31 +0200 Subject: [PATCH 1/2] add migrations for monitors and mobiles --- .../e919fe0611ff_placeholder_monitors.py | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 ereuse_devicehub/migrations/versions/e919fe0611ff_placeholder_monitors.py diff --git a/ereuse_devicehub/migrations/versions/e919fe0611ff_placeholder_monitors.py b/ereuse_devicehub/migrations/versions/e919fe0611ff_placeholder_monitors.py new file mode 100644 index 00000000..cf7efdbc --- /dev/null +++ b/ereuse_devicehub/migrations/versions/e919fe0611ff_placeholder_monitors.py @@ -0,0 +1,86 @@ +"""placeholder-monitors + +Revision ID: e919fe0611ff +Revises: bcfda54aaf2f +Create Date: 2022-09-27 10:55:00.859262 + +""" +from alembic import context + +from ereuse_devicehub.db import db +from ereuse_devicehub.devicehub import Devicehub +from ereuse_devicehub.resources.device.models import ( + Cellphone, + ComputerMonitor, + Display, + Mobile, + Monitor, + Placeholder, + Smartphone, + Tablet, +) + +# revision identifiers, used by Alembic. +revision = 'e919fe0611ff' +down_revision = 'bcfda54aaf2f' +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 init_app(): + app = Devicehub(inventory=f'{get_inv()}') + app.app_context().push() + + +def clone_monitors(): + devices = [ComputerMonitor, Monitor, Display, Smartphone, Tablet, Cellphone, Mobile] + for dev in devices: + for d in dev.query.all(): + if d.placeholder or d.binding: + continue + clone_device(d) + + +def clone_device(device): + if not device.owner_id: + return + placeholder = Placeholder( + device=device, is_abstract=False, owner_id=device.owner_id + ) + db.session.add(placeholder) + + +def remove_placeholders(): + devices = [ComputerMonitor, Monitor, Display, Smartphone, Tablet, Cellphone, Mobile] + for dev in devices: + for d in dev.query.all(): + if d.placeholder and d.binding: + continue + remove_device(d) + + +def remove_device(device): + if not device.owner_id: + return + placeholder = device.placeholder + db.session.delete(placeholder) + + +def upgrade(): + init_app() + clone_monitors() + db.session.commit() + + +def downgrade(): + pass + # init_app() + # remove_placeholders() + # db.session.commit() From 121012c1f804c04e870bdc0ed6ba94dd00f3fd33 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 27 Sep 2022 13:02:27 +0200 Subject: [PATCH 2/2] add migrations for monitors and mobiles --- .../versions/e919fe0611ff_placeholder_monitors.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/migrations/versions/e919fe0611ff_placeholder_monitors.py b/ereuse_devicehub/migrations/versions/e919fe0611ff_placeholder_monitors.py index cf7efdbc..ff4c0515 100644 --- a/ereuse_devicehub/migrations/versions/e919fe0611ff_placeholder_monitors.py +++ b/ereuse_devicehub/migrations/versions/e919fe0611ff_placeholder_monitors.py @@ -5,7 +5,7 @@ Revises: bcfda54aaf2f Create Date: 2022-09-27 10:55:00.859262 """ -from alembic import context +from alembic import context, op from ereuse_devicehub.db import db from ereuse_devicehub.devicehub import Devicehub @@ -74,13 +74,22 @@ def remove_device(device): def upgrade(): + con = op.get_bind() + devices = con.execute(f'select * from {get_inv()}.device') + if not list(devices): + return + init_app() clone_monitors() db.session.commit() def downgrade(): - pass + con = op.get_bind() + devices = con.execute(f'select * from {get_inv()}.device') + if not list(devices): + return + # init_app() # remove_placeholders() # db.session.commit()