From 5ac1e8efba13a7557648caea4f4034c43a9e922d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 30 Oct 2020 21:08:55 +0100 Subject: [PATCH] adding manually migrate and change model of device --- .../68a5c025ab8e_adding_owner_id_in_device.py | 40 +++++++++++++++++++ ereuse_devicehub/resources/device/models.py | 7 ++++ 2 files changed, 47 insertions(+) create mode 100644 ereuse_devicehub/migrations/versions/68a5c025ab8e_adding_owner_id_in_device.py diff --git a/ereuse_devicehub/migrations/versions/68a5c025ab8e_adding_owner_id_in_device.py b/ereuse_devicehub/migrations/versions/68a5c025ab8e_adding_owner_id_in_device.py new file mode 100644 index 00000000..2cac1941 --- /dev/null +++ b/ereuse_devicehub/migrations/versions/68a5c025ab8e_adding_owner_id_in_device.py @@ -0,0 +1,40 @@ +"""adding owner_id in device + +Revision ID: 68a5c025ab8e +Revises: b9b0ee7d9dca +Create Date: 2020-10-30 11:48:34.992498 + +""" +import sqlalchemy as sa +from alembic import context +from alembic import op +from sqlalchemy.dialects import postgresql + + +# revision identifiers, used by Alembic. +revision = '68a5c025ab8e' +down_revision = 'b9b0ee7d9dca' +branch_labels = None +depends_on = None + + +def get_inv(): + # import pdb; pdb.set_trace() + INV = context.get_x_argument(as_dictionary=True).get('inventory') + INV = 'dbtest' + if not INV: + raise ValueError("Inventory value is not specified") + return INV + +def upgrade(): + op.add_column('device', sa.Column('owner_id', postgresql.UUID(), nullable=True), schema=f'{get_inv()}') + op.create_foreign_key("fk_device_owner_id_user_id", + "device", "user", + ["owner_id"], ["id"], + ondelete="SET NULL", + source_schema=f'{get_inv()}', referent_schema='common') + + +def downgrade(): + op.drop_constraint("fk_device_owner_id_user_id", "device", type_="foreignkey", schema=f'{get_inv()}') + op.drop_column('device', 'owner_id', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 682327bb..e478f215 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -32,6 +32,7 @@ from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing, listener_reset from ereuse_devicehub.resources.user.models import User + class Device(Thing): """Base class for any type of physical object that can be identified. @@ -106,6 +107,12 @@ class Device(Thing): image = db.Column(db.URL) image.comment = "An image of the device." + owner_id = db.Column(UUID(as_uuid=True), + db.ForeignKey(User.id), + nullable=False, + default=lambda: g.user.id) + owner = db.relationship(User, primaryjoin=owner_id == User.id) + _NON_PHYSICAL_PROPS = { 'id', 'type',