This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/migrations/versions/21afd375a654_session_table.py

63 lines
2.5 KiB
Python
Raw Permalink Normal View History

2021-04-13 16:33:15 +00:00
"""session_table
Revision ID: 21afd375a654
Revises: 6a2a939d5668
Create Date: 2021-04-13 11:18:27.720567
"""
from alembic import context
from alembic import op
from sqlalchemy.dialects import postgresql
import sqlalchemy as sa
import sqlalchemy_utils
import citext
import teal
from ereuse_devicehub.resources.enums import SessionType
# revision identifiers, used by Alembic.
revision = '21afd375a654'
2021-05-13 15:27:19 +00:00
down_revision = '398826453b39'
2021-04-13 16:33:15 +00:00
branch_labels = None
depends_on = None
2021-04-15 19:18:15 +00:00
comment_update = 'The last time Devicehub recorded a change for this thing.\n'
2021-04-13 16:33:15 +00:00
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
2021-04-15 19:18:15 +00:00
2021-04-13 16:33:15 +00:00
def upgrade():
op.create_table('session',
2021-04-15 19:18:15 +00:00
sa.Column('updated', sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False,
comment=comment_update),
sa.Column('created', sa.TIMESTAMP(timezone=True),
server_default=sa.text('CURRENT_TIMESTAMP'),
nullable=False, comment='When Devicehub created this.'),
2021-04-13 16:33:15 +00:00
sa.Column('id', sa.BigInteger(), nullable=False),
sa.Column('expired', sa.BigInteger(), nullable=True),
sa.Column('token', postgresql.UUID(as_uuid=True), nullable=False),
sa.Column('type', teal.db.IntEnum(SessionType), nullable=False),
sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['common.user.id'], ),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('token'),
schema='common'
)
op.create_index(op.f('ix_session_created'), 'session', ['created'], unique=False, schema='common')
op.create_index(op.f('ix_session_updated'), 'session', ['updated'], unique=False, schema='common')
2021-04-15 19:18:15 +00:00
op.create_index(op.f('ix_session_token'), 'session', ['token'], unique=True, schema='common')
2021-04-13 16:33:15 +00:00
def downgrade():
op.drop_table('trade', schema=f'{get_inv()}')
op.drop_index(op.f('ix_session_created'), table_name='session', schema='common')
op.drop_index(op.f('ix_session_updated'), table_name='session', schema='common')
2021-04-15 19:18:15 +00:00
op.drop_index(op.f('ix_session_token'), table_name='session', schema='common')