- Remove better-sqlite3, add pg driver - Convert schema to PostgreSQL types (serial, timestamp, boolean, jsonb) - Generate fresh PostgreSQL migrations - Update database connection layer with pg.Pool - Fix all API routes: remove JSON.parse/stringify, use native timestamps and booleans - Update drizzle.config.ts and .env.example for PostgreSQL
3.4 KiB
3.4 KiB
1. Dependencies and Configuration
- 1.1 Remove
better-sqlite3and@types/better-sqlite3from package.json - 1.2 Add
pgand@types/pgto package.json dependencies - 1.3 Run
npm installto update node_modules and lockfile - 1.4 Update
drizzle.config.tsto targetpostgresqldialect withDATABASE_URLenv var - 1.5 Update
.env.example— replaceDATABASE_PATHwithDATABASE_URL=postgresql://ml_user:ml_password@postgres:5432/candle_annotator
2. Drizzle Schema Migration (SQLite → PostgreSQL)
- 2.1 Rewrite
src/lib/db/schema.ts— replace allsqliteTablewithpgTable, apply type mappings (integer→serial, integer→timestamp, integer→boolean, real→doublePrecision, text JSON→jsonb) - 2.2 Delete all existing SQLite migration files in
drizzle/directory - 2.3 Run
drizzle-kit generateto produce fresh PostgreSQL migration SQL - 2.4 Review generated migration SQL for correctness
3. Database Connection Layer
- 3.1 Rewrite
src/lib/db/index.ts— replacebetter-sqlite3driver withpg.Pool(max: 10), readDATABASE_URLfrom env, fail if missing - 3.2 Update migration runner to use PostgreSQL-compatible execution (skip during build phase via
NEXT_PHASEcheck) - 3.3 Update all imports if any changed (verify
dbexport still works for API routes)
4. API Route Adjustments
- 4.1 Audit all Next.js API routes using
dbfor SQLite-specific syntax (e.g., integer booleans, raw SQL fragments) - 4.2 Fix any SQLite-specific query patterns to work with PostgreSQL (boolean handling, timestamp handling, jsonb operations)
- 4.3 Update health check endpoint (
/api/health) to verify PostgreSQL connectivity
5. Docker and Deployment
- 5.1 Update
docker-compose.yml— renamePOSTGRES_DBtocandle_annotator, addDATABASE_URLenv to candle-annotator service, adddepends_on: postgreswith health check condition - 5.2 Remove
candle-datavolume fromdocker-compose.yml(SQLite volume) - 5.3 Update
Dockerfileif it references SQLite orDATABASE_PATH - 5.4 Update ML service database connection — change database name from
ml_dbtocandle_annotatorin environment config
6. ML Service Direct Data Access
- 6.1 Add SQLAlchemy table reflections or raw queries in the ML service for reading
candles,annotations,span_annotations,chartstables - 6.2 Update ML training pipeline to query candle/annotation data from PostgreSQL instead of CSV/JSON exports
- 6.3 Remove or deprecate any CSV/JSON export code paths that are no longer needed
7. Data Migration Script
- 7.1 Create
scripts/migrate-sqlite-to-postgres.ts— read all 6 tables from SQLite, apply type conversions (timestamps, booleans, JSON→jsonb), insert into PostgreSQL - 7.2 Make the script idempotent (skip or clear+re-insert with flag)
- 7.3 Test migration script with existing SQLite data
8. Testing and Verification
- 8.1 Run the full application locally with PostgreSQL — verify all API routes work
- 8.2 Verify ML service can query candle/annotation data from shared database
- 8.3 Run
docker compose upand verify all services start correctly with new configuration - 8.4 Update
DEPLOYMENT.mdwith new deployment steps (PostgreSQL migration, data migration script, rollback procedure) - 8.5 Update
README.mdandCLAUDE_DESCRIPTION.mdwith database architecture changes