28 lines
867 B
Python
28 lines
867 B
Python
"""
|
|
Database Backend Bootstrap
|
|
|
|
Import this module before any other imports that use sqlite3.
|
|
When DATABASE_BACKEND=postgresql, it monkey-patches sys.modules['sqlite3']
|
|
with pg_adapter so every subsequent `import sqlite3` gets the PostgreSQL adapter.
|
|
|
|
Default is 'sqlite' (no change — original behavior preserved).
|
|
"""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Load .env BEFORE checking DATABASE_BACKEND — systemd services don't set
|
|
# this env var, so .env is the primary source of truth.
|
|
try:
|
|
from dotenv import load_dotenv
|
|
_env_path = Path(__file__).resolve().parent.parent / '.env'
|
|
if _env_path.exists():
|
|
load_dotenv(_env_path)
|
|
except ImportError:
|
|
pass # rely on system env vars
|
|
|
|
if os.getenv('DATABASE_BACKEND', 'sqlite').lower() == 'postgresql':
|
|
import sys
|
|
from modules import pg_adapter
|
|
sys.modules['sqlite3'] = pg_adapter
|