# pylint: disable=invalid-name
"""Version Groups and Versions

Revision ID: 317f62cffc9a
Revises: 763b433aaf4a
Create Date: 2018-09-20 07:27:20.649971

"""
from alembic import op

import sqlalchemy as sa

from ttgn.pokedex.migrations import if_x_argument
from ttgn.pokedex.migrations.data import load_data_migrations

# revision identifiers, used by Alembic.
revision = '317f62cffc9a'
down_revision = '763b433aaf4a'
branch_labels = None
depends_on = None


def upgrade():
    """Perform upgrade migration."""
    if not if_x_argument('no-schema', False):
        schema_upgrade()

    if not if_x_argument('no-data', False):
        data_upgrade()


def downgrade():
    """Perform downgrade migration."""
    if not if_x_argument('no-data', False):
        data_downgrade()

    if not if_x_argument('no-schema', False):
        schema_downgrade()


def schema_upgrade():
    """Perform schema upgrade."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        'ttgn_pokedex_models_versions_version_groups',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('generation_id', sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ['generation_id'],
            ['ttgn_pokedex_models_versions_generations.id'],
        ), sa.PrimaryKeyConstraint('id'))
    op.create_table(
        'ttgn_pokedex_models_versions_versions',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('version_group_id', sa.Integer(), nullable=False),
        sa.Column('acronym', sa.String(length=2), nullable=False),
        sa.ForeignKeyConstraint(
            ['version_group_id'],
            ['ttgn_pokedex_models_versions_version_groups.id'],
        ), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('acronym'))
    # ### end Alembic commands ###


def schema_downgrade():
    """Perform schema downgrade."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('ttgn_pokedex_models_versions_versions')
    op.drop_table('ttgn_pokedex_models_versions_version_groups')
    # ### end Alembic commands ###


def data_upgrade():
    """Perform data upgrade."""
    try:
        load_data_migrations(revision, 'upgrade')
    except Exception:
        data_downgrade()
        schema_downgrade()
        raise


def data_downgrade():
    """Perform data downgrade."""
    load_data_migrations(revision, 'downgrade')
