diff --git a/ereuse_devicehub/migrations/versions/926865284103_snapshot_log.py b/ereuse_devicehub/migrations/versions/926865284103_snapshot_log.py new file mode 100644 index 00000000..7dc29c75 --- /dev/null +++ b/ereuse_devicehub/migrations/versions/926865284103_snapshot_log.py @@ -0,0 +1,71 @@ +"""snapshot_log + +Revision ID: 926865284103 +Revises: 6f6771813f2e +Create Date: 2022-05-17 17:57:46.651106 + +""" +import citext +import sqlalchemy as sa +from alembic import context, op +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = '926865284103' +down_revision = '6f6771813f2e' +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( + 'snapshots_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('description', citext.CIText(), nullable=True), + sa.Column('workbench_version', citext.CIText(), nullable=True), + sa.Column('sid', citext.CIText(), nullable=True), + sa.Column('severity', sa.SmallInteger(), nullable=False), + sa.Column('snapshot_uuid', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('snapshot_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('owner_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.ForeignKeyConstraint( + ['snapshot_id'], + [f'{get_inv()}.snapshot.id'], + ), + sa.ForeignKeyConstraint( + ['owner_id'], + ['common.user.id'], + ), + sa.PrimaryKeyConstraint('id'), + schema=f'{get_inv()}', + ) + op.execute(f"CREATE SEQUENCE {get_inv()}.snapshots_log_seq START 1;") + + op.drop_table('snapshot_errors', schema=f'{get_inv()}') + op.execute(f"DROP SEQUENCE {get_inv()}.snapshot_errors_seq;") + + +def downgrade(): + op.drop_table('snapshots_log', schema=f'{get_inv()}') + op.execute(f"DROP SEQUENCE {get_inv()}.snapshots_log_seq;") diff --git a/ereuse_devicehub/parser/models.py b/ereuse_devicehub/parser/models.py index 54f02461..a2621daa 100644 --- a/ereuse_devicehub/parser/models.py +++ b/ereuse_devicehub/parser/models.py @@ -4,25 +4,29 @@ from sqlalchemy import BigInteger, Column, Sequence, SmallInteger from sqlalchemy.dialects.postgresql import UUID from ereuse_devicehub.db import db +from ereuse_devicehub.resources.action.models import Snapshot from ereuse_devicehub.resources.enums import Severity from ereuse_devicehub.resources.models import Thing from ereuse_devicehub.resources.user.models import User -class SnapshotErrors(Thing): +class SnapshotsLog(Thing): """A Snapshot errors.""" - id = Column(BigInteger, Sequence('snapshot_errors_seq'), primary_key=True) - description = Column(CIText(), default='', nullable=False) - sid = Column(CIText(), nullable=True) + id = Column(BigInteger, Sequence('snapshots_log_seq'), primary_key=True) severity = Column(SmallInteger, default=Severity.Info, nullable=False) - snapshot_uuid = Column(UUID(as_uuid=True), nullable=False) + workbench_version = Column(CIText(), default='', nullable=True) + description = Column(CIText(), default='', nullable=True) + sid = Column(CIText(), nullable=True) + dhid = Column(CIText(), nullable=True) + snapshot_id = Column(UUID(as_uuid=True), db.ForeignKey(User.id), nullable=True) owner_id = db.Column( UUID(as_uuid=True), db.ForeignKey(User.id), nullable=False, default=lambda: g.user.id, ) + snapshot = db.relationship(User, primaryjoin=snapshot_id == Snapshot.id) owner = db.relationship(User, primaryjoin=owner_id == User.id) def save(self, commit=False):