Make sqlalchemy engine connect listener recorder specific (#34908)
This commit is contained in:
parent
8467b91390
commit
574d8d30a7
1 changed files with 6 additions and 7 deletions
|
@ -9,9 +9,7 @@ import threading
|
||||||
import time
|
import time
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
from sqlalchemy import create_engine, exc, select
|
from sqlalchemy import create_engine, event as sqlalchemy_event, exc, select
|
||||||
from sqlalchemy.engine import Engine
|
|
||||||
from sqlalchemy.event import listens_for
|
|
||||||
from sqlalchemy.orm import scoped_session, sessionmaker
|
from sqlalchemy.orm import scoped_session, sessionmaker
|
||||||
from sqlalchemy.pool import StaticPool
|
from sqlalchemy.pool import StaticPool
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
@ -488,15 +486,13 @@ class Recorder(threading.Thread):
|
||||||
"""Ensure database is ready to fly."""
|
"""Ensure database is ready to fly."""
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
|
|
||||||
# pylint: disable=unused-variable
|
def setup_recorder_connection(dbapi_connection, connection_record):
|
||||||
@listens_for(Engine, "connect")
|
|
||||||
def setup_connection(dbapi_connection, connection_record):
|
|
||||||
"""Dbapi specific connection settings."""
|
"""Dbapi specific connection settings."""
|
||||||
|
|
||||||
# We do not import sqlite3 here so mysql/other
|
# We do not import sqlite3 here so mysql/other
|
||||||
# users do not have to pay for it to be loaded in
|
# users do not have to pay for it to be loaded in
|
||||||
# memory
|
# memory
|
||||||
if self.db_url == "sqlite://" or ":memory:" in self.db_url:
|
if self.db_url.startswith("sqlite://"):
|
||||||
old_isolation = dbapi_connection.isolation_level
|
old_isolation = dbapi_connection.isolation_level
|
||||||
dbapi_connection.isolation_level = None
|
dbapi_connection.isolation_level = None
|
||||||
cursor = dbapi_connection.cursor()
|
cursor = dbapi_connection.cursor()
|
||||||
|
@ -519,6 +515,9 @@ class Recorder(threading.Thread):
|
||||||
self.engine.dispose()
|
self.engine.dispose()
|
||||||
|
|
||||||
self.engine = create_engine(self.db_url, **kwargs)
|
self.engine = create_engine(self.db_url, **kwargs)
|
||||||
|
|
||||||
|
sqlalchemy_event.listen(self.engine, "connect", setup_recorder_connection)
|
||||||
|
|
||||||
Base.metadata.create_all(self.engine)
|
Base.metadata.create_all(self.engine)
|
||||||
self.get_session = scoped_session(sessionmaker(bind=self.engine))
|
self.get_session = scoped_session(sessionmaker(bind=self.engine))
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue