add placeholder log models

This commit is contained in:
Cayo Puigdefabregas 2022-07-06 18:37:27 +02:00
parent 3af2645706
commit d93f040453
2 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,63 @@
"""placeholder log
Revision ID: 3e3a67f62972
Revises: aeca9fb50cc6
Create Date: 2022-07-06 18:23:54.267003
"""
import citext
import sqlalchemy as sa
from alembic import context, op
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = '3e3a67f62972'
down_revision = 'aeca9fb50cc6'
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.create_table(
'placeholders_log',
sa.Column(
'updated',
sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
comment='The last time Devicehub recorded a change for \n this thing.\n ',
),
sa.Column(
'created',
sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
comment='When Devicehub created this.',
),
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('source', citext.CIText(), nullable=True),
sa.Column('type', citext.CIText(), nullable=True),
sa.Column('status', sa.SmallInteger(), nullable=True),
sa.Column('placeholder_id', sa.BigInteger(), nullable=True),
sa.Column('owner_id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(
['placeholder_id'],
[f'{get_inv()}.placeholder.id'],
),
sa.ForeignKeyConstraint(
['owner_id'],
['common.user.id'],
),
sa.PrimaryKeyConstraint('id'),
)
def downgrade():
op.drop_table('placeholders_log')

View File

@ -5,6 +5,7 @@ from sqlalchemy.dialects.postgresql import UUID
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.resources.action.models import Snapshot from ereuse_devicehub.resources.action.models import Snapshot
from ereuse_devicehub.resources.device.models import Placeholder
from ereuse_devicehub.resources.enums import Severity from ereuse_devicehub.resources.enums import Severity
from ereuse_devicehub.resources.models import Thing from ereuse_devicehub.resources.models import Thing
from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.user.models import User
@ -43,3 +44,46 @@ class SnapshotsLog(Thing):
return self.snapshot.device.devicehub_id return self.snapshot.device.devicehub_id
return '' return ''
class PlaceholdersLog(Thing):
"""A Placeholder log."""
id = Column(BigInteger, Sequence('snapshots_log_seq'), primary_key=True)
source = Column(CIText(), default='', nullable=True)
type = Column(CIText(), default='', nullable=True)
status = Column(CIText(), default='', nullable=True)
placeholder_id = Column(
UUID(as_uuid=True), db.ForeignKey(Snapshot.id), nullable=True
)
placeholder = db.relationship(
Snapshot, primaryjoin=placeholder_id == Placeholder.id
)
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)
def save(self, commit=False):
db.session.add(self)
if commit:
db.session.commit()
@property
def phid(self):
if self.placeholder:
return self.placeholder.phid
return ''
@property
def dhid(self):
if self.placeholder:
return self.placeholder.device.dhid
return ''