"""empty message

Revision ID: 37ce6fbc57ff
Revises: 32d22e90db29
Create Date: 2022-11-28 13:07:32.915677

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

# revision identifiers, used by Alembic.
revision = '37ce6fbc57ff'
down_revision = '32d22e90db29'
branch_labels = None
depends_on = None

main_channel = 'fa050858-1d94-11ec-9621-0242ac130002'


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('main_chat_post',
    sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.Column('tags', sa.ARRAY(sa.String()), nullable=True),
    sa.Column('comment_channel_id', postgresql.UUID(as_uuid=True), nullable=False),
    sa.ForeignKeyConstraint(['comment_channel_id'], ['channel.id'], ),
    sa.ForeignKeyConstraint(['id'], ['post.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.add_column('post', sa.Column('type', sa.String(), nullable=True))
    type = sa.table('post', sa.column('type'))
    op.execute(type.update().values(type="post"))
    op.alter_column('post', 'type', nullable=False)
    # ### end Alembic commands ###
    conn = op.get_bind()
    main_posts = conn.execute(f"SELECT id FROM post WHERE channel_id='{ main_channel }';")
    conn.execute(f"UPDATE post SET type='main_chat_post' WHERE channel_id='{ main_channel }';")
    for post in main_posts:
        channel_id = uuid.uuid4()
        conn.execute(f"INSERT INTO channel (id) VALUES ('{channel_id}');")
        conn.execute(f"INSERT INTO main_chat_post (id, comment_channel_id) VALUES ('{post[0]}', '{channel_id}');")


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('post', 'type')
    op.drop_table('main_chat_post')
    # ### end Alembic commands ###
