From f19046b2ef0ef06e62f6f4319fdeea667a7e845d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 7 Sep 2022 14:04:09 +0200 Subject: [PATCH] add backups for the abstract phid and dhid --- ereuse_devicehub/inventory/views.py | 16 ++++++++ .../versions/6b0880832b78_backup_dhid.py | 41 +++++++++++++++++++ ereuse_devicehub/resources/device/models.py | 2 + 3 files changed, 59 insertions(+) create mode 100644 ereuse_devicehub/migrations/versions/6b0880832b78_backup_dhid.py diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 8b65e37a..4c1b38dd 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -279,6 +279,11 @@ class BindingView(GenericMixin): self.abstract_dhid = self.old_device.devicehub_id self.abstract_phid = self.old_placeholder.phid + # to do a backup of abstract_dhid and abstract_phid in + # workbench device + self.abstract_device.dhid_bk = self.abstract_dhid + self.abstract_device.phid_bk = self.abstract_phid + def post(self): for plog in PlaceholdersLog.query.filter_by( placeholder_id=self.old_placeholder.id @@ -379,6 +384,17 @@ class UnBindingView(GenericMixin): c.binding.device.parent = new_device placeholder = Placeholder(device=new_device, binding=device, is_abstract=True) + if ( + device.dhid_bk + and not Device.query.filter_by(devicehub_id=device.dhid_bk).first() + ): + new_device.devicehub_id = device.dhid_bk + if ( + device.phid_bk + and not Placeholder.query.filter_by(phid=device.phid_bk).first() + ): + placeholder.phid = device.phid_bk + db.session.add(placeholder) db.session.commit() diff --git a/ereuse_devicehub/migrations/versions/6b0880832b78_backup_dhid.py b/ereuse_devicehub/migrations/versions/6b0880832b78_backup_dhid.py new file mode 100644 index 00000000..f79085f4 --- /dev/null +++ b/ereuse_devicehub/migrations/versions/6b0880832b78_backup_dhid.py @@ -0,0 +1,41 @@ +"""backup dhid + +Revision ID: 6b0880832b78 +Revises: d7ea9a3b2da1 +Create Date: 2022-09-07 12:53:25.827186 + +""" +import citext +import sqlalchemy as sa +from alembic import context, op + +# revision identifiers, used by Alembic. +revision = '6b0880832b78' +down_revision = 'd7ea9a3b2da1' +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.add_column( + 'device', + sa.Column('dhid_bk', citext.CIText(), unique=False, nullable=True), + schema=f'{get_inv()}', + ) + op.add_column( + 'device', + sa.Column('phid_bk', citext.CIText(), unique=False, nullable=True), + schema=f'{get_inv()}', + ) + + +def downgrade(): + op.drop_column('device', 'dhid_bk', schema=f'{get_inv()}') + op.drop_column('device', 'phid_bk', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index f37ef586..01845ca7 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -173,6 +173,8 @@ class Device(Thing): db.CIText(), nullable=True, unique=True, default=create_code ) devicehub_id.comment = "device have a unique code." + dhid_bk = db.Column(db.CIText(), nullable=True, unique=False) + phid_bk = db.Column(db.CIText(), nullable=True, unique=False) active = db.Column(Boolean, default=True) _NON_PHYSICAL_PROPS = {