Add database models for devices
parent
145ca339c9
commit
b9fb4aa708
|
@ -12,6 +12,13 @@ class Account(db.Model):
|
||||||
email = db.Column(db.String, index=True, unique=True)
|
email = db.Column(db.String, index=True, unique=True)
|
||||||
role_id = db.Column(db.Integer, db.ForeignKey("roles.id"))
|
role_id = db.Column(db.Integer, db.ForeignKey("roles.id"))
|
||||||
role = db.relationship("Role", foreign_keys=[role_id])
|
role = db.relationship("Role", foreign_keys=[role_id])
|
||||||
|
created_at = db.Column(db.DateTime,
|
||||||
|
nullable=False,
|
||||||
|
default=db.func.current_timestamp())
|
||||||
|
modified_at = db.Column(db.DateTime,
|
||||||
|
nullable=False,
|
||||||
|
default=db.func.current_timestamp(),
|
||||||
|
onupdate=db.func.current_timestamp())
|
||||||
|
|
||||||
def __init__(self, username, password, email, role=2):
|
def __init__(self, username, password, email, role=2):
|
||||||
self.username = str(username)
|
self.username = str(username)
|
||||||
|
|
|
@ -11,7 +11,7 @@ class Recording(db.Model):
|
||||||
default=db.func.current_timestamp())
|
default=db.func.current_timestamp())
|
||||||
received_at = db.Column(db.DateTime, index=True,
|
received_at = db.Column(db.DateTime, index=True,
|
||||||
default=db.func.current_timestamp())
|
default=db.func.current_timestamp())
|
||||||
device_id = db.Column(db.Integer)
|
device_id = db.Column(db.Integer, db.ForeignKey('devices.id'))
|
||||||
record_type = db.Column(db.Integer, nullable=False)
|
record_type = db.Column(db.Integer, nullable=False)
|
||||||
record_value = db.Column(db.String, nullable=False)
|
record_value = db.Column(db.String, nullable=False)
|
||||||
raw_record = db.Column(JSON, nullable=True)
|
raw_record = db.Column(JSON, nullable=True)
|
||||||
|
@ -36,3 +36,26 @@ class Recording(db.Model):
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Recording (value=%s, recorded_at=%s)>' % (
|
return '<Recording (value=%s, recorded_at=%s)>' % (
|
||||||
self.record_value, self.recorded_at)
|
self.record_value, self.recorded_at)
|
||||||
|
|
||||||
|
|
||||||
|
class Device(db.Model):
|
||||||
|
__tablename__ = 'devices'
|
||||||
|
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
created_at = db.Column(db.DateTime,
|
||||||
|
nullable=False,
|
||||||
|
default=db.func.current_timestamp())
|
||||||
|
modified_at = db.Column(db.DateTime,
|
||||||
|
nullable=False,
|
||||||
|
default=db.func.current_timestamp(),
|
||||||
|
onupdate=db.func.current_timestamp())
|
||||||
|
name = db.Column(db.String, nullable=False)
|
||||||
|
device_type = db.Column(db.Integer, db.ForeignKey('device_types.id'))
|
||||||
|
configuration = db.Column(JSON, nullable=True)
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceType(db.Model):
|
||||||
|
__tablename__ = 'device_types'
|
||||||
|
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
name = db.Column(db.String, nullable=False)
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: c580745330a9
|
||||||
|
Revises: 7e9220844b2f
|
||||||
|
Create Date: 2018-05-08 10:34:20.757478
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'c580745330a9'
|
||||||
|
down_revision = '7e9220844b2f'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('device_types',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_table('devices',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||||
|
sa.Column('modified_at', sa.DateTime(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.Column('device_type', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('configuration', postgresql.JSON(astext_type=sa.Text()), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['device_type'], ['device_types.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.add_column('accounts', sa.Column('created_at', sa.DateTime(),
|
||||||
|
nullable=True))
|
||||||
|
op.add_column('accounts', sa.Column('modified_at', sa.DateTime(),
|
||||||
|
nullable=True))
|
||||||
|
accounts = sa.sql.table('accounts', sa.sql.column('created_at'), sa.sql.column('modified_at'))
|
||||||
|
op.execute(accounts.update().values(created_at=sa.func.now(), modified_at=sa.func.now()))
|
||||||
|
op.alter_column('accounts', 'created_at', nullable=False)
|
||||||
|
op.alter_column('accounts', 'modified_at', nullable=False)
|
||||||
|
op.execute('DELETE FROM recordings')
|
||||||
|
op.create_foreign_key(None, 'recordings', 'devices', ['device_id'], ['id'])
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_constraint(None, 'recordings', type_='foreignkey')
|
||||||
|
op.drop_column('accounts', 'modified_at')
|
||||||
|
op.drop_column('accounts', 'created_at')
|
||||||
|
op.drop_table('devices')
|
||||||
|
op.drop_table('device_types')
|
||||||
|
# ### end Alembic commands ###
|
Loading…
Reference in New Issue