"""Add meta_search table

Revision ID: 1a73867b1e79
Revises: 1c6e2fb3d3b6
Create Date: 2019-04-29 11:47:04.783168

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
from sqlalchemy.orm.session import sessionmaker
from api.actions import update_meta_search
# revision identifiers, used by Alembic.
revision = '1a73867b1e79'
down_revision = '1c6e2fb3d3b6'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('meta_search',
        sa.Column('schema', sa.String(length=100), nullable=False),
        sa.Column('table', sa.String(length=100), nullable=False),
        sa.Column('comment', postgresql.TSVECTOR(), nullable=True),
        sa.PrimaryKeyConstraint('schema', 'table'),
        schema='public'
    )
    conn = op.get_bind()
    meta = sa.MetaData(bind=conn)
    meta.reflect()
    Session = sessionmaker()
    session = Session(bind=conn)
    try:
        for table in meta.tables.values():
            update_meta_search(session, table.name, table.schema, insert_only=True)
        session.commit()
    except:
        session.rollback()
        raise
    finally:
        session.close()


def downgrade():
    op.drop_table('meta_search', schema='public')


Add meta_search to alembic