From 12d55c2d24e1c8f1f4129bd7472da48f8ca433bf Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 28 Jun 2022 17:38:05 +0200 Subject: [PATCH] add models --- .../versions/aeca9fb50cc6_add_placeholder.py | 60 +++++++++++++++++++ ereuse_devicehub/resources/device/models.py | 23 ++++--- 2 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 ereuse_devicehub/migrations/versions/aeca9fb50cc6_add_placeholder.py diff --git a/ereuse_devicehub/migrations/versions/aeca9fb50cc6_add_placeholder.py b/ereuse_devicehub/migrations/versions/aeca9fb50cc6_add_placeholder.py new file mode 100644 index 00000000..0eef82bd --- /dev/null +++ b/ereuse_devicehub/migrations/versions/aeca9fb50cc6_add_placeholder.py @@ -0,0 +1,60 @@ +"""add placeholder + +Revision ID: aeca9fb50cc6 +Revises: 8d4fe4b497b3 +Create Date: 2022-06-27 13:09:30.497678 + +""" +import citext +import sqlalchemy as sa +from alembic import context, op + +# revision identifiers, used by Alembic. +revision = 'aeca9fb50cc6' +down_revision = '8d4fe4b497b3' +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(): + # creating placeholder table + # con = op.get_bind() + # sql = f"CREATE SEQUENCE {get_inv()}.placeholder_id_seq START 1;" + # con.execute(sql) + + op.create_table( + 'placeholder', + sa.Column( + 'updated', + sa.TIMESTAMP(timezone=True), + server_default=sa.text('CURRENT_TIMESTAMP'), + nullable=False, + ), + sa.Column( + 'created', + sa.TIMESTAMP(timezone=True), + server_default=sa.text('CURRENT_TIMESTAMP'), + nullable=False, + ), + sa.Column('id', sa.BigInteger(), nullable=False), + sa.Column('id_device_supplier', sa.Unicode(), nullable=True), + sa.Column('pallet', sa.Unicode(), nullable=True), + sa.Column('info', citext.CIText(), nullable=True), + sa.Column('device_id', sa.BigInteger(), nullable=False), + sa.Column('binding_id', sa.BigInteger(), nullable=True), + sa.ForeignKeyConstraint(['device_id'], [f'{get_inv()}.device.id']), + sa.ForeignKeyConstraint(['binding_id'], [f'{get_inv()}.device.id']), + sa.PrimaryKeyConstraint('id'), + schema=f'{get_inv()}', + ) + + +def downgrade(): + op.drop_table('placeholder', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 003e0f3d..30c16c5b 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -793,8 +793,7 @@ class DisplayMixin: class Placeholder(Thing): id = Column(BigInteger, Sequence('placeholder_seq'), primary_key=True) - phid = Column(Unicode(), check_lower('phid'), unique=False) - pallet = Column(BigInteger, nullable=True) + pallet = Column(Unicode(), nullable=True) pallet.comment = "used for identification where from where is this placeholders" info = db.Column(CIText()) info.comment = "more info of placeholders" @@ -803,29 +802,29 @@ class Placeholder(Thing): "Identification used for one supplier of one placeholders" ) - placeholder_id = db.Column( + device_id = db.Column( BigInteger, db.ForeignKey(Device.id), nullable=False, ) - placeholder = db.relationship( + device = db.relationship( Device, - backref=backref('placeholder', lazy=True), - primaryjoin=placeholder_id == Device.id, + backref=backref('placeholder', lazy=True, uselist=False), + primaryjoin=device_id == Device.id, ) - placeholder_id.comment = "datas of the placeholder" + device_id.comment = "datas of the placeholder" - device_id = db.Column( + binding_id = db.Column( BigInteger, db.ForeignKey(Device.id), nullable=True, ) - device = db.relationship( + binding = db.relationship( Device, - backref=backref('binding', lazy=True), - primaryjoin=placeholder_id == Device.id, + backref=backref('binding', lazy=True, uselist=False), + primaryjoin=binding_id == Device.id, ) - device_id.comment = "device with snapshots than is linked to the placeholder" + binding_id.comment = "device with snapshots than is linked to the placeholder" class Computer(Device):