ba-thesis/backend/database/migrations/versions/7e5a8cabd3a4_create_device_tables.py

62 lines
1.9 KiB
Python
Raw Normal View History

"""create device tables
Revision ID: 7e5a8cabd3a4
Revises: 335e07a98bc8
Create Date: 2023-05-12 21:59:26.867894
"""
from alembic import op
from sqlalchemy import Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.sql.functions import now
# revision identifiers, used by Alembic.
revision = '7e5a8cabd3a4'
down_revision = '335e07a98bc8'
branch_labels = None
depends_on = down_revision
def upgrade() -> None:
device_models_table = op.create_table(
'device_models',
Column('id', Integer, primary_key=True, autoincrement=True, index=True),
Column('name', String, unique=True, nullable=False),
Column('picture_code', String, unique=True, nullable=False),
)
op.create_table(
'devices',
Column('id', Integer, primary_key=True, autoincrement=True, index=True),
Column('added', DateTime(timezone=True), nullable=False, server_default=now()),
Column('last_seen', DateTime(timezone=True), nullable=True, server_default=None),
Column('model_id', Integer, ForeignKey('device_models.id', ondelete="CASCADE"), nullable=False),
Column('owner_id', Integer, ForeignKey('patients.user_id', ondelete="CASCADE"), nullable=False),
)
# Fill table with known device models.
# Picture codes are generated using `python -c "import uuid; print(uuid.uuid4())"`
op.bulk_insert(
device_models_table,
[
{
"name": "ScanWatch",
"picture_code": "133184d2-3b7c-4589-bc76-9736455a56b9",
},
{
"name": "Thermo",
"picture_code": "3b0cd959-a293-488f-94d6-5f6dc0f775f3",
},
{
"name": "BPM Core",
"picture_code": "36b60fc2-567f-4e7e-b76e-2a9006c0f0fb",
},
]
)
def downgrade() -> None:
op.drop_table('devices')
op.drop_table('device_models')