"""
Copyright (c) 2015 Michael Bright and Bamboo HR LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

qa_products

Revision ID: e66b5abaa8c2
Revises: 593023a41b6d
Create Date: 2016-10-18 02:27:03.604303

"""

# revision identifiers, used by Alembic.
from rapid.qa.data.models import QaTestType

revision = 'e66b5abaa8c2'
down_revision = '593023a41b6d'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    op.create_table('qa_test_types',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=100), nullable=False),
    sa.Column('active', sa.Boolean(), nullable=True),
    sa.PrimaryKeyConstraint('id'),
    mysql_engine='InnoDB'
    )
    op.create_index(op.f('ix_qa_test_types_id'), 'qa_test_types', ['id'], unique=False)

    op.create_table('qa_products',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(length=250), nullable=False),
    sa.Column('vcs_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['vcs_id'], ['vcs.id'], ),
    sa.PrimaryKeyConstraint('id'),
    mysql_engine='InnoDB'
    )
    op.create_index(op.f('ix_qa_products_id'), 'qa_products', ['id'], unique=False)
    op.create_index(op.f('ix_qa_products_name'), 'qa_products', ['name'], unique=False)
    op.create_index(op.f('ix_qa_products_vcs_id'), 'qa_products', ['vcs_id'], unique=False)

    op.add_column('qa_areas', sa.Column('product_id', sa.Integer(), nullable=True))
    op.create_index(op.f('ix_qa_areas_product_id'), 'qa_areas', ['product_id'], unique=False)

    if 'sqlite' != op.get_context().dialect.name:
        op.create_foreign_key(None, 'qa_areas', 'qa_products', ['product_id'], ['id'])

    op.add_column('qa_behavior_points', sa.Column('product_id', sa.Integer(), nullable=True))
    op.create_index(op.f('ix_qa_behavior_points_product_id'), 'qa_behavior_points', ['product_id'], unique=False)

    if 'sqlite' != op.get_context().dialect.name:
        op.create_foreign_key(None, 'qa_behavior_points', 'qa_products', ['product_id'], ['id'])

    op.add_column('qa_features', sa.Column('product_id', sa.Integer(), nullable=True))
    op.create_index(op.f('ix_qa_features_product_id'), 'qa_features', ['product_id'], unique=False)

    if 'sqlite' != op.get_context().dialect.name:
        op.create_foreign_key(None, 'qa_features', 'qa_products', ['product_id'], ['id'])

    if 'sqlite' != op.get_context().dialect.name:
        op.alter_column('qa_test_mappings', 'feature_id',
                   existing_type=sa.INTEGER(),
                   nullable=True)
    op.create_index(op.f('ix_qa_test_mappings_area_id'), 'qa_test_mappings', ['area_id'], unique=False)
    op.create_index(op.f('ix_qa_test_mappings_behavior_id'), 'qa_test_mappings', ['behavior_id'], unique=False)
    op.create_index(op.f('ix_qa_test_mappings_feature_id'), 'qa_test_mappings', ['feature_id'], unique=False)

    op.add_column('qa_tests', sa.Column('qa_test_type_id', sa.Integer(), nullable=True))
    op.create_index(op.f('ix_qa_tests_qa_test_type_id'), 'qa_tests', ['qa_test_type_id'], unique=False)

    if 'sqlite' != op.get_context().dialect.name:
        op.create_foreign_key(None, 'qa_tests', 'qa_test_types', ['qa_test_type_id'], ['id'])

    op.create_index(op.f('ix_stage_instances_created_date'), 'stage_instances', ['created_date'], unique=False)
    op.create_index(op.f('ix_stage_instances_end_date'), 'stage_instances', ['end_date'], unique=False)
    op.create_index(op.f('ix_stage_instances_start_date'), 'stage_instances', ['start_date'], unique=False)

    op.create_index(op.f('ix_workflow_instances_created_date'), 'workflow_instances', ['created_date'], unique=False)
    op.create_index(op.f('ix_workflow_instances_end_date'), 'workflow_instances', ['end_date'], unique=False)
    op.create_index(op.f('ix_workflow_instances_start_date'), 'workflow_instances', ['start_date'], unique=False)
    ### end Alembic commands ###

    op.bulk_insert(QaTestType.__table__, [
        {"id": 1, "name": "Unit", "active": True},
        {"id": 2, "name": "Integration", "active": True},
        {"id": 3, "name": "Selenium", "active": True},
        {"id": 4, "name": "Smoke", "active": True},
    ])


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_workflow_instances_start_date'), table_name='workflow_instances')
    op.drop_index(op.f('ix_workflow_instances_end_date'), table_name='workflow_instances')
    op.drop_index(op.f('ix_workflow_instances_created_date'), table_name='workflow_instances')

    op.drop_index(op.f('ix_stage_instances_start_date'), table_name='stage_instances')
    op.drop_index(op.f('ix_stage_instances_end_date'), table_name='stage_instances')
    op.drop_index(op.f('ix_stage_instances_created_date'), table_name='stage_instances')

    op.drop_constraint(None, 'qa_tests', type_='foreignkey')
    op.drop_index(op.f('ix_qa_tests_qa_test_type_id'), table_name='qa_tests')
    op.drop_column('qa_tests', 'qa_test_type_id')

    op.drop_index(op.f('ix_qa_test_mappings_feature_id'), table_name='qa_test_mappings')
    op.drop_index(op.f('ix_qa_test_mappings_behavior_id'), table_name='qa_test_mappings')
    op.drop_index(op.f('ix_qa_test_mappings_area_id'), table_name='qa_test_mappings')

    if 'sqlite' != op.get_context().dialect.name:
        op.alter_column('qa_test_mappings', 'feature_id',
                   existing_type=sa.INTEGER(),
                   nullable=False)

    op.drop_constraint(None, 'qa_features', type_='foreignkey')
    op.drop_index(op.f('ix_qa_features_product_id'), table_name='qa_features')
    op.drop_column('qa_features', 'product_id')

    op.drop_constraint(None, 'qa_behavior_points', type_='foreignkey')
    op.drop_index(op.f('ix_qa_behavior_points_product_id'), table_name='qa_behavior_points')
    op.drop_column('qa_behavior_points', 'product_id')

    op.drop_constraint(None, 'qa_areas', type_='foreignkey')
    op.drop_index(op.f('ix_qa_areas_product_id'), table_name='qa_areas')
    op.drop_column('qa_areas', 'product_id')

    op.drop_index(op.f('ix_qa_products_vcs_id'), table_name='qa_products')
    op.drop_index(op.f('ix_qa_products_name'), table_name='qa_products')
    op.drop_index(op.f('ix_qa_products_id'), table_name='qa_products')
    op.drop_table('qa_products')

    op.drop_index(op.f('ix_qa_test_types_id'), table_name='qa_test_types')
    op.drop_table('qa_test_types')
    ### end Alembic commands ###
