"""empty message

Revision ID: 2018_04_20_data_src_refactor
Revises: 2018_04_11_add_sandbox_topic
Create Date: 2018-04-20 13:03:32.478880

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
from sqlalchemy.dialects.postgresql import ARRAY

revision = '2018_04_20_data_src_refactor'
down_revision = '2018_04_11_add_sandbox_topic'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###

    type_of_data_types = sa.Enum('ADMINISTRATIVE', 'SURVEY', name='type_of_data_types')
    op.add_column('page', sa.Column('secondary_source_1_type_of_data', ARRAY(type_of_data_types), nullable=True))
    op.add_column('page', sa.Column('suppression_and_disclosure', sa.TEXT(), nullable=True))
    op.add_column('page', sa.Column('note_on_corrections_or_updates', sa.TEXT(), nullable=True))
    op.add_column('page', sa.Column('secondary_source_1_note_on_corrections_or_updates', sa.TEXT(), nullable=True))
    op.add_column('page', sa.Column('secondary_source_1_data_source_purpose', sa.TEXT(), nullable=True))

    op.get_bind()

    op.execute('''
        UPDATE page SET suppression_and_disclosure = suppression_rules 
        WHERE disclosure_control is null;
    ''')

    op.execute('''
           UPDATE page SET suppression_and_disclosure = disclosure_control 
           WHERE suppression_rules is null;
       ''')

    op.execute('''
        UPDATE page SET suppression_and_disclosure = trim(suppression_rules  || '  ' ||  disclosure_control)
        WHERE suppression_rules is not null
        AND  disclosure_control is not null;
    ''')

    op.drop_constraint('organisation_secondary_source_2_fkey', 'page', type_='foreignkey')
    op.drop_constraint('frequency_secondary_source_2_fkey', 'page', type_='foreignkey')
    op.drop_constraint('secondary_source_2_type_of_statistic_fkey', 'page', type_='foreignkey')
    op.drop_column('page', 'secondary_source_1_date_next_update')
    op.drop_column('page', 'secondary_source_1_date_updated')
    op.drop_column('page', 'secondary_source_1_suppression_rules')
    op.drop_column('page', 'secondary_source_1_disclosure_control')
    op.drop_column('page', 'secondary_source_2_frequency')
    op.drop_column('page', 'secondary_source_2_contact_2_name')
    op.drop_column('page', 'secondary_source_2_contact_2_phone')
    op.drop_column('page', 'secondary_source_2_url')
    op.drop_column('page', 'secondary_source_2_date_next_update')
    op.drop_column('page', 'secondary_source_2_contact_1_name')
    op.drop_column('page', 'last_update_date')
    op.drop_column('page', 'secondary_source_2_contact_1_phone')
    op.drop_column('page', 'secondary_source_2_publisher_text')
    op.drop_column('page', 'secondary_source_2_disclosure_control')
    op.drop_column('page', 'secondary_source_2_type_of_statistic_id')
    op.drop_column('page', 'secondary_source_2_suppression_rules')
    op.drop_column('page', 'secondary_source_2_frequency_other')
    op.drop_column('page', 'secondary_source_2_publisher_id')
    op.drop_column('page', 'secondary_source_2_title')
    op.drop_column('page', 'secondary_source_2_date')
    op.drop_column('page', 'next_update_date')
    op.drop_column('page', 'secondary_source_2_date_updated')
    op.drop_column('page', 'secondary_source_2_statistic_type')
    op.drop_column('page', 'secondary_source_2_frequency_id')
    op.drop_column('page', 'secondary_source_2_contact_2_email')
    op.drop_column('page', 'secondary_source_2_contact_1_email')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('page', sa.Column('secondary_source_2_contact_1_email', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_contact_2_email', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_frequency_id', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_statistic_type', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_date_updated', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_date', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_title', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_publisher_id', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_frequency_other', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_suppression_rules', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_type_of_statistic_id', sa.INTEGER(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_disclosure_control', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_publisher_text', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_contact_1_phone', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_contact_1_name', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_date_next_update', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_url', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_contact_2_phone', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_contact_2_name', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_2_frequency', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('last_update_date', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('next_update_date', sa.VARCHAR(length=255), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_1_date_next_update', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_1_date_updated', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_1_disclosure_control', sa.TEXT(), autoincrement=False, nullable=True))
    op.add_column('page', sa.Column('secondary_source_1_suppression_rules', sa.TEXT(), autoincrement=False, nullable=True))

    op.create_foreign_key('secondary_source_2_type_of_statistic_fkey', 'page', 'type_of_statistic', ['secondary_source_2_type_of_statistic_id'], ['id'])
    op.create_foreign_key('frequency_secondary_source_2_fkey', 'page', 'frequency_of_release', ['secondary_source_2_frequency_id'], ['id'])
    op.create_foreign_key('organisation_secondary_source_2_fkey', 'page', 'organisation', ['secondary_source_2_publisher_id'], ['id'])

    op.drop_column('page', 'secondary_source_1_type_of_data')
    op.drop_column('page', 'suppression_and_disclosure')
    op.drop_column('page', 'note_on_corrections_or_updates')
    op.drop_column('page', 'secondary_source_1_note_on_corrections_or_updates')
    op.drop_column('page', 'secondary_source_1_data_source_purpose')

    # ### end Alembic commands ###

[(24, 'sqlalchemy.Enum', 'sa.Enum', (['"""ADMINISTRATIVE"""', '"""SURVEY"""'], {'name': '"""type_of_data_types"""'}), True, 'import sqlalchemy as sa\n'), (31, 'alembic.op.get_bind', 'op.get_bind', ([], {}), False, 'from alembic import op\n'), (33, 'alembic.op.execute', 'op.execute', (['"""\n        UPDATE page SET suppression_and_disclosure = suppression_rules \n        WHERE disclosure_control is null;\n    """'], {}), False, 'from alembic import op\n'), (38, 'alembic.op.execute', 'op.execute', (['"""\n           UPDATE page SET suppression_and_disclosure = disclosure_control \n           WHERE suppression_rules is null;\n       """'], {}), False, 'from alembic import op\n'), (43, 'alembic.op.execute', 'op.execute', (['"""\n        UPDATE page SET suppression_and_disclosure = trim(suppression_rules  || \'  \' ||  disclosure_control)\n        WHERE suppression_rules is not null\n        AND  disclosure_control is not null;\n    """'], {}), False, 'from alembic import op\n'), (49, 'alembic.op.drop_constraint', 'op.drop_constraint', (['"""organisation_secondary_source_2_fkey"""', '"""page"""'], {'type_': '"""foreignkey"""'}), False, 'from alembic import op\n'), (50, 'alembic.op.drop_constraint', 'op.drop_constraint', (['"""frequency_secondary_source_2_fkey"""', '"""page"""'], {'type_': '"""foreignkey"""'}), False, 'from alembic import op\n'), (51, 'alembic.op.drop_constraint', 'op.drop_constraint', (['"""secondary_source_2_type_of_statistic_fkey"""', '"""page"""'], {'type_': '"""foreignkey"""'}), False, 'from alembic import op\n'), (52, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_1_date_next_update"""'], {}), False, 'from alembic import op\n'), (53, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_1_date_updated"""'], {}), False, 'from alembic import op\n'), (54, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_1_suppression_rules"""'], {}), False, 'from alembic import op\n'), (55, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_1_disclosure_control"""'], {}), False, 'from alembic import op\n'), (56, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_frequency"""'], {}), False, 'from alembic import op\n'), (57, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_contact_2_name"""'], {}), False, 'from alembic import op\n'), (58, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_contact_2_phone"""'], {}), False, 'from alembic import op\n'), (59, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_url"""'], {}), False, 'from alembic import op\n'), (60, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_date_next_update"""'], {}), False, 'from alembic import op\n'), (61, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_contact_1_name"""'], {}), False, 'from alembic import op\n'), (62, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""last_update_date"""'], {}), False, 'from alembic import op\n'), (63, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_contact_1_phone"""'], {}), False, 'from alembic import op\n'), (64, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_publisher_text"""'], {}), False, 'from alembic import op\n'), (65, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_disclosure_control"""'], {}), False, 'from alembic import op\n'), (66, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_type_of_statistic_id"""'], {}), False, 'from alembic import op\n'), (67, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_suppression_rules"""'], {}), False, 'from alembic import op\n'), (68, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_frequency_other"""'], {}), False, 'from alembic import op\n'), (69, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_publisher_id"""'], {}), False, 'from alembic import op\n'), (70, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_title"""'], {}), False, 'from alembic import op\n'), (71, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_date"""'], {}), False, 'from alembic import op\n'), (72, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""next_update_date"""'], {}), False, 'from alembic import op\n'), (73, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_date_updated"""'], {}), False, 'from alembic import op\n'), (74, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_statistic_type"""'], {}), False, 'from alembic import op\n'), (75, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_frequency_id"""'], {}), False, 'from alembic import op\n'), (76, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_contact_2_email"""'], {}), False, 'from alembic import op\n'), (77, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_2_contact_1_email"""'], {}), False, 'from alembic import op\n'), (110, 'alembic.op.create_foreign_key', 'op.create_foreign_key', (['"""secondary_source_2_type_of_statistic_fkey"""', '"""page"""', '"""type_of_statistic"""', "['secondary_source_2_type_of_statistic_id']", "['id']"], {}), False, 'from alembic import op\n'), (111, 'alembic.op.create_foreign_key', 'op.create_foreign_key', (['"""frequency_secondary_source_2_fkey"""', '"""page"""', '"""frequency_of_release"""', "['secondary_source_2_frequency_id']", "['id']"], {}), False, 'from alembic import op\n'), (112, 'alembic.op.create_foreign_key', 'op.create_foreign_key', (['"""organisation_secondary_source_2_fkey"""', '"""page"""', '"""organisation"""', "['secondary_source_2_publisher_id']", "['id']"], {}), False, 'from alembic import op\n'), (114, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_1_type_of_data"""'], {}), False, 'from alembic import op\n'), (115, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""suppression_and_disclosure"""'], {}), False, 'from alembic import op\n'), (116, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""note_on_corrections_or_updates"""'], {}), False, 'from alembic import op\n'), (117, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_1_note_on_corrections_or_updates"""'], {}), False, 'from alembic import op\n'), (118, 'alembic.op.drop_column', 'op.drop_column', (['"""page"""', '"""secondary_source_1_data_source_purpose"""'], {}), False, 'from alembic import op\n'), (25, 'sqlalchemy.dialects.postgresql.ARRAY', 'ARRAY', (['type_of_data_types'], {}), False, 'from sqlalchemy.dialects.postgresql import ARRAY\n'), (26, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (27, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (28, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (29, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (83, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (84, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (85, 'sqlalchemy.INTEGER', 'sa.INTEGER', ([], {}), True, 'import sqlalchemy as sa\n'), (86, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (87, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (88, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (89, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (90, 'sqlalchemy.VARCHAR', 'sa.VARCHAR', ([], {'length': '(255)'}), True, 'import sqlalchemy as sa\n'), (91, 'sqlalchemy.VARCHAR', 'sa.VARCHAR', ([], {'length': '(255)'}), True, 'import sqlalchemy as sa\n'), (92, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (93, 'sqlalchemy.INTEGER', 'sa.INTEGER', ([], {}), True, 'import sqlalchemy as sa\n'), (94, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (95, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (96, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (97, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (98, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (99, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (100, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (101, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (102, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (103, 'sqlalchemy.VARCHAR', 'sa.VARCHAR', ([], {'length': '(255)'}), True, 'import sqlalchemy as sa\n'), (104, 'sqlalchemy.VARCHAR', 'sa.VARCHAR', ([], {'length': '(255)'}), True, 'import sqlalchemy as sa\n'), (105, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (106, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (107, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n'), (108, 'sqlalchemy.TEXT', 'sa.TEXT', ([], {}), True, 'import sqlalchemy as sa\n')]