Files
media-downloader/modules/db_bootstrap.py
Todd 0d7b2b1aab Initial commit
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-29 22:42:55 -04:00

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