"""new alarm schema

Revision ID: 31aec2f86c40
Revises: 1842ba85a5c7
Create Date: 2022-05-18 14:22:51.028405

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

# revision identifiers, used by Alembic.
revision = '31aec2f86c40'
down_revision = '1842ba85a5c7'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('alarm', schema=None) as batch_op:
        batch_op.execute("TRUNCATE alarm")
        batch_op.add_column(sa.Column('aspect', sa.String(length=255), nullable=False))
        batch_op.alter_column('last_updated',
               existing_type=postgresql.TIMESTAMP(),
               nullable=False)
        batch_op.alter_column('text',
               existing_type=sa.VARCHAR(length=255),
               nullable=False)
        batch_op.drop_constraint('fk_alarm_group_id_group', type_='foreignkey')
        batch_op.drop_constraint('fk_alarm_proxy_id_proxy', type_='foreignkey')
        batch_op.drop_constraint('fk_alarm_origin_id_origin', type_='foreignkey')
        batch_op.drop_constraint('fk_alarm_bridge_id_bridge', type_='foreignkey')
        batch_op.drop_column('origin_id')
        batch_op.drop_column('alarm_type')
        batch_op.drop_column('bridge_id')
        batch_op.drop_column('group_id')
        batch_op.drop_column('proxy_id')

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('alarm', schema=None) as batch_op:
        batch_op.add_column(sa.Column('proxy_id', sa.INTEGER(), autoincrement=False, nullable=True))
        batch_op.add_column(sa.Column('group_id', sa.INTEGER(), autoincrement=False, nullable=True))
        batch_op.add_column(sa.Column('bridge_id', sa.INTEGER(), autoincrement=False, nullable=True))
        batch_op.add_column(sa.Column('alarm_type', sa.VARCHAR(length=255), autoincrement=False, nullable=False))
        batch_op.add_column(sa.Column('origin_id', sa.INTEGER(), autoincrement=False, nullable=True))
        batch_op.create_foreign_key('fk_alarm_bridge_id_bridge', 'bridge', ['bridge_id'], ['id'])
        batch_op.create_foreign_key('fk_alarm_origin_id_origin', 'origin', ['origin_id'], ['id'])
        batch_op.create_foreign_key('fk_alarm_proxy_id_proxy', 'proxy', ['proxy_id'], ['id'])
        batch_op.create_foreign_key('fk_alarm_group_id_group', 'group', ['group_id'], ['id'])
        batch_op.alter_column('text',
               existing_type=sa.VARCHAR(length=255),
               nullable=True)
        batch_op.alter_column('last_updated',
               existing_type=postgresql.TIMESTAMP(),
               nullable=True)
        batch_op.drop_column('aspect')

    # ### end Alembic commands ###
