SQL Database Management
Safety
- Backup before migration (mandatory for SQLite, advisory for server engines).
- Abort on failure, roll back transaction, emit actionable error.
- Never partially start with inconsistent schema.
Observability
- Expose schema version via health endpoint (
/readyz or /healthz).
- Log upgrade status at INFO level on startup.
Migration Design
- Migrations embedded in the application, not external SQL files.
- Each migration wrapped in a transaction.
- Naming: sequential version + description (e.g.,
V003_AddUserPreferences).
- Every migration should be idempotent where possible.
- Every migration should be reversible where possible (provide
Down method).
- Never modify a previously released migration — always create a new one.
Cross-Engine Migration
When migrating data between database engines:
- Interactive progress: table name, progress bar (%), elapsed time, ETA (minutes + clock time).
- Batch processing: process in configurable batch sizes to avoid memory exhaustion.
- Per-table error tracking: errors on one table do not abort others.
- Schema detection:
- If target has all tables: offer skip or overwrite (with confirmation).
- If partial: create only missing tables.
- Engine managers must be generic (e.g.,
local_engine_manager.py), not provider-specific.
Connection Management
- Connection pooling: mandatory for server databases (PostgreSQL, MySQL, MSSQL).
- Pool size: configurable, sensible defaults (min: 2, max: 20).
- Connection timeout: configurable (default: 30s).
- Idle timeout: close connections idle > 5 minutes.
- Health check: validate connections before use.
SQL Best Practices
- Parameterized queries ALWAYS (prevent SQL injection).
- Use indexes for frequently queried columns.
- Avoid
SELECT * — specify columns explicitly.
- Use appropriate data types (don't store dates as strings).
- Foreign keys for referential integrity.
- Constraints (NOT NULL, UNIQUE, CHECK) for data integrity.
- Transactions for multi-statement operations.
PostgreSQL Specifics
- Primary server database for the standard stack.
- Use
JSONB for semi-structured data (not JSON).
- Partial indexes for filtered queries.
EXPLAIN ANALYZE for query optimization.
- Connection via
PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD env vars.
SQLite Specifics
- WAL mode enabled for concurrent reads.
PRAGMA journal_mode=WAL;
PRAGMA busy_timeout=5000;
PRAGMA synchronous=NORMAL;
PRAGMA foreign_keys=ON;
- File permissions: 600 (owner read/write only).
SQL Scripts Directory
- SQL utility scripts stored in
SQL/ directory at repo root.
- Naming:
{number}_{description}.sql (e.g., 001_initial_schema.sql).
- These are reference/documentation — runtime migrations are embedded in the app.
Schema Version Table
CREATE TABLE IF NOT EXISTS __schema_version (
version INTEGER NOT NULL,
migration_name TEXT NOT NULL,
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
duration_ms INTEGER,
status TEXT DEFAULT 'success',
checksum TEXT
);
Source: michalagata/AI.Prompts — distributed by TomeVault.
1---2name: skill-sql-database3description: SQL database management — migrations, schema auto-upgrade, PostgreSQL, SQLite, optimization Use when this capability is needed.4---56# SQL Database Management78### Safety910- Backup before migration (mandatory for SQLite, advisory for server engines).11- Abort on failure, roll back transaction, emit actionable error.12- Never partially start with inconsistent schema.1314### Observability1516- Expose schema version via health endpoint (`/readyz` or `/healthz`).17- Log upgrade status at INFO level on startup.1819## Migration Design2021- Migrations embedded in the application, not external SQL files.22- Each migration wrapped in a transaction.23- Naming: sequential version + description (e.g., `V003_AddUserPreferences`).24- Every migration should be **idempotent** where possible.25- Every migration should be **reversible** where possible (provide `Down` method).26- Never modify a previously released migration — always create a new one.2728## Cross-Engine Migration2930When migrating data between database engines:3132- **Interactive progress**: table name, progress bar (%), elapsed time, ETA (minutes + clock time).33- **Batch processing**: process in configurable batch sizes to avoid memory exhaustion.34- **Per-table error tracking**: errors on one table do not abort others.35- **Schema detection**:36 - If target has all tables: offer **skip** or **overwrite** (with confirmation).37 - If partial: create only missing tables.38- Engine managers must be **generic** (e.g., `local_engine_manager.py`), not provider-specific.3940## Connection Management4142- **Connection pooling**: mandatory for server databases (PostgreSQL, MySQL, MSSQL).43- Pool size: configurable, sensible defaults (min: 2, max: 20).44- Connection timeout: configurable (default: 30s).45- Idle timeout: close connections idle > 5 minutes.46- Health check: validate connections before use.4748## SQL Best Practices4950- Parameterized queries ALWAYS (prevent SQL injection).51- Use indexes for frequently queried columns.52- Avoid `SELECT *` — specify columns explicitly.53- Use appropriate data types (don't store dates as strings).54- Foreign keys for referential integrity.55- Constraints (NOT NULL, UNIQUE, CHECK) for data integrity.56- Transactions for multi-statement operations.5758## PostgreSQL Specifics5960- Primary server database for the standard stack.61- Use `JSONB` for semi-structured data (not `JSON`).62- Partial indexes for filtered queries.63- `EXPLAIN ANALYZE` for query optimization.64- Connection via `PGHOST`, `PGPORT`, `PGDATABASE`, `PGUSER`, `PGPASSWORD` env vars.6566## SQLite Specifics6768- WAL mode enabled for concurrent reads.69- `PRAGMA journal_mode=WAL;`70- `PRAGMA busy_timeout=5000;`71- `PRAGMA synchronous=NORMAL;`72- `PRAGMA foreign_keys=ON;`73- File permissions: 600 (owner read/write only).7475## SQL Scripts Directory7677- SQL utility scripts stored in `SQL/` directory at repo root.78- Naming: `{number}_{description}.sql` (e.g., `001_initial_schema.sql`).79- These are reference/documentation — runtime migrations are embedded in the app.8081## Schema Version Table8283```sql84CREATE TABLE IF NOT EXISTS __schema_version (85 version INTEGER NOT NULL,86 migration_name TEXT NOT NULL,87 applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,88 duration_ms INTEGER,89 status TEXT DEFAULT 'success',90 checksum TEXT91);92```9394---95> Source: [michalagata/AI.Prompts](https://github.com/michalagata/AI.Prompts) — distributed by [TomeVault](https://tomevault.io).96<!-- tomevault:4.0:skill_md:2026-05-22 -->