diff --git a/app/accounts/models.py b/app/accounts/models.py index 6ef058c..8df0985 100644 --- a/app/accounts/models.py +++ b/app/accounts/models.py @@ -12,6 +12,13 @@ class Account(db.Model): email = db.Column(db.String, index=True, unique=True) role_id = db.Column(db.Integer, db.ForeignKey("roles.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): self.username = str(username) diff --git a/app/devices/models.py b/app/devices/models.py index a711d63..07dcb20 100644 --- a/app/devices/models.py +++ b/app/devices/models.py @@ -11,7 +11,7 @@ class Recording(db.Model): default=db.func.current_timestamp()) received_at = db.Column(db.DateTime, index=True, 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_value = db.Column(db.String, nullable=False) raw_record = db.Column(JSON, nullable=True) @@ -36,3 +36,26 @@ class Recording(db.Model): def __repr__(self): return '' % ( 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) diff --git a/migrations/versions/c580745330a9_.py b/migrations/versions/c580745330a9_.py new file mode 100644 index 0000000..5d42e6a --- /dev/null +++ b/migrations/versions/c580745330a9_.py @@ -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 ###