Get or create the database engine (thread-safe singleton pattern). Returns: Tuple of (engine, SessionLocal)
()
| 155 | |
| 156 | |
| 157 | def _get_engine(): |
| 158 | """ |
| 159 | Get or create the database engine (thread-safe singleton pattern). |
| 160 | |
| 161 | Returns: |
| 162 | Tuple of (engine, SessionLocal) |
| 163 | """ |
| 164 | global _engine, _SessionLocal |
| 165 | |
| 166 | # Double-checked locking for thread safety |
| 167 | if _engine is None: |
| 168 | with _engine_lock: |
| 169 | if _engine is None: |
| 170 | db_path = get_registry_path() |
| 171 | db_url = f"sqlite:///{db_path.as_posix()}" |
| 172 | _engine = create_engine( |
| 173 | db_url, |
| 174 | connect_args={ |
| 175 | "check_same_thread": False, |
| 176 | "timeout": SQLITE_TIMEOUT, |
| 177 | } |
| 178 | ) |
| 179 | Base.metadata.create_all(bind=_engine) |
| 180 | _migrate_add_default_concurrency(_engine) |
| 181 | _SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=_engine) |
| 182 | logger.debug("Initialized registry database at: %s", db_path) |
| 183 | |
| 184 | return _engine, _SessionLocal |
| 185 | |
| 186 | |
| 187 | def _migrate_add_default_concurrency(engine) -> None: |
no test coverage detected