Search code examples
pythonsqlalchemyalembic

Irreversible migrations in Alembic


Not all database migrations are reversible. When using Alembic+SQLAlchemy, is there a (canonical) way to "mark" my downgrade function/migration so that it cannot be reversed?

Compare ActiveRecord migrations where you can raise ActiveRecord::IrreversibleMigration from your down method to signal this.

Would raising an exception (any exception) in donwgrade cause the downgrade to fail "cleanly"?


Solution

  • An exception is enough. It will fail the migration and you will never be able to go back.

    def downgrade():
        raise Exception("Irreversible migration")