"""archived_quickfix

Revision ID: 6b5b0854d342
Revises: 5b28afa694a0
Create Date: 2024-09-23 09:38:54.911404

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '6b5b0854d342'
down_revision = '5b28afa694a0'
branch_labels = None
depends_on = None

def index_exists(index_name, table_name):
    conn = op.get_bind()
    return conn.execute(
        f"""
        SELECT 1 
        FROM pg_indexes 
        WHERE indexname = '{index_name}' 
        AND tablename = '{table_name}'
        """
    ).scalar() is not None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('cmf_asset_cat', sa.Column('has_children_archived', sa.Boolean(), nullable=False, server_default="false"))
    op.add_column('cmf_asset_scheme', sa.Column('has_children_archived', sa.Boolean(), nullable=False, server_default="false"))
    op.add_column('cmf_asset_type_cat', sa.Column('has_children_archived', sa.Boolean(), nullable=False, server_default="false"))

    # Drop old indexes if they exist
    if index_exists('ix_cmf_full_search_custom_fields_tsvector_gin', 'cmf_full_search'):
        op.drop_index('ix_cmf_full_search_custom_fields_tsvector_gin', table_name='cmf_full_search')
    
    if index_exists('ix_cmf_full_search_obj_custom_fields_gin_trgm', 'cmf_full_search'):
        op.drop_index('ix_cmf_full_search_obj_custom_fields_gin_trgm', table_name='cmf_full_search')

    # Create new indexes if they don't exist
    if not index_exists('ix_cmf_full_search_addon_fields_tsvector_gin', 'cmf_full_search'):
        op.create_index('ix_cmf_full_search_addon_fields_tsvector_gin', 
                       'cmf_full_search', ['addon_fields_tsvector'], 
                       unique=False, postgresql_using='gin')
    
    if not index_exists('ix_cmf_full_search_obj_addon_fields_gin_trgm', 'cmf_full_search'):
        op.create_index('ix_cmf_full_search_obj_addon_fields_gin_trgm', 
                       'cmf_full_search', ['obj_addon_fields'], 
                       unique=False, postgresql_using='gin',
                       postgresql_ops={'obj_addon_fields': 'gin_trgm_ops'})
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    if index_exists('ix_cmf_full_search_obj_addon_fields_gin_trgm', 'cmf_full_search'):
        op.drop_index('ix_cmf_full_search_obj_addon_fields_gin_trgm', 
                     table_name='cmf_full_search')
    
    if index_exists('ix_cmf_full_search_addon_fields_tsvector_gin', 'cmf_full_search'):
        op.drop_index('ix_cmf_full_search_addon_fields_tsvector_gin', 
                     table_name='cmf_full_search')

    # Create old indexes if they don't exist
    if not index_exists('ix_cmf_full_search_obj_custom_fields_gin_trgm', 'cmf_full_search'):
        op.create_index('ix_cmf_full_search_obj_custom_fields_gin_trgm', 
                       'cmf_full_search', ['obj_addon_fields'], 
                       unique=False)
    
    if not index_exists('ix_cmf_full_search_custom_fields_tsvector_gin', 'cmf_full_search'):
        op.create_index('ix_cmf_full_search_custom_fields_tsvector_gin', 
                       'cmf_full_search', ['addon_fields_tsvector'], 
                       unique=False)

    op.drop_column('cmf_asset_type_cat', 'has_children_archived')
    op.drop_column('cmf_asset_scheme', 'has_children_archived')
    op.drop_column('cmf_asset_cat', 'has_children_archived')
    # ### end Alembic commands ###