def enable_sqlite_savepoints(engine): """ Savepoint support for sqlite. https://code.google.com/p/pysqlite-static-env/ """ from sqlalchemy import event @event.listens_for(engine, 'connect') def connect(dbapi_connection, connection_record): dbapi_connection.operation_needs_transaction_callback = lambda x: True from zope.sqlalchemy.datamanager import NO_SAVEPOINT_SUPPORT NO_SAVEPOINT_SUPPORT.discard('sqlite')
# See https://code.google.com/p/pysqlite-static-env/ HAS_PATCHED_PYSQLITE = False if engine.url.drivername == 'sqlite': try: from pysqlite2.dbapi2 import Connection except ImportError: pass else: if hasattr(Connection, 'operation_needs_transaction_callback'): HAS_PATCHED_PYSQLITE = True if HAS_PATCHED_PYSQLITE: from sqlalchemy import event from zope.sqlalchemy.datamanager import NO_SAVEPOINT_SUPPORT NO_SAVEPOINT_SUPPORT.remove('sqlite') @event.listens_for(engine, 'connect') def connect(dbapi_connection, connection_record): dbapi_connection.operation_needs_transaction_callback = lambda x: True Session = orm.scoped_session( orm.sessionmaker( bind=engine, extension=tx.ZopeTransactionExtension(), twophase=TEST_TWOPHASE, )) UnboundSession = orm.scoped_session( orm.sessionmaker(
# See https://code.google.com/p/pysqlite-static-env/ HAS_PATCHED_PYSQLITE = False if engine.url.drivername == 'sqlite': try: from pysqlite2.dbapi2 import Connection except ImportError: pass else: if hasattr(Connection, 'operation_needs_transaction_callback'): HAS_PATCHED_PYSQLITE = True if HAS_PATCHED_PYSQLITE: from sqlalchemy import event from zope.sqlalchemy.datamanager import NO_SAVEPOINT_SUPPORT NO_SAVEPOINT_SUPPORT.remove('sqlite') @event.listens_for(engine, 'connect') def connect(dbapi_connection, connection_record): dbapi_connection.operation_needs_transaction_callback = lambda x: True Session = orm.scoped_session(orm.sessionmaker( bind=engine, extension=tx.ZopeTransactionExtension(), twophase=TEST_TWOPHASE, )) UnboundSession = orm.scoped_session(orm.sessionmaker( extension=tx.ZopeTransactionExtension(), twophase=TEST_TWOPHASE,
# See https://code.google.com/p/pysqlite-static-env/ HAS_PATCHED_PYSQLITE = False if engine.url.drivername == "sqlite": try: from pysqlite2.dbapi2 import Connection except ImportError: pass else: if hasattr(Connection, "operation_needs_transaction_callback"): HAS_PATCHED_PYSQLITE = True if HAS_PATCHED_PYSQLITE: from sqlalchemy import event from zope.sqlalchemy.datamanager import NO_SAVEPOINT_SUPPORT NO_SAVEPOINT_SUPPORT.remove("sqlite") @event.listens_for(engine, "connect") def connect(dbapi_connection, connection_record): dbapi_connection.operation_needs_transaction_callback = lambda x: True Session = orm.scoped_session( orm.sessionmaker(bind=engine, twophase=TEST_TWOPHASE)) tx.register(Session) UnboundSession = orm.scoped_session(orm.sessionmaker(twophase=TEST_TWOPHASE)) tx.register(UnboundSession) EventSession = orm.scoped_session( orm.sessionmaker(bind=engine, twophase=TEST_TWOPHASE))