2

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?

Huy
  • 10,806
  • 13
  • 55
  • 99
  • 1
    Does this help? https://alembic.sqlalchemy.org/en/latest/api/runtime.html?highlight=autocommit#alembic.runtime.migration.MigrationContext.autocommit_block – Gord Thompson Jun 09 '21 at 17:59

1 Answers1

0

You can use the flag if_exists=True from v1.12.0 and up (alembic docs).

def upgrade():
    op.drop_index('idx_token_attribute_name', table_name='token_index', if_exists=True)
TKS
  • 45
  • 1
  • 7