estandiaa-marain · GitHub

Describe the bug
We are using this approach (suggested in this Stack Overflow answer) to do a dry-run for the alembic upgrade

with connectable.connect() as connection:
    context.configure(
        connection=connection, target_metadata=target_metadata,
        transactional_ddl=True
    )
    with context.begin_transaction() as transaction:
        context.run_migrations()
        if 'dry-run' in context.get_x_argument():
            print('Dry-run succeeded; now rolling back transaction...')
            transaction.rollback()

This used to work fine for our PostgreSQL database until recently.

Expected behavior
The DB upgrade is rolled back.

To Reproduce
See the code above. The key line is the call to transaction.rollback(). If this line is called when the transaction context manager is active, then an error is thrown when the __exit__ method is called.

Error

File "<python_path>/lib/python3.7/site-packages/alembic/runtime/migration.py", line 44, in __exit__
    self._proxied_transaction.__exit__(type_, value, traceback)
AttributeError: 'NoneType' object has no attribute '__exit__'

The problem seems to be that _ProxyTransaction.rollback sets self.migration_context._transaction = None which then throws an error when _ProxyTransaction.__exit__ runs self._proxied_transaction.__exit__(type_, value, traceback)

Versions.

  • OS: Ubuntu 20.04.2 LTS
  • Python: 3.7.7
  • Alembic: 1.5.8
  • SQLAlchemy: 1.3.23
  • Database: PostgreSQL 13.2
  • DBAPI: I am not sure what this is

Additional comments
This is my first issue in this repo. Please let me know if I missed anything or if you need any additional information. I am happy to provide more details.

Thank you in advance.

Read the original on github.com ↗