Files
zeekay cff8c69948 sdk: test kits, drivers, and the tests that run on them
Adds sdk/sqltest, sdk/sqlcheck and sdk/driver/{postgres,kv,s3} with the
tests they unlock. postgres.Declare names the subsystems that live on
hanzo-sql; the host declares them at start.

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

108 lines
3.7 KiB
Go

package postgres
import (
"context"
"database/sql"
"io"
"sync"
"time"
luxlog "github.com/luxfi/log"
"github.com/hanzoai/cloud/sdk/locus"
"github.com/hanzoai/namespace"
)
// The SDK keeps every store on its own SQLite and learns of a server only through an
// Engine. Linking this package is what turns hanzo-sql on: the host and every plugin
// binary import it for this registration, and CLOUD_SQL_DSN then says where it is.
func init() { locus.Register(engine{}) }
// engine is the SDK's locus.Engine over the functions of this package.
type engine struct{}
// Declare names the subsystems whose stores live on hanzo-sql when a server is
// configured; every other subsystem keeps its own SQLite file. The host declares the
// set once at start, from the apps it mounts.
func Declare(subsystems ...string) {
declaredMu.Lock()
defer declaredMu.Unlock()
for _, s := range subsystems {
declared[s] = struct{}{}
}
}
var (
declaredMu sync.RWMutex
declared = map[string]struct{}{}
)
func (engine) Ported(subsystem string) bool {
declaredMu.RLock()
defer declaredMu.RUnlock()
_, ok := declared[subsystem]
return ok
}
func (engine) Where(ns namespace.Namespace, subsystem, dir string) (locus.Place, error) {
return Where(ns, subsystem, dir)
}
func (engine) WhereAt(ns namespace.Namespace, subsystem, path string) (locus.Place, error) {
return WhereAt(ns, subsystem, path)
}
func (engine) Open(ns namespace.Namespace, subsystem string) (*sql.DB, error) {
return onServer(DSN(), ns, subsystem)
}
func (engine) Guard(ns namespace.Namespace, subsystem string) error { return Guard(ns, subsystem) }
func (engine) Settle(ctx context.Context, log luxlog.Logger, ns namespace.Namespace, subsystem, dir string, open func(*sql.DB) (io.Closer, error)) error {
return moveIfFile(ctx, log, ns, subsystem, dir, open)
}
func (engine) Move(ctx context.Context, log luxlog.Logger, ns namespace.Namespace, subsystem, dir string, open func(*sql.DB) (io.Closer, error)) (locus.Report, error) {
return Move(ctx, log, ns, subsystem, dir, open)
}
func (engine) MoveOnline(ctx context.Context, log luxlog.Logger, ns namespace.Namespace, subsystem, dir string, open func(*sql.DB) (io.Closer, error), on locus.Online) (locus.Report, error) {
return MoveOnline(ctx, log, ns, subsystem, dir, open, on)
}
func (engine) MoveFile(ctx context.Context, log luxlog.Logger, ns namespace.Namespace, subsystem string, f locus.Files) (locus.Report, error) {
return MoveFile(ctx, log, ns, subsystem, f)
}
func (engine) Revert(ctx context.Context, ns namespace.Namespace, subsystem string) error {
return Revert(ctx, ns, subsystem)
}
func (engine) Stores(subsystem string) ([]string, error) { return Stores(subsystem) }
func (engine) Subsystems(ns namespace.Namespace) ([]string, error) { return Subsystems(ns) }
func (engine) Exists(ns namespace.Namespace, subsystem string) (bool, error) {
return Exists(ns, subsystem)
}
func (engine) Lease(ns namespace.Namespace, subsystem, holder string, ttl time.Duration) (bool, time.Duration, error) {
return Lease(ns, subsystem, holder, ttl)
}
func (engine) Unlease(holder string) error { return Unlease(holder) }
func (engine) Slot(ctx context.Context) (func(), error) { return Slot(ctx) }
func (engine) Locks() (*sql.DB, error) { return Locks() }
func (engine) Described(ns namespace.Namespace, subsystem string) (*sql.DB, error) {
return Described(ns, subsystem)
}
func (engine) Placed(ctx context.Context, log luxlog.Logger, ns namespace.Namespace, subsystem, dir, legacy string, prepare func() error, open func(locus.Host) (io.Closer, error)) (locus.Host, error) {
return Placed(ctx, log, ns, subsystem, dir, legacy, prepare, open)
}
func (engine) Scratch(ctx context.Context) (locus.Host, func(), error) { return Scratch(ctx) }