"""New table structure for authlib.

Revision ID: 42930577deff
Revises: c0687b086ee3
Create Date: 2018-07-14 11:09:54.346801

"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship

# revision identifiers, used by Alembic.
revision = '42930577deff'
down_revision = 'c0687b086ee3'

Base = declarative_base()
db = sa
db.Model = Base
db.relationship = relationship


def create_session():
    connection = op.get_bind()
    session_maker = sa.orm.sessionmaker()
    session = session_maker(bind=connection)
    db.session = session


def upgrade():
    create_session()

    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        'oauth_authorization_code',
        sa.Column('code', sa.String(length=120), nullable=False),
        sa.Column('client_id', sa.String(length=48),
                  nullable=True),
        sa.Column('redirect_uri', sa.Text(), nullable=True),
        sa.Column('response_type', sa.Text(), nullable=True),
        sa.Column('scope', sa.Text(), nullable=True),
        sa.Column('auth_time', sa.Integer(), nullable=False),
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('user_id', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ['user_id'], ['user.id'], name=op.f(
                'fk_oauth_authorization_code_user_id_user'),
            ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id', name=op.f(
            'pk_oauth_authorization_code')),
        sa.UniqueConstraint('code', name=op.f(
            'uq_oauth_authorization_code_code'))
    )
    op.drop_table('oauth_token_scope')
    op.drop_table('oauth_client_scope')
    op.drop_table('oauth_grant_scope')
    op.drop_table('oauth_grant')
    op.drop_table('oauth_client_redirect')
    op.add_column('oauth_client',
                  sa.Column('client_name', sa.String(length=100),
                            nullable=True))
    op.add_column('oauth_client',
                  sa.Column('client_uri', sa.Text(), nullable=True))
    op.add_column('oauth_client',
                  sa.Column('contact', sa.Text(), nullable=True))
    op.add_column('oauth_client',
                  sa.Column('expires_at', sa.Integer(), nullable=False,
                            server_default='0'))
    op.add_column('oauth_client',
                  sa.Column('grant_type', sa.Text(), nullable=False,
                            server_default=''))
    op.add_column('oauth_client',
                  sa.Column('i18n_metadata', sa.Text(), nullable=True))
    op.add_column('oauth_client',
                  sa.Column('issued_at', sa.Integer(), nullable=False,
                            server_default='0'))
    op.add_column('oauth_client',
                  sa.Column('jwks_text', sa.Text(), nullable=True))
    op.add_column('oauth_client',
                  sa.Column('jwks_uri', sa.Text(), nullable=True))
    op.add_column('oauth_client',
                  sa.Column('logo_uri', sa.Text(), nullable=True))
    op.add_column('oauth_client',
                  sa.Column('policy_uri', sa.Text(), nullable=True))
    op.add_column('oauth_client',
                  sa.Column('redirect_uri', sa.Text(), nullable=False,
                            server_default=''))
    op.add_column('oauth_client',
                  sa.Column('response_type', sa.Text(), nullable=False,
                            server_default=''))
    op.add_column('oauth_client',
                  sa.Column('scope', sa.Text(), nullable=False,
                            server_default=''))
    op.add_column('oauth_client',
                  sa.Column('software_id', sa.String(length=36),
                            nullable=True))
    op.add_column('oauth_client',
                  sa.Column('software_version', sa.String(length=48),
                            nullable=True))
    op.add_column('oauth_client',
                  sa.Column('token_endpoint_auth_method', sa.String(length=48),
                            nullable=True))
    op.add_column('oauth_client',
                  sa.Column('tos_uri', sa.Text(), nullable=True))
    op.drop_index('ix_oauth_client_client_secret', table_name='oauth_client')
    op.drop_column('oauth_client', 'confidential')
    op.drop_column('oauth_client', 'name')
    op.drop_column('oauth_client', 'description')
    op.add_column('oauth_token',
                  sa.Column('expires_in', sa.Integer(), nullable=False,
                            server_default='0'))
    op.add_column('oauth_token',
                  sa.Column('issued_at', sa.Integer(), nullable=False,
                            server_default='0'))
    op.add_column('oauth_token',
                  sa.Column('revoked', sa.Boolean(), nullable=True))
    op.add_column('oauth_token', sa.Column('scope', sa.Text(), nullable=True))
    op.alter_column('oauth_token', 'access_token',
                    existing_type=sa.VARCHAR(length=255),
                    nullable=False, server_default='')
    op.alter_column('oauth_token', 'client_id',
                    existing_type=sa.VARCHAR(length=64),
                    nullable=True)
    op.create_index(op.f('ix_oauth_token_refresh_token'), 'oauth_token',
                    ['refresh_token'], unique=False)
    # op.drop_constraint('uq_oauth_token_refresh_token', 'oauth_token',
    #                    type_='unique')
    op.drop_constraint('fk_oauth_token_client_id_oauth_client', 'oauth_token',
                       type_='foreignkey')
    op.drop_column('oauth_token', 'expires')
    # ### end Alembic commands ###


def downgrade():
    create_session()

    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('oauth_token', sa.Column('expires', postgresql.TIMESTAMP(),
                                           autoincrement=False, nullable=True))
    op.create_foreign_key('fk_oauth_token_client_id_oauth_client',
                          'oauth_token', 'oauth_client', ['client_id'],
                          ['client_id'], ondelete='CASCADE')
    op.create_unique_constraint('uq_oauth_token_refresh_token', 'oauth_token',
                                ['refresh_token'])
    op.drop_index(op.f('ix_oauth_token_refresh_token'),
                  table_name='oauth_token')
    op.alter_column('oauth_token', 'client_id',
                    existing_type=sa.VARCHAR(length=64),
                    nullable=False)
    op.alter_column('oauth_token', 'access_token',
                    existing_type=sa.VARCHAR(length=255),
                    nullable=True)
    op.drop_column('oauth_token', 'scope')
    op.drop_column('oauth_token', 'revoked')
    op.drop_column('oauth_token', 'issued_at')
    op.drop_column('oauth_token', 'expires_in')
    op.add_column('oauth_client',
                  sa.Column('description', sa.VARCHAR(length=512),
                            autoincrement=False, nullable=True))
    op.add_column('oauth_client',
                  sa.Column('name', sa.VARCHAR(length=64), autoincrement=False,
                            nullable=True))
    op.add_column('oauth_client',
                  sa.Column('confidential', sa.BOOLEAN(), autoincrement=False,
                            nullable=True))
    op.create_index('ix_oauth_client_client_secret', 'oauth_client',
                    ['client_secret'], unique=True)
    op.drop_column('oauth_client', 'tos_uri')
    op.drop_column('oauth_client', 'token_endpoint_auth_method')
    op.drop_column('oauth_client', 'software_version')
    op.drop_column('oauth_client', 'software_id')
    op.drop_column('oauth_client', 'scope')
    op.drop_column('oauth_client', 'response_type')
    op.drop_column('oauth_client', 'redirect_uri')
    op.drop_column('oauth_client', 'policy_uri')
    op.drop_column('oauth_client', 'logo_uri')
    op.drop_column('oauth_client', 'jwks_uri')
    op.drop_column('oauth_client', 'jwks_text')
    op.drop_column('oauth_client', 'issued_at')
    op.drop_column('oauth_client', 'i18n_metadata')
    op.drop_column('oauth_client', 'grant_type')
    op.drop_column('oauth_client', 'expires_at')
    op.drop_column('oauth_client', 'contact')
    op.drop_column('oauth_client', 'client_uri')
    op.drop_column('oauth_client', 'client_name')
    op.create_table(
        'oauth_client_redirect',
        sa.Column('id', sa.INTEGER(), nullable=False),
        sa.Column('client_id', sa.VARCHAR(length=64),
                  autoincrement=False, nullable=True),
        sa.Column('redirect_uri', sa.VARCHAR(length=256),
                  autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ['client_id'], ['oauth_client.client_id'],
            name='fk_oauth_client_redirect_client_id_oauth_client',
            ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id',
                                name='pk_oauth_client_redirect')
    )
    op.create_table(
        'oauth_grant_scope',
        sa.Column('id', sa.INTEGER(), nullable=False),
        sa.Column('client_id', sa.INTEGER(), autoincrement=False,
                  nullable=True),
        sa.Column('scope', sa.VARCHAR(length=256),
                  autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ['client_id'], ['oauth_grant.id'],
            name='fk_oauth_grant_scope_client_id_oauth_grant',
            ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id', name='pk_oauth_grant_scope')
    )
    op.create_table(
        'oauth_client_scope',
        sa.Column('id', sa.INTEGER(), nullable=False),
        sa.Column('client_id', sa.VARCHAR(length=64),
                  autoincrement=False, nullable=True),
        sa.Column('scope', sa.VARCHAR(length=256),
                  autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ['client_id'], ['oauth_client.client_id'],
            name='fk_oauth_client_scope_client_id_oauth_client',
            ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id', name='pk_oauth_client_scope')
    )
    op.create_table(
        'oauth_token_scope',
        sa.Column('id', sa.INTEGER(), nullable=False),
        sa.Column('token_id', sa.INTEGER(), autoincrement=False,
                  nullable=True),
        sa.Column('scope', sa.VARCHAR(length=256),
                  autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ['token_id'], ['oauth_token.id'],
            name='fk_oauth_token_scope_token_id_oauth_token',
            ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id', name='pk_oauth_token_scope')
    )
    op.create_table(
        'oauth_grant',
        sa.Column('id', sa.INTEGER(), nullable=False),
        sa.Column('user_id', sa.INTEGER(), autoincrement=False,
                  nullable=True),
        sa.Column('client_id', sa.VARCHAR(length=64),
                  autoincrement=False, nullable=False),
        sa.Column('code', sa.VARCHAR(length=255),
                  autoincrement=False, nullable=False),
        sa.Column('redirect_uri', sa.VARCHAR(length=255),
                  autoincrement=False, nullable=True),
        sa.Column('expires', postgresql.TIMESTAMP(),
                  autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(['client_id'],
                                ['oauth_client.client_id'],
                                name='fk_oauth_grant_client_id_oauth_client',
                                ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['user_id'], ['user.id'],
                                name='fk_oauth_grant_user_id_user',
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id', name='pk_oauth_grant')
    )
    op.drop_table('oauth_authorization_code')
    # ### end Alembic commands ###

# vim: ft=python
