SqlAlchemy/ALembic migrations - how to ignore errors and apply migration

Viewed 410

I have the following migration:

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    try:
        op.drop_index('idx_token_attribute_name', table_name='token_index')
    except Exception:
        logger = logging.getLogger()
        logger.info('idx_token_attribute_name does not exist on token_index')
    # ### end Alembic commands ###

The issue I'm running into is that sometimes, the index doesnt exist. In this case, I want it to fail silently and still apply this migration. However, because the transaction fails, and it never rollsback, I get the following error:

 (psycopg2.errors.InFailedSqlTransaction) current transaction is aborted, commands ignored until end of transaction block

How do I ignore the error and apply the migration?

0 Answers
Related