beads/internal/storage/postgres/schema_test.go
Test User 1fc38ba778 feat: choose your own storage backend (Postgres / MySQL / SQLite) — rebased on main
Rebase of PR #4601 (feat/choose-your-own-backend) onto current origin/main,
squashed to a single commit because the original 73-commit history built and then
removed the uowStore derisk spike incrementally, which does not replay cleanly
over the 125 commits main has since advanced (notably #4679, the ready-work
counts fix, which merged to main and is now inherited rather than re-applied).
The full pre-rebase history remains on the original branch tip for reference.

Contents (multi-backend feature + the six review follow-ups):
- bd init --backend=postgres|mysql|sqlite provisioning behind the Dolt-parity
  seam; the three SQL backends share *sqlkit.Store over pluggable dialects and
  carry a generated typed-unsupported shell for the non-core surface.
- Passwords in init DSNs are redacted before persisting to metadata.json (driver
  parser + re-parse verification), with a negative-test matrix proving no secret
  survives (pgdialect + mysql).
- The uowStore derisk spike and its report artifacts are removed; the shell
  generator now lives in internal/storage/unsupportedgen (neutral name).
- Compile-time guard `var _ storage.DoltStorage = (*Store)(nil)` on each backend
  plus a regen-idempotence test keep the generated skip lists honest.
- sqlkit exposes CountReadyWork so every SQL backend satisfies
  storage.ReadyWorkCounter and inherits the shared page-pushed ready-counts path.
- Commercial/product names scrubbed from the feature's own code for vendor
  neutrality.

Rebase reconciliations: adopted main's metadata-load hardening in the store
factories alongside the backend dispatch; combined the config interface
(GetMetadata + ReconcileVersion) and the conformance registrations (ReadyCounts +
Claim/lease); took main's versions of the shared-infra concurrency (uow
doltserver_tx) and delete-cascade (domain/issue_delete) evolutions; implemented
the interface's new UnclaimIssue on sqlkit (delegating to issueops) and added it
to the skip lists.

Verified: go build (cgo + CGO_ENABLED=0) and go vet clean; unit tests
(sqlbuild/issueops/pgdialect/mysql/unsupportedgen) and the SQLite conformance
suite green (RunAll, incl. ReadyCounts equivalence and Claim/lease on the real
sqlkit backend).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-10 17:44:37 +00:00

68 lines
1.9 KiB
Go

package postgres
import (
"context"
"database/sql"
"fmt"
"os"
"testing"
"time"
"github.com/steveyegge/beads/internal/storage/pgdialect"
)
// TestInitSchema applies the embedded DDL into a throwaway schema on a live
// Postgres and asserts the core tables materialize. It is gated on
// BEADS_PG_TEST_URL (a pgx-parseable DSN) and skips otherwise.
func TestInitSchema(t *testing.T) {
url := os.Getenv("BEADS_PG_TEST_URL")
if url == "" {
t.Skip("BEADS_PG_TEST_URL not set; skipping Postgres schema test")
}
schema := fmt.Sprintf("bd_schema_test_%d", time.Now().UnixNano())
// DDL runs over a RAW (non-translating) DB; assertions below use ? bindings
// and so need the translating DB.
raw, err := pgdialect.OpenRaw(url, schema)
if err != nil {
t.Fatalf("openraw: %v", err)
}
db, err := pgdialect.Open(url, schema)
if err != nil {
t.Fatalf("open: %v", err)
}
// Registered first so they run last (cleanups are LIFO): the schema drop
// below still has open connections when it runs.
t.Cleanup(func() { _ = raw.Close() })
t.Cleanup(func() { _ = db.Close() })
ctx := context.Background()
if err := InitSchema(ctx, raw, schema); err != nil {
t.Fatalf("InitSchema: %v", err)
}
t.Cleanup(func() {
if _, err := db.ExecContext(ctx, `DROP SCHEMA IF EXISTS "`+schema+`" CASCADE`); err != nil {
t.Errorf("drop schema %q: %v", schema, err)
}
})
for _, table := range []string{"issues", "dependencies"} {
if !tableExists(ctx, t, db, schema, table) {
t.Errorf("expected table %q to exist in schema %q", table, schema)
}
}
}
func tableExists(ctx context.Context, t *testing.T, db *sql.DB, schema, table string) bool {
t.Helper()
var count int
err := db.QueryRowContext(ctx,
`SELECT count(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ?`,
schema, table).Scan(&count)
if err != nil {
t.Fatalf("query information_schema.tables for %q: %v", table, err)
}
return count > 0
}