Files
framework/postgres_test.go
zeekayandGitHub 3206f5140c store: the engine runs on PostgreSQL (hanzo-sql) as well as SQLite (#3)
A host that keeps its stores on a server passes Config.Postgres with the handle it opens.
The engine then writes numbered placeholders, keeps a document's data in JSONB, migrates
in one transaction under an advisory lock (CREATE TABLE IF NOT EXISTS is not safe against
itself), reads the schema from information_schema, and leaves the pragmas and the single
connection to the host. SQLite is unchanged: ? placeholders, a text column, the same DDL.

Both engines use ->> to look inside a document (CAST(data->>? AS TEXT)), where SQLite had
json_extract; times and counters are BIGINT; docstatus filters are typed for the column;
the series upsert names its table (the column was ambiguous); a lock or series key, which
joins its parts with NUL, is stored in hex on PostgreSQL, which cannot hold NUL in text.

Testing: the whole suite runs on both engines. FRAMEWORK_TEST_POSTGRES_DSN names a server;
each test gets a schema of its own. go test ./... passes on SQLite and on a throwaway
PostgreSQL 16, 0 failures on either.
Risk: a custom sort field now orders as text on SQLite too (it was the native JSON type);
doctype, document, naming and lock behaviour is unchanged on SQLite (suite green).
How to revert: git revert this commit.

Claude-Session: https://claude.ai/code/session_015XYKLn69gF4x75RWePpDBV
2026-11-21 00:17:43 -08:00

59 lines
1.5 KiB
Go

package framework
import (
"crypto/rand"
"database/sql"
"encoding/hex"
"os"
"testing"
_ "github.com/jackc/pgx/v5/stdlib"
luxlog "github.com/luxfi/log"
)
// testDSN names a PostgreSQL server the suite runs against as well as SQLite: a postgres://
// URL whose role may create schemas. Each test gets a schema of its own, dropped after it.
//
// FRAMEWORK_TEST_POSTGRES_DSN='postgres://me@/db?host=/run/postgresql&sslmode=disable' go test ./...
const testDSN = "FRAMEWORK_TEST_POSTGRES_DSN"
// testDB is where a test's store lives: a file on SQLite, or a schema on the server
// testDSN names. open is nil on SQLite.
func testDB(t *testing.T) (open OpenDBFunc, pg bool) {
t.Helper()
dsn := os.Getenv(testDSN)
if dsn == "" {
return nil, false
}
var b [6]byte
_, _ = rand.Read(b[:])
schema := "fw_" + hex.EncodeToString(b[:])
admin, err := sql.Open("pgx", dsn)
if err != nil {
t.Fatalf("postgres: %v", err)
}
if _, err := admin.Exec(`CREATE SCHEMA ` + schema); err != nil {
t.Fatalf("create schema: %v", err)
}
t.Cleanup(func() {
_, _ = admin.Exec(`DROP SCHEMA ` + schema + ` CASCADE`)
_ = admin.Close()
})
return func(string) (*sql.DB, error) {
db, err := sql.Open("pgx", dsn+"&search_path="+schema)
if err != nil {
return nil, err
}
return db, nil
}, true
}
// testConfig is an engine's configuration on the test engine.
func testConfig(t *testing.T) Config {
t.Helper()
open, pg := testDB(t)
return Config{Dir: t.TempDir(), OpenDB: open, Postgres: pg}
}
func withLogger(c Config, l luxlog.Logger) Config { c.Logger = l; return c }