You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
No easy way to enforce versioning when using nested transactions #204
To speed up tests I'm using nested transactions - this allows me to rollback all changes after I'm done testing, and move onto next test without reloading whole database, migrations and fixtures.
Caused my quite a headache as I couldn't figure out why sqlalchemy-continuum fails to work for me during tests (it always stored only the latest version).
It would be nice for this check to be easily disabled, maybe by using if self.options['ignore_nested'] and session.transaction.nested: instead?
For now I'm using ugly pytest fixture which contains a copy pasted function without this check. I expect it to break at if sqlachemy_continuum changes how clear method looks like or if it gets installed in more places that I don't manually replace after monkey patching so I don't think this is a viable solution.
@pytest.fixturedefsqlalchemy_continuum_nested_mock():
""" Patch sqlalchemy-continuum so it creates new version on nested transaction commit See https://github.com/kvesteri/sqlalchemy-continuum/issues/204 for details """importsqlalchemy_continuumassertsqlalchemy_continuum.__version__=='1.3.6', (
f'Please review this patch as it was untested against sqlalchemy_continuum {sqlalchemy_continuum.__version__}',
)
versioning_manager=sqlalchemy_continuum.versioning_managerdefmock_clear(self, session):
conn=self.session_connection_map.pop(session, None)
ifconnisNone:
returnifconninself.units_of_work:
uow=self.units_of_work[conn]
uow.reset(session)
delself.units_of_work[conn]
forconnectionindict(self.units_of_work).keys():
ifconnection.closedorconn.connectionisconnection.connection:
uow=self.units_of_work[connection]
uow.reset(session)
delself.units_of_work[connection]
defreinstall_clear_ref():
versioning_manager.remove_session_tracking(_db.session)
versioning_manager.session_listeners.update({
'after_commit': getattr(versioning_manager, 'clear'),
'after_rollback': getattr(versioning_manager, 'clear'),
})
versioning_manager.track_session(_db.session)
withpatch.object(versioning_manager, 'clear', lambda*args: mock_clear(versioning_manager, *args)):
reinstall_clear_ref()
yieldreinstall_clear_ref()
On SQLAlchemy 2.0, session.commit() always commits the outermost transaction. For tests that roll back an outer transaction, use Session(bind=connection, join_transaction_mode="create_savepoint"); each session.commit() then produces its own version. The SAWarning that pattern currently emits is tracked in #392 (PR #393).
Activity
rooterkyberian commentedon Oct 16, 2018
For now I'm using ugly pytest fixture which contains a copy pasted function without this check. I expect it to break at if
sqlachemy_continuumchanges howclearmethod looks like or if it gets installed in more places that I don't manually replace after monkey patching so I don't think this is a viable solution.josecsotomorales commentedon Oct 15, 2026
On SQLAlchemy 2.0,
session.commit()always commits the outermost transaction. For tests that roll back an outer transaction, useSession(bind=connection, join_transaction_mode="create_savepoint"); eachsession.commit()then produces its own version. TheSAWarningthat pattern currently emits is tracked in #392 (PR #393).