diff --git a/.planning/phases/03-postgresql-support/03-01-PLAN.md b/.planning/phases/03-postgresql-support/03-01-PLAN.md index 47c52ca..ddb4569 100644 --- a/.planning/phases/03-postgresql-support/03-01-PLAN.md +++ b/.planning/phases/03-postgresql-support/03-01-PLAN.md @@ -18,8 +18,9 @@ must_haves: truths: - "PostgresStore implements all 9 Store interface methods with PostgreSQL SQL syntax" - "PostgreSQL baseline migration creates the same 3 tables as SQLite (updates, tags, tag_assignments)" - - "RunMigrations is renamed to RunSQLiteMigrations for symmetry; RunPostgresMigrations exists for PostgreSQL" + - "RunMigrations is renamed to RunSQLiteMigrations in migrate.go; RunPostgresMigrations exists for PostgreSQL" - "Existing SQLite migration path is unchanged (backward compatible)" + - "Application compiles and all existing tests pass after adding PostgreSQL support code" artifacts: - path: "pkg/diunwebhook/postgres_store.go" provides: "PostgresStore struct implementing Store interface" @@ -139,16 +140,12 @@ Import alias: `sqlitemigrate "github.com/golang-migrate/migrate/v4/database/sqli - pkg/diunwebhook/migrate.go (current RunMigrations implementation to rename) - pkg/diunwebhook/migrations/sqlite/0001_initial_schema.up.sql (schema to translate) - pkg/diunwebhook/migrations/sqlite/0001_initial_schema.down.sql (down migration to copy) - - pkg/diunwebhook/export_test.go (calls RunMigrations - must update call site) - - cmd/diunwebhook/main.go (calls RunMigrations - must update call site) - go.mod (current dependencies) pkg/diunwebhook/migrations/postgres/0001_initial_schema.up.sql, pkg/diunwebhook/migrations/postgres/0001_initial_schema.down.sql, pkg/diunwebhook/migrate.go, - pkg/diunwebhook/export_test.go, - cmd/diunwebhook/main.go, go.mod, go.sum @@ -201,6 +198,7 @@ Import alias: `sqlitemigrate "github.com/golang-migrate/migrate/v4/database/sqli 4. Rewrite `pkg/diunwebhook/migrate.go`: - Rename `RunMigrations` to `RunSQLiteMigrations` (per RESEARCH.md recommendation) + - IMPORTANT: Only rename the function definition in migrate.go itself. Do NOT touch cmd/diunwebhook/main.go or pkg/diunwebhook/export_test.go — those call-site renames are handled in Plan 02. - Add a second `//go:embed migrations/postgres` directive for `var postgresMigrations embed.FS` - Add `RunPostgresMigrations(db *sql.DB) error` using `pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5"` as the database driver - The pgx migrate driver name string for `migrate.NewWithInstance` is `"pgx5"` (NOT "pgx" or "postgres" -- this is the registration name used by golang-migrate's pgx/v5 sub-package) @@ -221,14 +219,12 @@ Import alias: `sqlitemigrate "github.com/golang-migrate/migrate/v4/database/sqli ``` - RunPostgresMigrations body follows the exact same pattern as RunSQLiteMigrations but uses `postgresMigrations`, `"migrations/postgres"`, `pgxmigrate.WithInstance`, and `"pgx5"` as the database name -5. Update the two call sites that reference `RunMigrations`: - - `cmd/diunwebhook/main.go` line 29: change `diun.RunMigrations(db)` to `diun.RunSQLiteMigrations(db)` - - `pkg/diunwebhook/export_test.go` line 12 and line 24: change `RunMigrations(db)` to `RunSQLiteMigrations(db)` +5. Because migrate.go renames `RunMigrations` to `RunSQLiteMigrations` but the call sites in main.go and export_test.go still reference the old name, the build will break temporarily. This is expected — Plan 02 (wave 2) updates those call sites. To verify this plan in isolation, the verify command uses `go build ./pkg/diunwebhook/` (package only, not `./...`) and `go vet ./pkg/diunwebhook/`. 6. Run `go mod tidy` to clean up go.sum. - cd /home/jean-luc-makiola/Development/projects/DiunDashboard && go build ./... && go test -v -count=1 ./pkg/diunwebhook/ -run TestWebhookHandler 2>&1 | head -30 + cd /home/jean-luc-makiola/Development/projects/DiunDashboard && go build ./pkg/diunwebhook/ && go vet ./pkg/diunwebhook/ - pkg/diunwebhook/migrations/postgres/0001_initial_schema.up.sql contains `SERIAL PRIMARY KEY` @@ -241,13 +237,11 @@ Import alias: `sqlitemigrate "github.com/golang-migrate/migrate/v4/database/sqli - pkg/diunwebhook/migrate.go contains `//go:embed migrations/postgres` - pkg/diunwebhook/migrate.go contains `pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5"` - pkg/diunwebhook/migrate.go contains `"pgx5"` (driver name in NewWithInstance call) - - cmd/diunwebhook/main.go contains `RunSQLiteMigrations` (not RunMigrations) - - pkg/diunwebhook/export_test.go contains `RunSQLiteMigrations` (not RunMigrations) - go.mod contains `github.com/jackc/pgx/v5` - - `go build ./...` exits 0 - - `go test -count=1 ./pkg/diunwebhook/` exits 0 (all existing SQLite tests still pass) + - `go build ./pkg/diunwebhook/` exits 0 + - `go vet ./pkg/diunwebhook/` exits 0 - PostgreSQL migration files exist with correct dialect. RunMigrations renamed to RunSQLiteMigrations. RunPostgresMigrations added. pgx/v5 dependency in go.mod. All existing tests pass unchanged. + PostgreSQL migration files exist with correct dialect. RunMigrations renamed to RunSQLiteMigrations in migrate.go. RunPostgresMigrations added. pgx/v5 dependency in go.mod. Package builds and vets cleanly. @@ -378,7 +372,7 @@ func NewPostgresStore(db *sql.DB) *PostgresStore { **IMPORTANT: No mutex.Lock/Unlock anywhere in PostgresStore** (per D-05). No `sync.Mutex` field in the struct. - cd /home/jean-luc-makiola/Development/projects/DiunDashboard && go build ./... && go vet ./pkg/diunwebhook/ + cd /home/jean-luc-makiola/Development/projects/DiunDashboard && go build ./pkg/diunwebhook/ && go vet ./pkg/diunwebhook/ - pkg/diunwebhook/postgres_store.go contains `type PostgresStore struct` @@ -398,7 +392,7 @@ func NewPostgresStore(db *sql.DB) *PostgresStore { - pkg/diunwebhook/postgres_store.go contains `SetMaxOpenConns(25)` (constructor pool config) - pkg/diunwebhook/postgres_store.go does NOT contain `sync.Mutex` (no mutex for PostgreSQL) - pkg/diunwebhook/postgres_store.go does NOT contain `mu.Lock` (no mutex) - - `go build ./...` exits 0 + - `go build ./pkg/diunwebhook/` exits 0 - `go vet ./pkg/diunwebhook/` exits 0 PostgresStore implements all 9 Store interface methods with PostgreSQL-native SQL. No mutex. Pool settings configured. CreateTag uses RETURNING id. AssignTag uses ON CONFLICT DO UPDATE. Code compiles and passes vet. @@ -407,18 +401,18 @@ func NewPostgresStore(db *sql.DB) *PostgresStore { -1. `go build ./...` succeeds (both stores compile, migrate.go compiles with both drivers) -2. `go test -v -count=1 ./pkg/diunwebhook/` passes (all existing SQLite tests unchanged) -3. `go vet ./pkg/diunwebhook/` clean -4. PostgresStore has all 9 methods matching Store interface (compiler enforces this) -5. Migration files exist in both `migrations/sqlite/` and `migrations/postgres/` +1. `go build ./pkg/diunwebhook/` succeeds (both stores compile, migrate.go compiles with both drivers) +2. `go vet ./pkg/diunwebhook/` clean +3. PostgresStore has all 9 methods matching Store interface (compiler enforces this) +4. Migration files exist in both `migrations/sqlite/` and `migrations/postgres/` +5. Note: `go build ./...` and full test suite will fail until Plan 02 updates call sites in main.go and export_test.go that still reference the old `RunMigrations` name. This is expected. -- PostgresStore compiles and implements Store interface (go build succeeds) -- All existing SQLite tests pass (RunMigrations rename did not break anything) +- PostgresStore compiles and implements Store interface (go build ./pkg/diunwebhook/ succeeds) - PostgreSQL migration creates identical table structure to SQLite (3 tables: updates, tags, tag_assignments) - pgx/v5 is in go.mod as a direct dependency +- migrate.go exports both RunSQLiteMigrations and RunPostgresMigrations diff --git a/.planning/phases/03-postgresql-support/03-02-PLAN.md b/.planning/phases/03-postgresql-support/03-02-PLAN.md index 0038152..7da1553 100644 --- a/.planning/phases/03-postgresql-support/03-02-PLAN.md +++ b/.planning/phases/03-postgresql-support/03-02-PLAN.md @@ -21,6 +21,7 @@ must_haves: - "Docker Compose with --profile postgres activates a PostgreSQL service" - "Default docker compose (no profile) remains SQLite-only" - "Duplicate tag creation returns 409 on both SQLite and PostgreSQL" + - "Existing SQLite users can upgrade to this version with zero configuration changes and no data loss" artifacts: - path: "cmd/diunwebhook/main.go" provides: "DATABASE_URL branching logic" @@ -55,7 +56,7 @@ must_haves: Wire PostgresStore into the application and deployment infrastructure. -Purpose: Connects the PostgresStore (built in Plan 01) to the startup path, adds Docker Compose profiles for PostgreSQL deployments, creates build-tagged integration test helpers, and fixes the UNIQUE constraint detection to work across both database backends. +Purpose: Connects the PostgresStore (built in Plan 01) to the startup path, adds Docker Compose profiles for PostgreSQL deployments, creates build-tagged integration test helpers, and fixes the UNIQUE constraint detection to work across both database backends. Also updates all call sites that still reference the old `RunMigrations` name (renamed to `RunSQLiteMigrations` in Plan 01). Output: Updated main.go with DATABASE_URL branching, compose files with postgres profiles, build-tagged test helper, cross-dialect error handling fix. @@ -106,18 +107,19 @@ func NewServer(store Store, webhookSecret string) *Server - Task 1: Wire DATABASE_URL branching in main.go and fix cross-dialect UNIQUE detection + Task 1: Wire DATABASE_URL branching in main.go, update call sites, and fix cross-dialect UNIQUE detection - - cmd/diunwebhook/main.go (current SQLite-only startup to add branching) - - pkg/diunwebhook/diunwebhook.go (TagsHandler line 172 - UNIQUE detection to fix) + - cmd/diunwebhook/main.go (current SQLite-only startup to rewrite with branching) + - pkg/diunwebhook/diunwebhook.go (TagsHandler - UNIQUE detection to fix) + - pkg/diunwebhook/export_test.go (calls RunMigrations - must rename to RunSQLiteMigrations) - pkg/diunwebhook/postgres_store.go (verify NewPostgresStore exists from Plan 01) - pkg/diunwebhook/migrate.go (verify RunSQLiteMigrations and RunPostgresMigrations exist from Plan 01) - cmd/diunwebhook/main.go, pkg/diunwebhook/diunwebhook.go + cmd/diunwebhook/main.go, pkg/diunwebhook/diunwebhook.go, pkg/diunwebhook/export_test.go -**1. Update `cmd/diunwebhook/main.go`** to branch on `DATABASE_URL` per D-07, D-08, D-09. +**1. Rewrite `cmd/diunwebhook/main.go`** with DATABASE_URL branching per D-07, D-08, D-09. -Replace the current database setup block (lines 18-33) with DATABASE_URL branching. The full main function should: +Replace the current database setup block with DATABASE_URL branching. The full main function should: ```go package main @@ -174,13 +176,17 @@ func main() { Key changes: - Add blank import `_ "github.com/jackc/pgx/v5/stdlib"` to register "pgx" driver name - `DATABASE_URL` present -> `sql.Open("pgx", databaseURL)` -> `RunPostgresMigrations` -> `NewPostgresStore` -- `DATABASE_URL` absent -> existing SQLite path with `RunSQLiteMigrations` (renamed in Plan 01) +- `DATABASE_URL` absent -> existing SQLite path with `RunSQLiteMigrations` (renamed from `RunMigrations` in Plan 01) - Log `"Using PostgreSQL database"` or `"Using SQLite database at %s"` per D-09 - Keep all existing code after the store setup unchanged (secret, server, mux, httpSrv, shutdown) -**2. Fix cross-dialect UNIQUE constraint detection in `pkg/diunwebhook/diunwebhook.go`.** +**2. Update `pkg/diunwebhook/export_test.go`** to use the renamed function. -In the `TagsHandler` method, line 172, change: +Change all occurrences of `RunMigrations(db)` to `RunSQLiteMigrations(db)` in export_test.go. This completes the rename that Plan 01 started in migrate.go. + +**3. Fix cross-dialect UNIQUE constraint detection in `pkg/diunwebhook/diunwebhook.go`.** + +In the `TagsHandler` method, change: ```go if strings.Contains(err.Error(), "UNIQUE") { ``` @@ -203,12 +209,13 @@ Why: SQLite errors contain uppercase "UNIQUE" (e.g., `UNIQUE constraint failed: - cmd/diunwebhook/main.go contains `log.Printf("Using SQLite database at %s", dbPath)` - cmd/diunwebhook/main.go contains `_ "github.com/jackc/pgx/v5/stdlib"` - cmd/diunwebhook/main.go contains `diun.RunSQLiteMigrations(db)` (not RunMigrations) + - pkg/diunwebhook/export_test.go contains `RunSQLiteMigrations` (not RunMigrations) - pkg/diunwebhook/diunwebhook.go contains `strings.Contains(strings.ToLower(err.Error()), "unique")` - pkg/diunwebhook/diunwebhook.go does NOT contain `strings.Contains(err.Error(), "UNIQUE")` (old pattern removed) - `go build ./...` exits 0 - - `go test -count=1 ./pkg/diunwebhook/` exits 0 + - `go test -v -count=1 ./pkg/diunwebhook/` exits 0 (full test suite passes) - main.go branches on DATABASE_URL to select PostgreSQL or SQLite. pgx/v5/stdlib is blank-imported to register the driver. Startup log identifies the active backend. UNIQUE detection is case-insensitive for cross-dialect compatibility. All existing tests pass. + main.go branches on DATABASE_URL to select PostgreSQL or SQLite. pgx/v5/stdlib is blank-imported to register the driver. Startup log identifies the active backend. export_test.go updated with RunSQLiteMigrations. UNIQUE detection is case-insensitive for cross-dialect compatibility. All existing tests pass. @@ -349,7 +356,7 @@ func NewTestPostgresServer() (*Server, error) { This file is in the `diunwebhook` package (internal, same as export_test.go pattern). The `//go:build postgres` tag ensures it only compiles when explicitly requested with `go test -tags postgres`. Without the tag, `go test ./pkg/diunwebhook/` skips this file entirely -- no pgx import, no PostgreSQL dependency. - cd /home/jean-luc-makiola/Development/projects/DiunDashboard && go build ./... && docker compose config --quiet 2>&1; echo "exit: $?" + cd /home/jean-luc-makiola/Development/projects/DiunDashboard && go build ./... && go test -v -count=1 ./pkg/diunwebhook/ 2>&1 | tail -5 - compose.yml contains `profiles:` under the postgres service @@ -371,7 +378,7 @@ This file is in the `diunwebhook` package (internal, same as export_test.go patt - pkg/diunwebhook/postgres_test.go contains `NewPostgresStore(db)` - pkg/diunwebhook/postgres_test.go contains `TEST_DATABASE_URL` - `go build ./...` exits 0 (postgres_test.go is not compiled without build tag) - - `go test -count=1 ./pkg/diunwebhook/` exits 0 (SQLite tests still pass, postgres_test.go skipped) + - `go test -v -count=1 ./pkg/diunwebhook/` exits 0 (full SQLite test suite passes, postgres_test.go skipped) Docker Compose files support optional PostgreSQL via profiles. Default deploy remains SQLite-only. Build-tagged test helper exists for PostgreSQL integration testing. Dockerfile needs no changes (pgx/v5 is pure Go).