I've been able to successfully get SQLAlchemy and SQLite to work together for the purpose of vertical partitioning (partitioning certain tables into different databases).
The problem I have is that there are parts of the application that do not require the partitioned tables and if I load them via an "attach" command in those sessions that don't actually need it, then the attached database is effectively locked thereby eliminating the entire benefit of the partitioning.
I've tried with the following code where if the SQLALchemy object inherits from a certain base class, then I would attach the database for the session but this does not seem to work as I expect:
class LazyAttachDatabaseSession(orm.Session):
def __init__(self, theEngine, dbpath, **kw):
super().__init__(**kw)
self._engine = theEngine
self._dbpath = dbpath
self._logger = logging.getLogger(__name__)
def get_bind(self, mapper=None, clause=None):
if mapper is not None and issubclass(mapper.class_, _Realtime):
self._engine.execute(f"ATTACH DATABASE '{self._dbpath}' as 'realtime'")
return self._engine
I configure the Session object as follows:
_Session = orm.sessionmaker(theEngine=engine,
class_=LazyAttachDatabaseSession,
dbpath=realtimeDBPath)
But this doesn't work for a variety of reasons. For example, I receive the "database is already attached" error