Database-Backends.md
March 24, 2026 · View on GitHub
Database Backends & Migrations (tldw_server)
Overview
- The project uses a backend abstraction (
DatabaseBackend) to support SQLite (default) and PostgreSQL (select modules). - Content stores (Media/ChaChaNotes/Evaluations) default to SQLite with WAL, tuned pragmas, and FTS where applicable.
- Prompt Studio and Workflows include first-class PostgreSQL support and ship their own schema/migration logic.
Backends
- SQLite (default): optimized with WAL,
synchronous=NORMAL, andbusy_timeout=5000to reduce lock contention. - PostgreSQL (optional): available where adapters are implemented; connection managed via lightweight pooling.
Configuration
- Content database backend selection comes from config file/env, normalized via:
TLDW_CONTENT_DB_BACKEND=sqlite|postgres- Additional vars:
TLDW_CONTENT_SQLITE_PATH,TLDW_CONTENT_PG_*(or compatiblePOSTGRES_TEST_*).
- See
tldw_Server_API/app/core/DB_Management/content_backend.pyfor resolution rules.
Migrations
-
Media Database (SQLite):
- Uses internal schema versioning with optional JSON-based migrations via
DatabaseMigrator. - If no JSON migration files are present, the system proceeds (no-op) and ensures FTS structures are initialized.
- Entry points:
media_db.native_class.MediaDatabase(initializes/migrates on first use).
- Uses internal schema versioning with optional JSON-based migrations via
-
ChaChaNotes (SQLite):
- Managed version upgrades inside
ChaChaNotes_DB.pywith FTS triggers and sync logs.
- Managed version upgrades inside
-
Evaluations (SQLite):
- Dedicated migrations: v5 unified evaluations table, v6 audit logging enhancements.
- See
migrations_v5_unified_evaluations.pyandmigrations_v6_audit_logging.py.
-
Prompt Studio (PostgreSQL):
- Schema and indexes defined in SQL files under
DB_Management/migrations/. - Adapter applies migrations and FTS setup (
PromptStudioDatabase).
- Schema and indexes defined in SQL files under
-
Workflows (SQLite/PG):
- SQLite schema included in adapter; PostgreSQL schema applied via adapter migrations.
- SQLite→PostgreSQL migration utility:
DB_Management/migration_tools.py.
CLI Utilities
- Generic JSON-file migration runner:
tldw_Server_API/app/core/DB_Management/migrate_db.py. - SQLite→PostgreSQL content/workflows migrator:
tldw_Server_API/app/core/DB_Management/migration_tools.py.
Backups
- Centralized DB path helpers:
db_path_utils.DatabasePaths(per-user). Backup helper:DB_Backups.setup_backup_config(). - Recommend using SQLite
backupAPI (full) orVACUUM INTO(incremental) for safe copies.
Notes
- Keep raw SQL within
DB_Managementmodules per project guidelines. - Prefer parameterized queries; avoid logging secrets or full SQL with sensitive values.