Get database connection. Supports both SQLite and PostgreSQL.
()
| 303 | |
| 304 | |
| 305 | def get_db_connection(): |
| 306 | """Get database connection. Supports both SQLite and PostgreSQL.""" |
| 307 | if using_postgres(): |
| 308 | if psycopg is None: |
| 309 | raise RuntimeError( |
| 310 | "PostgreSQL support requires psycopg. Install service requirements first." |
| 311 | ) |
| 312 | conn = psycopg.connect(DATABASE_URL, row_factory=dict_row) |
| 313 | return DatabaseConnection(conn, "postgres") |
| 314 | |
| 315 | db_path = _SQLITE_DB_PATH |
| 316 | os.makedirs(os.path.dirname(db_path), exist_ok=True) |
| 317 | |
| 318 | conn = sqlite3.connect(db_path, timeout=30.0) |
| 319 | conn.row_factory = sqlite3.Row |
| 320 | |
| 321 | # Enable WAL mode for better concurrent access |
| 322 | conn.execute("PRAGMA journal_mode=WAL") |
| 323 | conn.execute("PRAGMA busy_timeout=30000") |
| 324 | |
| 325 | return DatabaseConnection(conn, "sqlite") |
| 326 | |
| 327 | |
| 328 | def get_database_status() -> dict[str, Any]: |
no test coverage detected