adding manually migrate and change model of device

This commit is contained in:
Cayo Puigdefabregas 2020-10-30 21:08:55 +01:00
parent f7f7432c4e
commit 5ac1e8efba
2 changed files with 47 additions and 0 deletions

View File

@ -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()}')

View File

@ -32,6 +32,7 @@ from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing, listener_reset
from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.user.models import User
class Device(Thing): class Device(Thing):
"""Base class for any type of physical object that can be identified. """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 = db.Column(db.URL)
image.comment = "An image of the device." 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 = { _NON_PHYSICAL_PROPS = {
'id', 'id',
'type', 'type',