MCPcopy Create free account
hub / github.com/AQ-MedAI/MedMemoryBench / initialize_engine

Function initialize_engine

methods/letta/server/db.py:57–119  ·  view source on GitHub ↗

Initialize the database engine only when needed.

()

Source from the content-addressed store, hash-verified

55
56
57def initialize_engine():
58 """Initialize the database engine only when needed."""
59 global engine, SessionLocal, _engine_initialized
60
61 with _engine_lock:
62 # Check again inside the lock to prevent race conditions
63 if _engine_initialized:
64 return
65
66 if settings.letta_pg_uri_no_default:
67 logger.info("Creating postgres engine")
68 config.recall_storage_type = "postgres"
69 config.recall_storage_uri = settings.letta_pg_uri_no_default
70 config.archival_storage_type = "postgres"
71 config.archival_storage_uri = settings.letta_pg_uri_no_default
72
73 # create engine
74 engine = create_engine(
75 settings.letta_pg_uri,
76 # f"{settings.letta_pg_uri}?options=-c%20client_encoding=UTF8",
77 pool_size=settings.pg_pool_size,
78 max_overflow=settings.pg_max_overflow,
79 pool_timeout=settings.pg_pool_timeout,
80 pool_recycle=settings.pg_pool_recycle,
81 echo=settings.pg_echo,
82 # connect_args={"client_encoding": "utf8"},
83 )
84 else:
85 # TODO: don't rely on config storage
86 engine_path = "sqlite:///" + os.path.join(config.recall_storage_path, "sqlite.db")
87 logger.info("Creating sqlite engine " + engine_path)
88
89 engine = create_engine(engine_path)
90
91 # Store the original connect method
92 original_connect = engine.connect
93
94 def wrapped_connect(*args, **kwargs):
95 with db_error_handler():
96 # Get the connection
97 connection = original_connect(*args, **kwargs)
98
99 # Store the original execution method
100 original_execute = connection.execute
101
102 # Wrap the execute method of the connection
103 def wrapped_execute(*args, **kwargs):
104 with db_error_handler():
105 return original_execute(*args, **kwargs)
106
107 # Replace the connection's execute method
108 connection.execute = wrapped_execute
109
110 return connection
111
112 # Replace the engine's connect method
113 engine.connect = wrapped_connect
114

Callers 1

get_dbFunction · 0.85

Calls 2

infoMethod · 0.65
joinMethod · 0.45

Tested by

no test coverage detected