"""init schema

Revision ID: 467af4a6e6b3
Revises: 
Create Date: 2019-03-11 16:23:21.021301

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '467af4a6e6b3'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    from norm.models.mixins import ARRAY
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('licenses',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=256), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('users',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('first_name', sa.String(length=64), nullable=False),
    sa.Column('last_name', sa.String(length=64), nullable=False),
    sa.Column('username', sa.String(length=64), nullable=False),
    sa.Column('email', sa.String(length=64), nullable=False),
    sa.Column('last_login', sa.DateTime(), nullable=True),
    sa.Column('login_count', sa.Integer(), nullable=True),
    sa.Column('fail_login_count', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    sa.UniqueConstraint('email'),
    sa.UniqueConstraint('username')
    )
    op.create_table('lambdas',
    sa.Column('params', sa.Text(), nullable=True),
    sa.Column('category', sa.String(length=128), nullable=True),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('namespace', sa.String(length=512), nullable=True),
    sa.Column('name', sa.String(length=256), nullable=False),
    sa.Column('dtype', sa.String(length=16), nullable=True),
    sa.Column('adaptable', sa.Boolean(), nullable=True),
    sa.Column('atomic', sa.Boolean(), nullable=True),
    sa.Column('queryable', sa.Boolean(), nullable=True),
    sa.Column('created_by_id', sa.Integer(), nullable=True),
    sa.Column('created_on', sa.DateTime(), nullable=True),
    sa.Column('changed_on', sa.DateTime(), nullable=True),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('anchor', sa.Boolean(), nullable=True),
    sa.Column('cloned_from_id', sa.Integer(), nullable=True),
    sa.Column('merged_from_ids', ARRAY(), nullable=True),
    sa.Column('version', sa.String(), nullable=False),
    sa.Column('current_revision', sa.Integer(), nullable=True),
    sa.Column('status', sa.Enum('DRAFT', 'READY', name='status'), nullable=True),
    sa.Column('license_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['cloned_from_id'], ['lambdas.id'], ),
    sa.ForeignKeyConstraint(['created_by_id'], ['users.id'], ),
    sa.ForeignKeyConstraint(['license_id'], ['licenses.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('revisions',
    sa.Column('params', sa.Text(), nullable=True),
    sa.Column('category', sa.String(length=128), nullable=True),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('query', sa.Text(), nullable=True),
    sa.Column('position', sa.Integer(), nullable=True),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('lambda_id', sa.Integer(), nullable=True),
    sa.Column('renames', sa.JSON(), nullable=True),
    sa.ForeignKeyConstraint(['lambda_id'], ['lambdas.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('variables',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=256), nullable=True),
    sa.Column('primary', sa.Boolean(), nullable=False),
    sa.Column('position', sa.Integer(), nullable=True),
    sa.Column('type_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['type_id'], ['lambdas.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('lambda_variable',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('lambda_id', sa.Integer(), nullable=True),
    sa.Column('variable_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['lambda_id'], ['lambdas.id'], ),
    sa.ForeignKeyConstraint(['variable_id'], ['variables.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('revision_variable',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('revision_id', sa.Integer(), nullable=True),
    sa.Column('variable_id', sa.Integer(), nullable=True),
    sa.ForeignKeyConstraint(['revision_id'], ['revisions.id'], ),
    sa.ForeignKeyConstraint(['variable_id'], ['variables.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('revision_variable')
    op.drop_table('lambda_variable')
    op.drop_table('variables')
    op.drop_table('revisions')
    op.drop_table('lambdas')
    op.drop_table('users')
    op.drop_table('licenses')
    # ### end Alembic commands ###
