"""chat messages 2

Revision ID: 46af1902cd81
Revises: c1fb20145a41
Create Date: 2021-11-09 17:44:21.704600

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

# revision identifiers, used by Alembic.
revision = '46af1902cd81'
down_revision = 'c1fb20145a41'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index('ix_cmf_chat_group_last_comment_id', table_name='cmf_chat_group')
    # compat fix cmf_u_work_chat_group_last_comment_id_fkey -> cmf_chat_group_last_comment_id_fkey
    op.drop_constraint('cmf_chat_group_last_comment_id_fkey', 'cmf_chat_group', type_='foreignkey')
    op.drop_column('cmf_chat_group', 'last_comment_id')
    op.add_column('cmf_chat_message', sa.Column('name', sa.String(length=256), nullable=True))
    op.add_column('cmf_chat_message', sa.Column('code', sa.String(length=64), nullable=True))
    op.add_column('cmf_chat_message', sa.Column('system', sa.Boolean(), nullable=True))
    op.execute('update cmf_chat_message set system = false')
    op.alter_column('cmf_chat_message', 'system', nullable=False)
    op.add_column('cmf_chat_message', sa.Column('cache_last_comments', sa.TEXT(), nullable=False, server_default='[]'))
    op.create_index(op.f('ix_cmf_chat_message_code'), 'cmf_chat_message', ['code'], unique=True)
    op.create_index(op.f('ix_cmf_chat_message_name'), 'cmf_chat_message', ['name'], unique=False)
    op.drop_index('ix_cmf_chat_topic_last_comment_id', table_name='cmf_chat_topic')
    # compat fix cmf_u_work_chat_topic_last_comment_id_fkey -> cmf_chat_topic_last_comment_id_fkey
    op.drop_constraint('cmf_chat_topic_last_comment_id_fkey', 'cmf_chat_topic', type_='foreignkey')
    op.drop_column('cmf_chat_topic', 'last_comment_id')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('cmf_chat_topic', sa.Column('last_comment_id', sa.VARCHAR(length=64), autoincrement=False, nullable=True))
    # compat fix cmf_u_work_chat_topic_last_comment_id_fkey -> cmf_chat_topic_last_comment_id_fkey
    op.create_foreign_key('cmf_chat_topic_last_comment_id_fkey', 'cmf_chat_topic', 'cmf_comment', ['last_comment_id'], ['id'])
    op.create_index('ix_cmf_chat_topic_last_comment_id', 'cmf_chat_topic', ['last_comment_id'], unique=False)
    op.drop_index(op.f('ix_cmf_chat_message_name'), table_name='cmf_chat_message')
    op.drop_index(op.f('ix_cmf_chat_message_code'), table_name='cmf_chat_message')
    op.drop_column('cmf_chat_message', 'cache_last_comments')
    op.drop_column('cmf_chat_message', 'system')
    op.drop_column('cmf_chat_message', 'code')
    op.drop_column('cmf_chat_message', 'name')
    op.add_column('cmf_chat_group', sa.Column('last_comment_id', sa.VARCHAR(length=64), autoincrement=False, nullable=True))
    # compat fix cmf_u_work_chat_group_last_comment_id_fkey -> cmf_chat_group_last_comment_id_fkey
    op.create_foreign_key('cmf_chat_group_last_comment_id_fkey', 'cmf_chat_group', 'cmf_comment', ['last_comment_id'], ['id'])
    op.create_index('ix_cmf_chat_group_last_comment_id', 'cmf_chat_group', ['last_comment_id'], unique=False)
    # ### end Alembic commands ###
