@@ -187,9 +187,6 @@ def __init__(self, db_url: str, **kwargs: Any):
187187 # The current database schema version in use, "None" if not yet checked
188188 self ._db_schema_version : Optional [str ] = None
189189
190- # Lock to ensure thread-safe schema version check
191- self ._db_schema_lock = asyncio .Lock ()
192-
193190 # Per-session locks used to serialize append_event calls in this process.
194191 self ._session_locks : dict [_SessionLockKey , asyncio .Lock ] = {}
195192 self ._session_lock_ref_count : dict [_SessionLockKey , int ] = {}
@@ -261,62 +258,55 @@ async def _prepare_tables(self):
261258 DB schema version to use and creates the tables (including setting the
262259 schema version metadata) if needed.
263260 """
264- # Check the database schema version and set the _db_schema_version if
265- # needed
266- if self ._db_schema_version is not None :
267- return
268-
269- async with self ._db_schema_lock :
270- # Double-check after acquiring the lock
271- if self ._db_schema_version is not None :
272- return
273- try :
274- async with self .db_engine .connect () as conn :
275- self ._db_schema_version = await conn .run_sync (
276- _schema_check_utils .get_db_schema_version_from_connection
277- )
278- except Exception as e :
279- logger .error ("Failed to inspect database tables: %s" , e )
280- raise
281-
282- # Check if tables are created and create them if not
261+ # Early return if tables are already created
283262 if self ._tables_created :
284263 return
285264
286265 async with self ._table_creation_lock :
287266 # Double-check after acquiring the lock
288- if not self ._tables_created :
289- async with self .db_engine .begin () as conn :
290- if (
291- self ._db_schema_version
292- == _schema_check_utils .LATEST_SCHEMA_VERSION
293- ):
294- # Uncomment to recreate DB every time
295- # await conn.run_sync(BaseV1.metadata.drop_all)
296- logger .debug ("Using V1 schema tables..." )
297- await conn .run_sync (BaseV1 .metadata .create_all )
298- else :
299- # await conn.run_sync(BaseV0.metadata.drop_all)
300- logger .debug ("Using V0 schema tables..." )
301- await conn .run_sync (BaseV0 .metadata .create_all )
302- self ._tables_created = True
267+ if self ._tables_created :
268+ return
269+
270+ # Check the database schema version and set the _db_schema_version
271+ if self ._db_schema_version is None :
272+ try :
273+ async with self .db_engine .connect () as conn :
274+ self ._db_schema_version = await conn .run_sync (
275+ _schema_check_utils .get_db_schema_version_from_connection
276+ )
277+ except Exception as e :
278+ logger .error ("Failed to inspect database tables: %s" , e )
279+ raise
303280
281+ async with self .db_engine .begin () as conn :
304282 if self ._db_schema_version == _schema_check_utils .LATEST_SCHEMA_VERSION :
305- async with self ._rollback_on_exception_session () as sql_session :
306- # Check if schema version is set, if not, set it to the latest
307- # version
308- stmt = select (StorageMetadata ).where (
309- StorageMetadata .key == _schema_check_utils .SCHEMA_VERSION_KEY
283+ # Uncomment to recreate DB every time
284+ # await conn.run_sync(BaseV1.metadata.drop_all)
285+ logger .debug ("Using V1 schema tables..." )
286+ await conn .run_sync (BaseV1 .metadata .create_all )
287+ else :
288+ # await conn.run_sync(BaseV0.metadata.drop_all)
289+ logger .debug ("Using V0 schema tables..." )
290+ await conn .run_sync (BaseV0 .metadata .create_all )
291+
292+ if self ._db_schema_version == _schema_check_utils .LATEST_SCHEMA_VERSION :
293+ async with self ._rollback_on_exception_session () as sql_session :
294+ # Check if schema version is set, if not, set it to the latest
295+ # version
296+ stmt = select (StorageMetadata ).where (
297+ StorageMetadata .key == _schema_check_utils .SCHEMA_VERSION_KEY
298+ )
299+ result = await sql_session .execute (stmt )
300+ metadata = result .scalars ().first ()
301+ if not metadata :
302+ metadata = StorageMetadata (
303+ key = _schema_check_utils .SCHEMA_VERSION_KEY ,
304+ value = _schema_check_utils .LATEST_SCHEMA_VERSION ,
310305 )
311- result = await sql_session .execute (stmt )
312- metadata = result .scalars ().first ()
313- if not metadata :
314- metadata = StorageMetadata (
315- key = _schema_check_utils .SCHEMA_VERSION_KEY ,
316- value = _schema_check_utils .LATEST_SCHEMA_VERSION ,
317- )
318- sql_session .add (metadata )
319- await sql_session .commit ()
306+ sql_session .add (metadata )
307+ await sql_session .commit ()
308+
309+ self ._tables_created = True
320310
321311 @override
322312 async def create_session (
0 commit comments