"""Foreign key concept for data source

Revision ID: 0c56a2478a23
Revises: 7fd1bcffbbc2
Create Date: 2020-05-11 15:45:20.757418

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '0c56a2478a23'
down_revision = '7fd1bcffbbc2'
branch_labels = None
depends_on = None


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('data_source_foreign_key',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('from_source_id', sa.Integer(), nullable=False),
    sa.Column('to_source_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['from_source_id'], ['data_source.id'], ),
    sa.ForeignKeyConstraint(['to_source_id'], ['data_source.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('attribute_foreign_key',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('order', sa.Integer(), nullable=False),
    sa.Column('direction', sa.Enum('FROM', 'TO', name='AttributeForeignKeyDirectionEnumType'), nullable=False),
    sa.Column('foreign_key_id', sa.Integer(), nullable=False),
    sa.Column('from_attribute_id', sa.Integer(), nullable=False),
    sa.Column('to_attribute_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['foreign_key_id'], ['data_source_foreign_key.id'], ),
    sa.ForeignKeyConstraint(['from_attribute_id'], ['attribute.id'], ),
    sa.ForeignKeyConstraint(['to_attribute_id'], ['attribute.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.add_column('attribute', sa.Column('key', sa.Boolean(), nullable=False))
    ### end Alembic commands ###
    op.execute(""" ALTER TABLE data_source CHANGE `format` `format` ENUM(
             'CSV','CUSTOM','GEO_JSON','HAR_IMAGE_FOLDER',
             'HDF5','HIVE', 'DATA_FOLDER','IMAGE_FOLDER','JDBC','JSON','NPY',
             'PARQUET','PICKLE','SAV','SHAPEFILE','TAR_IMAGE_FOLDER','TEXT',
             'VIDEO_FOLDER','UNKNOWN','XML_FILE'
        ) CHARSET utf8 COLLATE utf8_unicode_ci NOT NULL;"""
    ) 
    op.execute("""
        ALTER TABLE `storage` CHANGE `type` `type` ENUM(
            'CASSANDRA','ELASTIC_SEARCH','HDFS','HIVE','JDBC','LOCAL','MONGODB'
            ) CHARSET utf8 COLLATE
            utf8_unicode_ci NOT NULL;""")


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('attribute', 'key')
    op.drop_table('attribute_foreign_key')
    op.drop_table('data_source_foreign_key')

    op.execute(""" ALTER TABLE data_source CHANGE `format` `format` ENUM(
             'CSV','CUSTOM','GEO_JSON','HAR_IMAGE_FOLDER',
             'HDF5','DATA_FOLDER','IMAGE_FOLDER','JDBC','JSON','NETCDF4','NPY',
             'PARQUET','PICKLE','SAV','SHAPEFILE','TAR_IMAGE_FOLDER','TEXT',
             'VIDEO_FOLDER','UNKNOWN','VALLUM','XML_FILE'
        ) CHARSET utf8 COLLATE utf8_unicode_ci NOT NULL;"""
    ) 
    op.execute("""
     ALTER TABLE `storage` CHANGE `type` `type` ENUM(
        'CASSANDRA','ELASTIC_SEARCH','HBASE','HDFS','JDBC','LOCAL','MONGODB','OPHIDIA','POSTGIS','VALLUM'
        ) CHARSET utf8 COLLATE
        utf8_unicode_ci NOT NULL;""",
     )

    # ### end Alembic commands ###
