"""Copy certify_history to publish_history and rename certified_files_history

Revision ID: 6e3be68b87ae
Revises: 78a36e024274
Create Date: 2020-06-08 12:04:57.747397

"""

# revision identifiers, used by Alembic.
revision = '6e3be68b87ae'
down_revision = '78a36e024274'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

def upgrade(engine_name):
    globals()["upgrade_%s" % engine_name]()


def downgrade(engine_name):
    globals()["downgrade_%s" % engine_name]()





def upgrade_data_broker():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('publish_history',
        sa.Column('created_at', sa.DateTime(), nullable=True),
        sa.Column('updated_at', sa.DateTime(), nullable=True),
        sa.Column('publish_history_id', sa.Integer(), nullable=False),
        sa.Column('submission_id', sa.Integer(), nullable=True),
        sa.Column('user_id', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['submission_id'], ['submission.submission_id'], name='fk_publish_history_submission_id'),
        sa.ForeignKeyConstraint(['user_id'], ['users.user_id'], name='fk_publish_history_user', ondelete='SET NULL'),
        sa.PrimaryKeyConstraint('publish_history_id')
    )
    op.execute("""
            INSERT INTO publish_history (created_at, updated_at, publish_history_id, submission_id, user_id)
            SELECT created_at, updated_at, certify_history_id, submission_id, user_id
            FROM certify_history
        """)
    op.create_table('published_files_history',
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.Column('published_files_history_id', sa.Integer(), nullable=False),
    sa.Column('publish_history_id', sa.Integer(), nullable=True),
    sa.Column('certify_history_id', sa.Integer(), nullable=True),
    sa.Column('submission_id', sa.Integer(), nullable=True),
    sa.Column('filename', sa.Text(), nullable=True),
    sa.Column('file_type_id', sa.Integer(), nullable=True),
    sa.Column('warning_filename', sa.Text(), nullable=True),
    sa.Column('comment', sa.Text(), nullable=True),
    sa.ForeignKeyConstraint(['file_type_id'], ['file_type.file_type_id'], name='fk_published_files_history_file_type'),
    sa.ForeignKeyConstraint(['publish_history_id'], ['publish_history.publish_history_id'], name='fk_publish_history_published_files_id'),
    sa.ForeignKeyConstraint(['certify_history_id'], ['certify_history.certify_history_id'], name='fk_certify_history_published_files_id'),
    sa.ForeignKeyConstraint(['submission_id'], ['submission.submission_id'], name='fk_published_files_history_submission_id'),
    sa.PrimaryKeyConstraint('published_files_history_id')
    )
    op.execute("""
            INSERT INTO published_files_history (created_at, updated_at, published_files_history_id, publish_history_id, certify_history_id, submission_id, filename, file_type_id, warning_filename, comment)
            SELECT created_at, updated_at, certified_files_history_id, certify_history_id, certify_history_id, submission_id, filename, file_type_id, warning_filename, comment
            FROM certified_files_history
        """)
    op.drop_table('certified_files_history')
    # Set sequences because apparently INSERT INTO doesn't update the sequence
    op.execute("SELECT setval('published_files_history_published_files_history_id_seq', (SELECT MAX(published_files_history_id) FROM published_files_history))")
    op.execute(
        "SELECT setval('publish_history_publish_history_id_seq', (SELECT MAX(publish_history_id) FROM publish_history));")
    # ### end Alembic commands ###


def downgrade_data_broker():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('certified_files_history',
    sa.Column('created_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
    sa.Column('updated_at', postgresql.TIMESTAMP(), autoincrement=False, nullable=True),
    sa.Column('certified_files_history_id', sa.INTEGER(), nullable=False),
    sa.Column('certify_history_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('submission_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('filename', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('file_type_id', sa.INTEGER(), autoincrement=False, nullable=True),
    sa.Column('warning_filename', sa.TEXT(), autoincrement=False, nullable=True),
    sa.Column('comment', sa.TEXT(), autoincrement=False, nullable=True),
    sa.ForeignKeyConstraint(['certify_history_id'], ['certify_history.certify_history_id'], name='fk_certify_history_certified_files_id'),
    sa.ForeignKeyConstraint(['file_type_id'], ['file_type.file_type_id'], name='fk_certified_files_history_file_type'),
    sa.ForeignKeyConstraint(['submission_id'], ['submission.submission_id'], name='fk_certified_files_history_submission_id'),
    sa.PrimaryKeyConstraint('certified_files_history_id', name='certified_files_history_pkey')
    )
    op.execute("""
            INSERT INTO certified_files_history (created_at, updated_at, certified_files_history_id, certify_history_id, submission_id, filename, file_type_id, warning_filename, comment)
            SELECT created_at, updated_at, published_files_history_id, certify_history_id, submission_id, filename, file_type_id, warning_filename, comment
            FROM published_files_history
        """)
    op.drop_table('published_files_history')
    op.drop_table('publish_history')
    # Set sequences because apparently INSERT INTO doesn't update the sequence
    op.execute("SELECT setval('certified_files_history_certified_files_history_id_seq', (SELECT MAX(certified_files_history_id) FROM certified_files_history))")
    # ### end Alembic commands ###

