Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
940a9137b4 | ||
|
|
0ba1dcfef0 |
No files matched your search
@@ -47,6 +47,10 @@ jobs:
|
||||
platforms: linux/amd64
|
||||
push: false
|
||||
|
||||
- name: Test native PostgreSQL 17 before publishing
|
||||
shell: bash
|
||||
run: bash tools/ci/test-postgres.sh
|
||||
|
||||
- name: Log in to registry
|
||||
uses: https://github.com/docker/login-action@184bdaa0721073962dff0199f1fb9940f07167d1 # v3.5.0
|
||||
with:
|
||||
|
||||
@@ -20,6 +20,7 @@ data/*
|
||||
.tmp/
|
||||
.deployment-backups/
|
||||
*.sh
|
||||
!tools/ci/test-postgres.sh
|
||||
assets/editor/node_modules/
|
||||
assets/city-generator/node_modules/
|
||||
assets/excalidraw-viewer/node_modules/
|
||||
|
||||
@@ -16,6 +16,13 @@ import (
|
||||
// All role changes are operator-only and offline. Never expose this as a
|
||||
// domain-admin API: a data directory contains multiple campuses and domains.
|
||||
func runDR(args []string, out io.Writer) error {
|
||||
backend, err := configuredDatabaseBackend()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if backend != "sqlite" {
|
||||
return errors.New("DR commands require the SQLite backend; use PostgreSQL backup/restore for native instances")
|
||||
}
|
||||
const usage = "pageswiki dr {init|status|snapshot|verify|restore|promote|resume-workers} [flags]\nUse COMMAND --help. Snapshot requires all writers stopped; promotion requires external fencing."
|
||||
if len(args) == 0 || args[0] == "--help" {
|
||||
_, err := fmt.Fprintln(out, usage)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net/url"
|
||||
@@ -14,18 +15,30 @@ import (
|
||||
|
||||
const managedCampusReconcileInterval = 5 * time.Second
|
||||
|
||||
func startManagedCampusReconciler(dataDir string, st *store.Store) {
|
||||
func startManagedCampusReconciler(ctx context.Context, dataDir string, st *store.Store) <-chan struct{} {
|
||||
done := make(chan struct{})
|
||||
instanceFQDN := strings.TrimSpace(os.Getenv("PAGESWIKI_INSTANCE_FQDN"))
|
||||
if instanceFQDN == "" {
|
||||
return
|
||||
if instanceFQDN == "" || st.ReadOnly || st.BackgroundJobsDisabled {
|
||||
close(done)
|
||||
return done
|
||||
}
|
||||
go func() {
|
||||
defer close(done)
|
||||
ticker := time.NewTicker(managedCampusReconcileInterval)
|
||||
defer ticker.Stop()
|
||||
for range ticker.C {
|
||||
reconcileManagedCampuses(dataDir, st, instanceFQDN)
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
reconcileManagedCampusesContext(ctx, dataDir, st, instanceFQDN)
|
||||
}
|
||||
}
|
||||
}()
|
||||
return done
|
||||
}
|
||||
|
||||
func reconcileManagedCampuses(
|
||||
@@ -33,12 +46,19 @@ func reconcileManagedCampuses(
|
||||
st *store.Store,
|
||||
instanceFQDN string,
|
||||
) {
|
||||
reconcileManagedCampusesContext(context.Background(), dataDir, st, instanceFQDN)
|
||||
}
|
||||
|
||||
func reconcileManagedCampusesContext(ctx context.Context, dataDir string, st *store.Store, instanceFQDN string) {
|
||||
campuses, err := st.ListCampuses()
|
||||
if err != nil {
|
||||
log.Printf("managed Campus reconciliation: %v", err)
|
||||
return
|
||||
}
|
||||
for _, campus := range campuses {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
if campus.AuthorityMode != "managed" || !campus.Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
+23
-56
@@ -1,18 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/dr"
|
||||
"pageswiki/internal/server"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "migrate-postgres" {
|
||||
if err := runPostgreSQLMigration(os.Args[2:], os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 1 && os.Args[1] == "schema" {
|
||||
if err := runSchema(os.Args[2:], os.Stdout); err != nil {
|
||||
log.Print(err)
|
||||
@@ -29,6 +37,8 @@ func main() {
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
enrollment, err := parseExternalCampusEnrollment(os.Args[1:])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -64,6 +74,13 @@ func main() {
|
||||
if err = dr.RequireRole(node, os.Getenv("PAGESWIKI_DR_EXPECT_ROLE")); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
backend, err := configuredDatabaseBackend()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if backend == "postgresql" && node != nil {
|
||||
log.Fatal("PostgreSQL currently supports standalone instances only; DR startup refused")
|
||||
}
|
||||
if node != nil && node.Role == "standby" {
|
||||
if len(os.Args) > 1 {
|
||||
log.Fatal("standby does not accept enrollment/startup mutation flags")
|
||||
@@ -79,73 +96,23 @@ func main() {
|
||||
}
|
||||
st.StandbyReadUntil = node.ReadUntil()
|
||||
log.Printf("DR standby: migrations, federation reconciliation and background workers disabled; snapshot=%s", node.SnapshotID)
|
||||
if err := server.New(st).Run(":3000"); err != nil {
|
||||
if err := serveInstance(ctx, dataDir, st, ":3000"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
dbPath :=
|
||||
filepath.Join(
|
||||
dataDir,
|
||||
"pages.db",
|
||||
)
|
||||
|
||||
log.Printf(
|
||||
"pages.db = %s",
|
||||
dbPath,
|
||||
)
|
||||
|
||||
database, err :=
|
||||
db.Open(dbPath)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = db.Migrate(database)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
documentRoot, err :=
|
||||
store.MigrateLegacyDocumentRoot(
|
||||
dataDir,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
st :=
|
||||
store.NewWithDocumentRoot(
|
||||
database,
|
||||
documentRoot,
|
||||
)
|
||||
|
||||
err = st.EnsureConfigTable()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = st.EnsurePathLinkColumns()
|
||||
|
||||
log.Printf("database backend = %s", backend)
|
||||
database, st, err := openApplicationStore(ctx, dataDir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer database.Close()
|
||||
if err := enrollExternalCampus(dataDir, st, enrollment); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
st.BackgroundJobsDisabled = node != nil && !node.WorkersEnabled
|
||||
if !st.BackgroundJobsDisabled {
|
||||
startManagedCampusReconciler(dataDir, st)
|
||||
}
|
||||
|
||||
srv := server.New(st)
|
||||
|
||||
err = srv.Run(":3000")
|
||||
err = serveInstance(ctx, dataDir, st, ":3000")
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/dr"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func runPostgreSQLMigration(args []string, out io.Writer) error {
|
||||
f := flag.NewFlagSet("migrate-postgres", flag.ContinueOnError)
|
||||
f.SetOutput(out)
|
||||
defaultDir := os.Getenv("PAGESWIKI_DATA_DIR")
|
||||
if defaultDir == "" {
|
||||
defaultDir = "./data"
|
||||
}
|
||||
dir := f.String("data-dir", defaultDir, "stopped instance data directory; existing files are retained in place")
|
||||
apply := f.Bool("apply", false, "copy into an empty PostgreSQL schema after source preflight")
|
||||
insecure := f.Bool("allow-insecure", false, "allow unverified PostgreSQL TLS for isolated testing only")
|
||||
timeout := f.Duration("timeout", 30*time.Minute, "migration timeout")
|
||||
if err := f.Parse(args); err != nil {
|
||||
if errors.Is(err, flag.ErrHelp) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
if f.NArg() != 0 || *timeout <= 0 {
|
||||
return errors.New("invalid migration arguments")
|
||||
}
|
||||
if info, err := os.Stat(*dir); err != nil || !info.IsDir() {
|
||||
return errors.New("existing data directory required")
|
||||
}
|
||||
lock, err := dr.Lock(*dir, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer lock.Close()
|
||||
if node, err := dr.Load(*dir); err != nil {
|
||||
return err
|
||||
} else if node != nil {
|
||||
return errors.New("PostgreSQL migration does not support a configured DR instance")
|
||||
}
|
||||
source, err := db.OpenReadOnly(filepath.Join(*dir, "pages.db"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer source.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
||||
defer cancel()
|
||||
tables, err := store.InspectSQLiteApplicationImport(ctx, source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !*apply {
|
||||
return json.NewEncoder(out).Encode(map[string]any{"mode": "preflight", "tables": tables, "copied": false})
|
||||
}
|
||||
target, err := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: os.Getenv("PAGESWIKI_POSTGRES_DSN"), AllowInsecure: *insecure})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer target.Close()
|
||||
root := filepath.Join(*dir, "documents")
|
||||
if err = store.PreparePostgreSQLImport(ctx, target, root); err != nil {
|
||||
return err
|
||||
}
|
||||
report, err := store.ImportSQLiteApplication(ctx, source, target, root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return json.NewEncoder(out).Encode(report)
|
||||
}
|
||||
|
||||
func configuredDatabaseBackend() (string, error) {
|
||||
b := strings.ToLower(strings.TrimSpace(os.Getenv("PAGESWIKI_DB_BACKEND")))
|
||||
switch b {
|
||||
case "", "sqlite":
|
||||
return "sqlite", nil
|
||||
case "postgresql":
|
||||
return b, nil
|
||||
default:
|
||||
return "", errors.New("PAGESWIKI_DB_BACKEND must be sqlite or postgresql")
|
||||
}
|
||||
}
|
||||
|
||||
func openApplicationStore(ctx context.Context, dataDir string) (database *sql.DB, st *store.Store, err error) {
|
||||
backend, err := configuredDatabaseBackend()
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer func() {
|
||||
if err != nil && database != nil {
|
||||
database.Close()
|
||||
database = nil
|
||||
}
|
||||
}()
|
||||
if backend == "postgresql" {
|
||||
database, err = db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: os.Getenv("PAGESWIKI_POSTGRES_DSN")})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
root := filepath.Join(dataDir, "documents")
|
||||
if err = store.VerifyPostgreSQLImport(ctx, database, root); err != nil {
|
||||
return database, nil, err
|
||||
}
|
||||
st, err = store.NewPostgreSQLWithDocumentRoot(ctx, database, root)
|
||||
return database, st, err
|
||||
}
|
||||
database, err = db.Open(filepath.Join(dataDir, "pages.db"))
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if err = db.Migrate(database); err != nil {
|
||||
return database, nil, err
|
||||
}
|
||||
root, err := store.MigrateLegacyDocumentRoot(dataDir)
|
||||
if err != nil {
|
||||
return database, nil, err
|
||||
}
|
||||
st = store.NewWithDocumentRoot(database, root)
|
||||
if err = st.EnsureConfigTable(); err != nil {
|
||||
return database, nil, err
|
||||
}
|
||||
err = st.EnsurePathLinkColumns()
|
||||
return database, st, err
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/google/uuid"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/dr"
|
||||
"pageswiki/internal/server"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestPostgreSQLMigrationAndTLSStartup(t *testing.T) {
|
||||
dsn := os.Getenv("PAGESWIKI_TEST_POSTGRES_TLS_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("set a disposable PostgreSQL verify-full DSN")
|
||||
}
|
||||
ctx := context.Background()
|
||||
admin, err := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: dsn})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schema := "pageswiki_cli_test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
if _, err = admin.Exec(`CREATE SCHEMA "` + schema + `"`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _, _ = admin.Exec(`DROP SCHEMA "` + schema + `" CASCADE`); admin.Close() })
|
||||
u, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
q := u.Query()
|
||||
q.Set("search_path", schema)
|
||||
u.RawQuery = q.Encode()
|
||||
t.Setenv("PAGESWIKI_POSTGRES_DSN", u.String())
|
||||
t.Setenv("PAGESWIKI_AUTH_KEY", "")
|
||||
dir := t.TempDir()
|
||||
database, err := db.Open(filepath.Join(dir, "pages.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := store.NewWithDocumentRoot(database, filepath.Join(dir, "documents"))
|
||||
if err = s.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domain, err := s.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owner, err := s.BootstrapOwner(domain.UUID, "owner", "synthetic cli password", "Owner", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := s.CreateAuthSession(domain.UUID, owner.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = database.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
if err = runPostgreSQLMigration([]string{"--data-dir", dir, "--apply"}, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PAGESWIKI_DB_BACKEND", "postgresql")
|
||||
t.Chdir("../..")
|
||||
for range 2 {
|
||||
database, native, err := openApplicationStore(ctx, dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := native.PrincipalForSession(domain.UUID, token); err != nil || got.UUID != owner.UUID {
|
||||
database.Close()
|
||||
t.Fatal("session lost", err)
|
||||
}
|
||||
srv, err := server.NewChecked(native)
|
||||
if err != nil {
|
||||
database.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
closeCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
err = srv.Close(closeCtx)
|
||||
cancel()
|
||||
database.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLMigrationPreflight(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
database, err := db.Open(filepath.Join(dir, "pages.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := store.NewWithDocumentRoot(database, filepath.Join(dir, "documents"))
|
||||
if err = s.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = database.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// No target is needed for the schema-only preflight.
|
||||
t.Setenv("PAGESWIKI_POSTGRES_DSN", "")
|
||||
var out bytes.Buffer
|
||||
if err = runPostgreSQLMigration([]string{"--data-dir", dir}, &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var result struct {
|
||||
Mode string
|
||||
Copied bool
|
||||
Tables []string
|
||||
}
|
||||
if err = json.Unmarshal(out.Bytes(), &result); err != nil || result.Mode != "preflight" || result.Copied || len(result.Tables) < 130 {
|
||||
t.Fatal(result, err)
|
||||
}
|
||||
lock, err := dr.Lock(dir, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer lock.Close()
|
||||
if err = runPostgreSQLMigration([]string{"--data-dir", dir}, &out); err == nil {
|
||||
t.Fatal("active instance maintenance accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLBackendRefusesFallback(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for _, backend := range []string{"unsupported", "postgresql"} {
|
||||
t.Setenv("PAGESWIKI_DB_BACKEND", backend)
|
||||
t.Setenv("PAGESWIKI_POSTGRES_DSN", "")
|
||||
if _, _, err := openApplicationStore(context.Background(), dir); err == nil {
|
||||
t.Fatal("invalid configuration accepted")
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, "pages.db")); !os.IsNotExist(err) {
|
||||
t.Fatal("SQLite fallback created")
|
||||
}
|
||||
if err := runSchema([]string{"status", "--data-dir", dir}, &bytes.Buffer{}); err == nil {
|
||||
t.Fatal("legacy inspection accepted")
|
||||
}
|
||||
if err := runDR([]string{"init", "--data-dir", dir}, &bytes.Buffer{}); err == nil {
|
||||
t.Fatal("legacy DR accepted")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLMigrationArguments(t *testing.T) {
|
||||
for _, args := range [][]string{{"--timeout", "0"}, {"unexpected"}, {"--data-dir", filepath.Join(t.TempDir(), "missing")}} {
|
||||
if err := runPostgreSQLMigration(args, &bytes.Buffer{}); err == nil {
|
||||
t.Fatal("invalid arguments accepted", args)
|
||||
}
|
||||
}
|
||||
}
|
||||
+51
-2
@@ -12,19 +12,28 @@ import (
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/metadata"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
var errSchemaPending = errors.New("schema migrations are pending; no changes were applied")
|
||||
var errSchemaBlocked = errors.New("schema history or contract is incompatible; no changes were applied")
|
||||
var errPostgreSQLNotReady = errors.New("core-only inspection cannot certify application migration; use migrate-postgres; no changes were applied")
|
||||
|
||||
func runSchema(args []string, out io.Writer) error {
|
||||
const usage = "pageswiki schema {status|check} [--db FILE | --data-dir DIR] [--json] [--timeout 30s]\nRead-only inspection of managed components; legacy migrations and pending DDL are not rehearsed.\ncheck exits 2 for pending migrations, 1 for incompatible state, 0 when managed components are current."
|
||||
backend, err := configuredDatabaseBackend()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if backend != "sqlite" {
|
||||
return errors.New("legacy schema inspection requires SQLite; PostgreSQL startup validates native migrations and import receipt")
|
||||
}
|
||||
const usage = "pageswiki schema {status|check|portability} [--db FILE | --data-dir DIR] [--json] [--timeout 30s]\nRead-only inspection; no migration is performed.\ncheck exits 2 for pending migrations, 1 for incompatible state, 0 when managed components are current.\nportability reports PostgreSQL migration blockers and exits 1 while full application support is incomplete."
|
||||
if len(args) == 0 || args[0] == "--help" {
|
||||
_, err := fmt.Fprintln(out, usage)
|
||||
return err
|
||||
}
|
||||
if args[0] != "status" && args[0] != "check" {
|
||||
if args[0] != "status" && args[0] != "check" && args[0] != "portability" {
|
||||
return errors.New(usage)
|
||||
}
|
||||
f := flag.NewFlagSet("schema "+args[0], flag.ContinueOnError)
|
||||
@@ -65,6 +74,46 @@ func runSchema(args []string, out io.Writer) error {
|
||||
defer database.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
||||
defer cancel()
|
||||
if args[0] == "portability" {
|
||||
repository, err := metadata.New(database, metadata.SQLite)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
report, err := repository.InspectPortability(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if *jsonOutput {
|
||||
encoder := json.NewEncoder(out)
|
||||
encoder.SetIndent("", " ")
|
||||
if err := encoder.Encode(report); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if _, err := fmt.Fprintf(out, "PostgreSQL application ready: %t\nPortable core rehearsal compatible: %t\nScope: %s\n", report.ApplicationReady, report.CoreCompatible, report.Scope); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, table := range report.Tables {
|
||||
if _, err := fmt.Fprintf(out, "%-28s %-20s %s\n", table.Name, table.Component, table.State); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, blocker := range report.Blockers {
|
||||
if _, err := fmt.Fprintln(out, "blocked:", blocker); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !report.ApplicationReady {
|
||||
if _, err := fmt.Fprintln(out, "This legacy diagnostic covers core rehearsal only. Use migrate-postgres for full application preflight and transfer."); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
if !report.ApplicationReady {
|
||||
return errPostgreSQLNotReady
|
||||
}
|
||||
return nil
|
||||
}
|
||||
report, err := store.InspectSchema(ctx, database)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -9,9 +9,60 @@ import (
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/metadata"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func TestPortabilityCommandIsReadOnlyAndNeverClaimsApplicationReady(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
database, err := db.Open(filepath.Join(dir, "pages.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer database.Close()
|
||||
if err := db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := store.NewWithDocumentRoot(database, filepath.Join(dir, "documents"))
|
||||
if _, err := st.CreatePageAtPath("/preserved"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := database.Exec(`CREATE TABLE private_extension_data(secret TEXT); INSERT INTO private_extension_data VALUES ('DO_NOT_PRINT_PRIVATE_VALUE')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var out bytes.Buffer
|
||||
if err := runSchema([]string{"portability", "--db", filepath.Join(dir, "pages.db"), "--json"}, &out); !errors.Is(err, errPostgreSQLNotReady) {
|
||||
t.Fatal("unsafe readiness result", err)
|
||||
}
|
||||
var report metadata.PortabilityReport
|
||||
if err := json.Unmarshal(out.Bytes(), &report); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if report.ApplicationReady || report.CoreCompatible || len(report.Blockers) == 0 {
|
||||
t.Fatal("legacy application marked portable", report)
|
||||
}
|
||||
if bytes.Contains(out.Bytes(), []byte("DO_NOT_PRINT_PRIVATE_VALUE")) {
|
||||
t.Fatal("row values leaked")
|
||||
}
|
||||
if _, err := st.ResolvePath("/preserved"); err != nil {
|
||||
t.Fatal("inspection changed existing data", err)
|
||||
}
|
||||
var count int
|
||||
if err := database.QueryRow(`SELECT count(*) FROM schema_migration_steps WHERE component='core-metadata'`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("inspection migrated database", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPortabilityCommandMissingDatabaseDoesNotCreateFiles(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "not-created")
|
||||
if err := runSchema([]string{"portability", "--data-dir", dir}, &bytes.Buffer{}); err == nil {
|
||||
t.Fatal("nonexistent DB accepted")
|
||||
}
|
||||
if _, err := os.Stat(dir); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatal("directory created", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSchemaCommandMissingDatabaseDoesNotInitialize(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "missing")
|
||||
var out bytes.Buffer
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"pageswiki/internal/server"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
// Let an in-flight federation reconciliation finish before releasing the
|
||||
// event-worker owner. Each remote request retains its existing network timeout;
|
||||
// cancellation prevents starting another campus or reconciliation pass.
|
||||
func serveInstance(ctx context.Context, dataDir string, st *store.Store, addr string) error {
|
||||
srv, err := server.NewChecked(st)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reconcileCtx, stopReconciler := context.WithCancel(ctx)
|
||||
reconcilerDone := startManagedCampusReconciler(reconcileCtx, dataDir, st)
|
||||
serveCtx, stopServer := context.WithCancel(context.Background())
|
||||
finished := make(chan struct{})
|
||||
go func() {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
<-reconcilerDone
|
||||
stopServer()
|
||||
case <-finished:
|
||||
}
|
||||
}()
|
||||
err = srv.RunContext(serveCtx, addr)
|
||||
stopReconciler()
|
||||
<-reconcilerDone
|
||||
close(finished)
|
||||
stopServer()
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/store"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestServeInstanceCancelled(t *testing.T) {
|
||||
t.Chdir("../..")
|
||||
t.Setenv("PAGESWIKI_INSTANCE_FQDN", "")
|
||||
root := t.TempDir()
|
||||
database, err := db.Open(root + "/pages.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer database.Close()
|
||||
if err = db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st := store.NewWithDocumentRoot(database, root+"/documents")
|
||||
if err = st.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st.BackgroundJobsDisabled = true
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if err = serveInstance(ctx, root, st, "127.0.0.1:0"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = database.Ping(); err != nil {
|
||||
t.Fatal("server closed caller-owned DB", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagedReconcilerCancellationBeforeFirstPass(t *testing.T) {
|
||||
t.Setenv("PAGESWIKI_INSTANCE_FQDN", "synthetic.invalid")
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
// No DB: cancellation must finish without attempting a repository read or
|
||||
// accessing an external campus. This test does not make a network request.
|
||||
done := startManagedCampusReconciler(ctx, t.TempDir(), &store.Store{})
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("reconciler did not stop")
|
||||
}
|
||||
}
|
||||
@@ -312,6 +312,9 @@ class PagesWikiCLI:
|
||||
)
|
||||
|
||||
def __init__(self) -> None:
|
||||
backend = os.environ.get("PAGESWIKI_DB_BACKEND", "sqlite").strip().lower() or "sqlite"
|
||||
if backend != "sqlite":
|
||||
raise CLIError("Local data CLI requires SQLite; use the HTTP pages-cli for PostgreSQL")
|
||||
script_dir = Path(__file__).resolve().parent
|
||||
self.data_dir = Path(
|
||||
os.environ.get("PAGESWIKI_DATA_DIR", script_dir)
|
||||
|
||||
@@ -10,6 +10,11 @@ from pageswiki_cli_test import pageswiki_cli
|
||||
|
||||
|
||||
class DRGuardTest(unittest.TestCase):
|
||||
def test_postgresql_rejected_before_database_open(self):
|
||||
with patch.dict(os.environ, {"PAGESWIKI_DB_BACKEND": "postgresql"}):
|
||||
with self.assertRaisesRegex(pageswiki_cli.CLIError, "HTTP pages-cli"):
|
||||
pageswiki_cli.PagesWikiCLI()
|
||||
|
||||
def test_standby_rejected_before_database_open(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# 保存基盤の多様化と運用機能の統合
|
||||
|
||||
状態:詳細設計・段階導入。2026-09-27。
|
||||
状態:詳細設計・段階導入。2026-11-15 更新。
|
||||
本書は [基本設計](arch.md) と [拡張契約](platform-extension-architecture.md) を補完する。
|
||||
各 TODO は実装済みを意味しない。本番 DB・保存先の移行は別途承認する。
|
||||
|
||||
@@ -384,6 +384,35 @@ setup 完了後は token 再利用不可。機密欄の再表示・ログ出力
|
||||
|
||||
### S2:外部 backend
|
||||
|
||||
- [x] PostgreSQL 接続層と独立した migration runner(アプリの backend 切替は未接続)。
|
||||
- [x] Object/Path/Revision/Asset の共通 metadata Repository と空 DB 用スキーマ、SQLite/PostgreSQL 共通契約テスト。
|
||||
- [x] 既存 Store の Campus/Domain 取得・ホスト解決・ドメイン別設定・Campus 作成/更新の native SQL 移植と共通契約テスト。
|
||||
- [x] 管理者グループの初期化・メンバー更新・継承切替・projection・派生所属とセッションの失効を共通 SQL と transaction に移植。
|
||||
- [x] 一般グループの作成・一覧・メンバー一覧・Path 権限設定、直接 Role binding と Group 権限の付与判定、Principal 状態更新と失効を共通 SQL に移植。
|
||||
- [x] 初回 owner・パスワードアカウント・プロフィール・セッション管理、TOTP、外部認証の設定と Identity 紐付け、CLI 認証引き継ぎの DB 処理を移植し、SQLite/PostgreSQL 共通契約テストを追加。
|
||||
- [x] 認可と読み取りナビゲーションの Locality 制限、City 所有者・公開範囲の上限制約、ユーザー/グループ ACL 更新、保護 Path/City 構造の DB ガードを移植。
|
||||
- [x] 公開 manifest・自動公開・匿名 navigation、承認要求、ユーザー領域とプロフィール連絡先の公開制御を native SQL に移植し、共通 DB テストを追加。
|
||||
- [x] Domain 作成/更新/削除と Campus 削除、管理者継承、連携トークン・instance 登録・署名 manifest の DB 処理を移植。
|
||||
- [x] 通常 Revision 保存・履歴取得・履歴復元の Store 境界と HTTP 呼び出しを native metadata Repository に接続し、合成データで保存・競合・故障時の rollback を検証。
|
||||
- [x] Path の再帰移動・コピー・snapshot 付き一括削除、alias/primary link と navigator の DB 処理を移植。
|
||||
- [x] City map・所属 Page・Path context・health・監査履歴の取得、タイトル・境界変更、Spot 整理(移動・統合)を移植。
|
||||
- [x] Asset の一覧・参照・履歴、作成/更新・復元・削除を移植し、楽観ロックと履歴保存失敗時の rollback を検証。
|
||||
- [x] City の個別作成・配置・relation・再帰削除、一括変更計画の preview/apply、自動生成候補と名前競合の問い合わせを移植。
|
||||
- [x] Browser・children・Path 補完・権限付き Object metadata 一覧を移植し、native canonical HTML の H1 と複数 Store 間の children cache 失効を検証。
|
||||
- [x] アーカイブ import のセッション・計画・journal・Path rollback を移植。全 DB の SQLite→PostgreSQL インポートとは別機能。
|
||||
- [x] タスク・プロジェクトの保存/取得/履歴/階層付き一覧/削除、DAV resource・collection・資格情報・既定リスト名の Store 操作を移植し、両 DB で契約を検証。
|
||||
- [x] タスク個人設定、作業時間の開始・終了・訂正・取消と監査履歴、Pomodoro と日次集計、ダッシュボード配置・達成率、予定枠の DB 予約を移植。
|
||||
- [x] 通知 Inbox の保存・重複排除・取得・既読、期限/着手日の通知配信、タイマー完了、活動 journal の outbox 補完処理を移植。scheduler と Push 配信は別途接続する。
|
||||
- [x] タスク capture と Wiki 同時作成、削除後の明示的再作成、プロジェクト配下のメモ作成を移植。受付記録・本文参照・Path・outbox の原子性を検証。
|
||||
- [x] 日次メールの送信予約、本人の同意・権限確認、送信履歴、明示的再試行、保持期限処理を移植。SMTP worker と scheduler は別途統合する。
|
||||
- [x] カレンダーの再構築可能な月別索引、予定の取得・更新・削除、DAV resource 操作、タスク予定枠と日次予定の cache 参照を移植。正本 ICS と manifest の形式は維持する。
|
||||
- [x] 連絡先の保存・検索・削除・履歴復元・export、CardDAV の resource 名・差分同期・metadata inventory と資格情報失効を移植。
|
||||
- [x] ブログ・日記の設定、記事作成・登録・解除・一覧、投稿受付の重複排除、公開タイトルと別 Domain への射影設定を移植。本文と埋め込み Asset 参照の固定、監査失敗時の rollback を検証。
|
||||
- [x] RSS/FreshRSS の購読元・既読/スター・記事 cache・分類・購読 import・Wiki 取り込みと受付記録、外部操作の claim・確認・履歴を移植。scheduler 登録は別途接続する。
|
||||
- [ ] 通常 HTTP/scheduler/CLI 起動を経由した認証・公開・federation の PostgreSQL 統合試験。
|
||||
- [x] 読み取り専用の移植診断 CLI と、対応済み core の catalog / 履歴検査。
|
||||
- [x] 停止状態の core SQLite→PostgreSQL→SQLite 往復リハーサル、内容指紋・採番・参照・Document の検証。
|
||||
- [ ] Object/Path/Revision/Asset の全 Store 操作と関連正本更新の接続、既存 DB 移行、共通統合テスト。
|
||||
- [ ] PostgreSQL の全 repository 移植と SQLite との共通機能テスト。
|
||||
- [ ] MySQL の DDL/索引/照合順序/同時書き込み試験。
|
||||
- [ ] S3 adapter と互換サービスで range/条件付き作成/破損/切断/容量制限を検証。
|
||||
@@ -391,6 +420,790 @@ setup 完了後は token 再利用不可。機密欄の再表示・ログ出力
|
||||
- [ ] Redis cache adapter と停止時の制限付き縮退、policy epoch による失効。
|
||||
- [ ] mutable canonical CAS と metadata 参照の回復プロトコル、HA の fencing。
|
||||
|
||||
#### PostgreSQL 対応の実装順序
|
||||
|
||||
2026-11-14 時点では `internal/db.OpenPostgreSQL` と
|
||||
`ApplyPostgreSQLMigrations` を追加した。既存の `Open`、`Migrate`、通常の Store 初期化、
|
||||
通常起動、DR、CLI は引き続き SQLite 用であり、PostgreSQL を本番選択可能にはしない。
|
||||
利用者の DB を自動変換せず、既存 migration の checksum も変更しない。
|
||||
|
||||
接続には pgx の database/sql 実装を使用し、DSN はインスタンスの秘密設定として扱う。
|
||||
既定で検証付き TLS と平文 fallback の禁止を要求する。開発専用の
|
||||
`AllowInsecure` を明示した場合だけ非検証接続を許す。接続確認の timeout、
|
||||
pool の上限、connection の寿命を設け、失敗時は pool を閉じる。
|
||||
エラーは SQLSTATE と操作名だけを公開し、DSN、SQL、サーバの詳細メッセージや
|
||||
constraint 内の個人情報をエラー文字列へ含めない。
|
||||
|
||||
PostgreSQL 用 migration は SQLite の SQL 自動翻訳ではなく、独立した append-only 定義とする。
|
||||
BIGINT の epoch ms、ネイティブの `$1` 引数、明示した conflict target を使う。
|
||||
runner は READ COMMITTED transaction と共通の transaction-level advisory lock で
|
||||
ledger の初回作成を含めて直列化する。異なる component にも同じロックを使う。
|
||||
DDL、checksum 履歴、最新 assertion の検証を同じ transaction に含め、
|
||||
失敗時は rollback、正常終了時は commit と同時にロックを解放する。
|
||||
未知の新しい履歴、欠けた version、変更された定義は適用を停止する。
|
||||
|
||||
| 順序 | 範囲 | 完了条件 |
|
||||
| --- | --- | --- |
|
||||
| 1 | 接続と migration | 実 PostgreSQL で並行適用、rollback、履歴不一致、lock 待ちキャンセルを検証 |
|
||||
| 2 | Object/Path/Revision/Asset | Domain 分離、UUID 不変性、revision 保存、再帰 rename、hash 参照の SQLite 共通契約を通過 |
|
||||
| 3 | 認証と ACL | ユーザー、グループ、セッション、公開範囲の共通契約を通過 |
|
||||
| 4 | タスクと CalDAV | プロジェクト所属、VTODO、ETag と競合更新、通知を検証 |
|
||||
| 5 | ジョブと通知 | claim token、lease、row lock、outbox と重複処理耐性を検証 |
|
||||
| 6 | City と検索と管理 | rowid/PRAGMA/FTS5 依存を分離し、安定した順序・検索・schema 診断を検証 |
|
||||
| 7 | 既存データ移行と運用 | 停止状態の export/import、件数・参照・hash 検査、restore rehearsal、起動設定と readiness を接続 |
|
||||
|
||||
#### 既存 Store の Campus と Domain と設定の移植
|
||||
|
||||
`internal/store` に backend を保持し、既存 API から native PostgreSQL の SQL を使える
|
||||
経路を追加した。既定値は SQLite とし、従来の Store constructor と最小 fixture の
|
||||
振る舞いを維持する。SQL の `?` を一括変換せず、query 構築時に placeholder を生成する。
|
||||
ホスト名の判定は SQLite の `instr` と PostgreSQL の `strpos` を明示的に分ける。
|
||||
|
||||
移植済みの範囲は次のとおり。
|
||||
|
||||
- Campus の既定値取得、UUID 取得、一覧、作成と更新。
|
||||
- Domain の既定値取得、UUID 取得、一覧、件数、Host 解決、実効 FQDN の取得。
|
||||
- Domain ごとの設定の取得と UPSERT。欠けた設定は従来どおり空文字とし、更新時刻は既存の秒単位を維持。
|
||||
- 既存 Store の Path 解決で backend を選択し、Domain が local であることを確認して共通 Repository を呼ぶ。
|
||||
|
||||
Campus の FQDN と Domain の相対ホスト名・FQDN 指定を同じ規則で解決する。
|
||||
未知の Host を既定 Domain へ fallback せず、無効な Campus/Domain と remote Domain の
|
||||
ローカル名前空間へのアクセスを既存の取得規則に従って拒否する。
|
||||
Campus 作成・更新では、移植済み writer が同じ namespace lock を使う範囲で、
|
||||
ホスト名競合の検査と書き込みを一つの transaction に入れる。
|
||||
PostgreSQL では同一 DB の advisory lock、SQLite では書き込みロックを検査前に取得する。
|
||||
transaction の期限とキャンセルは内部の SQL 呼び出しまで伝搬する。
|
||||
|
||||
Domain/Campus の context 付き取得 API を追加し、HTTP のリクエスト単位の Host 解決でも
|
||||
request context を利用する。PostgreSQL の SQL/driver/Scan エラーから個人情報や設定値を
|
||||
出力しないようにし、入力フォームへ返せる固定の validation message は区別して保持する。
|
||||
|
||||
PostgreSQL の初期スキーマは `store-namespaces` migration として独立させる。
|
||||
対象は `campuses`、`domains`、`config`、`personal_appearance` のテーブル定義と索引であり、
|
||||
後者の個人外観 API の移植まで完了したという意味ではない。
|
||||
既知の列不足や適用済み定義の不一致は停止し、既存 SQLite の ALTER/adopt 処理は流用しない。
|
||||
既定 Campus/Domain の初期化は transaction で直列化し、並行実行時の重複と関連付けの欠落を防ぐ。
|
||||
これは旧インスタンスの全データを取り込む import 処理ではない。
|
||||
|
||||
Domain 作成・更新は Campus/実効 Host/配置先の検査、Domain 本体、既定 locale、
|
||||
管理者グループ作成と所属 projection を同じ管理用 writer lock と transaction に含める。
|
||||
同じ実効 Host の同時作成は一つだけ成功し、別 Campus への移動は旧 Campus 由来の管理者権限を取り消す。
|
||||
依存テーブルや projection の障害時は Domain と設定も rollback する。
|
||||
Domain/Campus 削除の参照・最終要素検査も transaction 内で実施する。
|
||||
Campus 削除では連携 token、instance と capability、署名 manifest の checkpoint も削除する。
|
||||
|
||||
`store-federation` は token hash、instance identity、manifest 採番と受信 checkpoint を保持する。
|
||||
登録と token 失効は同じ lock を使用し、登録後に失効済み token を再利用できない。
|
||||
署名の検証後、初回 controller fingerprint の固定、旧 revision と同一 revision の異なる内容の拒否、
|
||||
受信 Campus/Domain/管理者継承と checkpoint の保存を一つの transaction で扱う。
|
||||
`store-domain-entry` は manifest v2 の許可済み entry 設定と日記/ブログの有効化・prefix を保持する。
|
||||
これは日記/ブログ本文の新規作成・検索・projection repository 全体の移植完了ではない。
|
||||
|
||||
現時点の PostgreSQL Store は component 統合テスト内でのみ構築する。
|
||||
本番用 constructor と起動フラグはまだ解放せず、通常のサーバーは SQLite のままとする。
|
||||
core 限定インポートの対象も拡張しておらず、namespace を含む DB は引き続き対象外として拒否する。
|
||||
|
||||
#### 認証と管理者グループの移植
|
||||
|
||||
`store-auth-foundation` の native migration は Principal、Identity、Domain membership、
|
||||
Role、直接 Role binding、Capability grant、Group、Group member と Campus 管理者継承の
|
||||
provenance を定義する。アバターは BYTEA、時刻は BIGINT の Unix milliseconds、
|
||||
識別子と一意制約の比較は C collation とする。既存テーブルの暗黙の取り込みや修復は行わない。
|
||||
適用済み migration の列検査が失敗した場合も既存 Principal を保持したまま停止する。
|
||||
|
||||
既存 Store の `IsBuiltinGroupAdmin` と `IsCampusAdmin` は SQLite と PostgreSQL の
|
||||
両方で動作する。context 付き API は SQL エラーと拒否を区別し、従来の bool API は
|
||||
DB 障害時も権限を与えない。Domain と membership の対応、Principal・membership・Group
|
||||
の有効状態、Domain の enabled、owner/manager role を同じ SQL で検査する。
|
||||
SQLite と実 PostgreSQL の共通テストで状態変更、越境所属、キャンセル、DB 障害を検証する。
|
||||
|
||||
`EnsureBuiltinAdminGroups`、メンバーの追加・更新・削除、`SetCampusAdminInheritance`、
|
||||
`ProjectCampusAdminMemberships` は両 DB 共通の実装を使う。
|
||||
グループの名前・UUID による取得と member role の参照も backend に対応する。
|
||||
PostgreSQL では namespace 更新と同じ advisory lock、SQLite では読み取り前の
|
||||
DB writer lock を使い、元の grant 更新と継承先の再計算を同一 transaction に含める。
|
||||
context 付き API の期限は内部の SQL まで伝搬する。
|
||||
|
||||
継承の入力は独立した grant に限り、派生 grant を新たな継承元にしない。
|
||||
複数の継承元のうち最も強い role を実効値とし、継承解除時には弱いローカル role が再び現れる。
|
||||
派生した membership のみを追跡し、独立した group/role binding で採用済みの所属と session は保持する。
|
||||
派生権限しかない所属は削除し、その Domain の session を同じ transaction で失効する。
|
||||
再有効化しても失効済み session は復活させない。
|
||||
最後の独立 owner の並行削除、継承先での inherited-only grant 削除、owner の通常更新による降格は拒否する。
|
||||
Role からの初期 grant は記録済みの migration marker で再投入を防ぎ、明示的に取り消した権限を
|
||||
再起動で復活させない。Domain の異なる Role binding と別 Domain を指定する manage grant は初期 owner にしない。
|
||||
|
||||
native migration の v2 には失効処理に必要な `auth_sessions` と索引を追加する。
|
||||
管理者継承の共通テストは合成 session 行で取り消しと保持を検査し、
|
||||
projection 後半の故障注入でメンバー追加・削除・継承切替のすべてが rollback することを確認する。
|
||||
|
||||
一般グループの作成は owner の有効な Domain membership と active な Principal を確認し、
|
||||
Group、owner member、派生 membership の独立所属への採用を同一 transaction に含める。
|
||||
一覧の `domain` visibility は単に Principal UUID があるだけでなく、その Domain で有効な所属を要求する。
|
||||
`members` visibility とメンバー一覧、role 参照、Path 権限評価でも membership の Domain 一致を検査する。
|
||||
別 Domain の不正な owner 行を最後の独立 owner の代わりに数えない。
|
||||
一覧は EXISTS で重複を避け、表示名・名前・UUID による安定順を持たせる。
|
||||
|
||||
native migration の v3 は `group_permissions` とメンバーの Identity 参照用索引を追加する。
|
||||
権限設定は既存 Group を要求し、同じ Group/Path の設定を upsert する。
|
||||
`read`、`read_write_once`、`read_write`、`upload_only` の既存 capability と literal Path 境界を保持する。
|
||||
SQL の `%` と `_` を wildcard として扱わず、readonly Principal は read だけを許す。
|
||||
|
||||
直接 Role binding の付与判定も backend に対応し、Domain の異なる Role を拒否する。
|
||||
exact Object モードでは Domain 全体や wildcard の grant を採用しない。
|
||||
この低水準判定は Locality 制限を代替しない。`AuthorizeContext` と
|
||||
`AuthorizeExactObjectContext` は Role/Group grant と Locality cap、Object の別名・別 Domain 制限を
|
||||
同じ read-only transaction で評価する。PostgreSQL は REPEATABLE READ を用い、
|
||||
ナビゲーション用 `LoadReadPathPolicyContext` も同じ snapshot 契約を持つ。
|
||||
公開コンテンツの匿名読み取りとユーザー領域の公開制約も、下記の同一 snapshot 契約を使う。
|
||||
アカウント作成時の Role/binding 書き込みは下記の transaction に含める。
|
||||
それ以外の federation・管理操作からの Role/binding 書き込みは別途移植する。
|
||||
|
||||
`SetPrincipalState` は管理者継承と同じ writer lock を利用する。
|
||||
グローバルな Principal の状態更新、active 以外への変更による全 Domain の session 失効、
|
||||
派生管理者権限の再計算を同じ transaction に含め、失敗時は全体を rollback する。
|
||||
既に失効した session の失効時刻を上書きせず、active へ戻しても session は復活させない。
|
||||
|
||||
アカウント作成は Principal、Identity、Credential、Domain membership、Role/binding を
|
||||
同じ transaction に含める。初回 owner は管理者グループへの projection まで含め、
|
||||
途中の失敗時にはアカウントも残さない。並行 bootstrap は一つだけ成功する。
|
||||
password hash の計算は writer lock を取得する前に行う。
|
||||
native migration v4〜v7 は Credential、Challenge、Provider 設定、CLI 認証引き継ぎを追加する。
|
||||
v8 は既存の Group/Role mapping と認証監査ログ用テーブルを追加する。監査イベントの新しい発行機能を追加する変更ではない。
|
||||
適用済み migration の SQL/checksum は変更しない。
|
||||
|
||||
ブラウザ/API session の発行は停止・継承失効と同じ writer lock を利用する。
|
||||
token の用途 prefix を hash 対象に含め、Domain 境界と有効な所属を検査する。
|
||||
プロフィールと avatar の読み書き、Domain のアカウント一覧、session 一覧・失効も両 DB に対応する。
|
||||
|
||||
TOTP の登録確定と counter 消費は同じ transaction 内で検証・更新する。
|
||||
SQLite の JSON 関数を PostgreSQL 上で代用せず、Go 側で counter を読み、writer lock で
|
||||
再利用を防ぐ。外部認証の設定保存では、空の secret を指定した更新は UPSERT 内で
|
||||
現在の暗号化 secret を保持する。外部 Identity の競合で別 Principal に紐付け直さず、
|
||||
重複するメールアドレスや表示名は曖昧なマッチとして拒否する。
|
||||
|
||||
CLI の認証要求は同時受付でも件数上限を超えないよう transaction で直列化する。
|
||||
承認消費と API session 発行は一括 commit し、発行失敗時には承認も rollback する。
|
||||
同時 exchange の成功は一つに限り、poll 間隔・origin・Domain・trusted choice を保持する。
|
||||
共通テストは合成データだけを用い、実際の LDAP/OIDC/SMTP サーバーには接続しない。
|
||||
|
||||
`store-city-foundation` は階層・所有者・公開設定・監査記録・公開 manifest の保存用 schema を持つ。
|
||||
`CityGovernance`、所有者候補検索、`ChangeCityGovernance`、`LocalityAccess` は native SQL を使う。
|
||||
変更前に管理者継承と同じ advisory lock を取得し、epoch/topology の比較、上位の公開範囲、
|
||||
所有者の委任権限を検査する。preview は rollback し、実変更は policy・epoch・監査を一括 commit する。
|
||||
上位管理者の ACL を下位所有者が広げることは拒否し、対象者の削除などの縮小だけを許可する。
|
||||
|
||||
`store-locality-foundation` は Path と設定のガードに加え、Zone/Spot/Street所属の変更を
|
||||
保護する PostgreSQL trigger を持つ。ACL のある Domain で enforcing を解除できず、
|
||||
保護 Path の移動・削除・別名による脱出を拒否する。新規 Object を保護範囲内に作成することは許可する。
|
||||
Path の比較には C collation の文字列範囲を使い、`%`、`_`、大小文字、日本語を LIKE の wildcard としない。
|
||||
trigger を無効化・削除した状態は migration の assertion と ACL 変更時の検査で拒否する。
|
||||
将来の City/namespace writer も、行ロックより先に同じ管理用 lock を取得する順序に統一する。
|
||||
|
||||
共通テストでは上位公開範囲の制約、所有者委任、ACL 縮小、同時変更の単一成功、
|
||||
preview の非永続化、監査失敗時の rollback、保護 namespace、失効後の次リクエストを確認する。
|
||||
schema の追加は City 全体の対応完了を意味しない。通常 HTTP ハンドラとの PostgreSQL 統合試験、
|
||||
City 全変更系と他の管理経路からの Role/binding 操作の監査は残る。
|
||||
既存 SQLite の他の mutation から呼ばれる旧 transaction wrapper は
|
||||
SQLite 用のままであり、それらの PostgreSQL 化は呼び出し側の lock 契約も含めて行う。
|
||||
`EnsureAuthSchema` は PostgreSQL の認証・Locality schema 初期化へ接続済み。
|
||||
管理対象 migration の未移植 component は `ErrPostgreSQLComponentNotReady` で拒否し、
|
||||
SQLite migration runner を PostgreSQL 接続へ流さない。本番用 PostgreSQL Store は未公開のままとする。
|
||||
|
||||
#### 公開とユーザー領域の移植
|
||||
|
||||
`store-publication` は公開 manifest の失効と navigation generation 用 trigger を持つ。
|
||||
匿名 Revision 読み取りと navigation は、明示承認と自動公開候補を同一 read-only snapshot で確認する。
|
||||
有効な Campus/Domain、HEAD、hash、別名、公開範囲、公開者の状態を検査し、
|
||||
本文更新・別名追加・権限変更後の次リクエストに失効を反映する。
|
||||
上位の手動承認必須設定は下位の自動公開を止める。
|
||||
PostgreSQL の navigation trigger は statement 単位で更新し、generation を行数ではなく失効 token として扱う。
|
||||
|
||||
`store-publication-approval` の要求作成は domain/idempotency key の競合を UPSERT で処理する。
|
||||
承認は状態・generation・期限を検査し、decision 記録と状態更新を一括 commit する。
|
||||
同時承認の成功は一つだけで、期限切れ整理ジョブの実行前でも期限切れ要求は承認できない。
|
||||
|
||||
`store-user-space` は profile/avatar/root/descendant の公開設定、Path ごとの縮小例外、監査記録を持つ。
|
||||
公開読み取りは有効な本人所属と Domain の上限、本文 HEAD を同じ snapshot で評価し、
|
||||
別名付き Object は匿名公開しない。例外 Path の改名は同じ所有者の namespace に制限し、
|
||||
改名・削除に policy を追従させる。policy 更新と監査記録は一括 commit する。
|
||||
`store-profile-contacts` は電話番号を Principal の一般情報と分離し、
|
||||
本人の同意・profile 公開設定・Domain の上限を満たす場合だけ匿名へ公開する。
|
||||
競合した version は拒否し、非公開時は値だけでなく version/visibility も返さない。
|
||||
|
||||
既存 SQLite の同一時刻 Revision は `rowid` で HEAD を決定する一方、native PostgreSQL は UUID を用いる。
|
||||
新規 native 保存は時刻を単調増加させるが、旧 DB の同時刻データにはその保証がない。
|
||||
全件 import は言語別 HEAD とその参照先を移行前後で照合し、順序の保存方法を確定するまで
|
||||
本番切替を許可しない。core の往復試験だけでこの条件を満たしたと判断しない。
|
||||
|
||||
#### 共通 metadata Repository の実装範囲
|
||||
|
||||
`internal/metadata` は Object 作成、Domain ごとの Path 解決と alias、再帰移動、
|
||||
言語別 Revision HEAD の比較更新、履歴 UUID の参照と非破壊復元、Asset HEAD と
|
||||
履歴の原子的更新を提供する。SQL の placeholder を組み立て時に生成し、
|
||||
SQL 文字列の一括置換は行わない。DB には Document 本文を保持せず、
|
||||
低水準の `AppendRevision` は呼び出し側が Geduldh へ先に検証・永続化した SHA-256 を公開する。
|
||||
`SaveDocument` は参照固定と payload 永続化も含めた保存パイプラインを提供する。
|
||||
Revision の `body` 列は既存形式との橋渡し用の空キャッシュであり、正本ではない。
|
||||
|
||||
PostgreSQL の書き込みは Domain 単位の transaction-level advisory lock を取得し、
|
||||
Revision ではその後に Object 単位の lock も取得する。lock の取得順を統一し、
|
||||
異なる Object の保存でも Domain 内の outbox sequence を直列化する。
|
||||
アプリ用の `NewApplication` は、その前に namespace 管理と共通の advisory lock を取り、
|
||||
有効な Campus と Domain、local 配置を transaction 内で再確認する。
|
||||
順序は namespace 管理 → Domain → Object とする。本文の検証・永続化 I/O 中には取得しない。
|
||||
SQLite は読み取り前に書き込みロックを取得する。
|
||||
全 writer が同じ契約を使う前提で、プロセス内の mutex に依存せずに HEAD を比較する。
|
||||
Revision の時刻は同一言語の前の値より必ず後にし、順序付けに隠れた rowid を使わない。
|
||||
復元は新しい Revision UUID で追加し、過去の Revision を変更しない。
|
||||
埋め込み Document の参照も履歴から引き継ぎ、現在の Asset HEAD へ置き換えない。
|
||||
|
||||
再帰移動は移動先が空であることを検査し、一つの UPDATE で配下を移動する。
|
||||
全 Path のメモリ展開や一件ずつの SQL 発行はしない。Path は SQLite の binary、
|
||||
PostgreSQL の C collation とし、配下の検査は索引を使える文字列範囲で行う。
|
||||
日本語、大小文字、`%`、`_`、仮想フォルダ、同名の別 Domain を区別する。
|
||||
同一 Domain 内の alias 追加は既存 Path を上書きしない。Domain を越えるリンクの
|
||||
取り扱いは認可・共有の移植時に別途実装する。
|
||||
|
||||
`InitializeFresh` は専用 migration 履歴で管理する空 DB/schema 用の初期化である。
|
||||
既存の未管理テーブルがある場合は採用や変換をせず失敗し、既存データを保持する。
|
||||
再実行時には必要なテーブルと列の存在を SELECT assertion で検査し、不足時は停止する。
|
||||
列型、照合順序、索引、一意制約の完全な catalog 検査はまだ対象外である。
|
||||
既存 Store の初期化からは呼ばない。Store の PostgreSQL 分岐には Path 解決、
|
||||
Object/Page 作成、通常 Revision 保存と履歴復元を接続した。アプリ用の Page 作成は
|
||||
Object・origin・Path と `page.created` を一括 commit し、イベント保存失敗時は全て rollback する。
|
||||
`NewApplication` 自体は通常起動の constructor や認可処理ではない。
|
||||
|
||||
保存パイプラインの実装と、変更系のアプリ接続前の残件は次のとおり。
|
||||
|
||||
- [x] DB 非依存の Asset freeze を切り出し、既存 SQLite Store からも利用。
|
||||
- [x] 共通 Repository の `SaveDocument` で Document 永続化、埋め込み Asset 検証、参照記録を接続。
|
||||
- [x] 共通 Repository の Revision 保存と activity outbox の追加を同じ transaction に統合。
|
||||
- [x] 通常保存・復元 HTTP API を Domain 明示の Store 境界へ接続し、PostgreSQL 分岐から新パイプラインを呼び出す。
|
||||
- [x] 言語別 HEAD・履歴 cursor・Document 参照一覧の native SQL と欠損 payload の失敗処理を接続。
|
||||
- [x] 通常の Path 再帰変更、native filename Revision とコピー時の埋め込み参照保持、navigator の追従を接続。
|
||||
- [x] Asset Store API の native SQL と履歴復元を接続。
|
||||
- [ ] Web/API/CLI の変更系を新保存パイプラインへ切り替え。
|
||||
- [ ] Path/City の残る入口・reconciler と関連 metadata を監査し、通常 HTTP と cache invalidation を統合試験。
|
||||
- [ ] 既存 Store の残りの取得・一覧・専用リンク操作・import writer を移植。
|
||||
- [ ] 既存 schema の移行計画、schema drift の assertions、整合性検査を整備。
|
||||
- [ ] 同時 GC との pin 契約、巨大 Asset の検証 I/O の予算と再検証 cache を整備。
|
||||
|
||||
`internal/documentrefs.Freeze` は可変 `asset:` を HTML の参照属性から解決し、
|
||||
SHA-256 URL と出自属性へ固定する。同一 address の解決と同一 hash の参照記録は重複させない。
|
||||
通常テキスト、コード例、非 HTML payload は解決せず、変更不要の HTML token は元の byte 列を維持する。
|
||||
既存 Store は従来の resolver を adapter として使い、PostgreSQL 用の保存では Domain を明示した resolver を使う。
|
||||
外部 URL へアクセスする機能ではない。
|
||||
|
||||
`SaveDocument` は対象 Object の Domain 内の Path と言語別 HEAD を先に確認し、
|
||||
解決した埋め込み payload を検証してから、凍結済み Document を Geduldh へ永続化する。
|
||||
この間に DB transaction や lock は保持しない。その後に HEAD を再比較し、
|
||||
Revision、`revision_document_refs`、`revision.created` の pending outbox、型別 watermark を
|
||||
一つの transaction で確定する。outbox の sequence は型別 watermark から割り当て、
|
||||
増え続けるイベント履歴全体を走査しない。イベント配信と worker lease の移植は別工程である。
|
||||
|
||||
アプリ用 Repository は未紐付け Object に限り作成元 Domain の origin を認め、
|
||||
create/save/bind の順序を維持する。別 Domain に Path が存在する Object へ origin だけで書き込めない。
|
||||
Object のみを渡す旧呼び出しは所属 Domain が一意の場合に限り解決し、複数なら明示指定を要求する。
|
||||
これは認可の代替ではなく、HTTP 側の read/write 権限検査は維持する。
|
||||
|
||||
`SaveRevisionContentInDomain` は期待 HEAD の有無を区別する。空文字の期待値は未作成を要求し、
|
||||
期待値なしは無条件 append とする。CAS 失敗を無条件 append で自動再試行しない。
|
||||
PostgreSQL の `RestoreRevisionInDomain` は過去の本文と埋め込み参照の payload を検証し、
|
||||
現在の Asset HEAD を再解決せず新しい Revision に引き継ぐ。
|
||||
DocumentRoot を必須とし、正本の欠損を空の DB body cache で補修しない。
|
||||
SQLite の既存 writer と復元の参照固定動作は維持しており、その統一は別の変更単位とする。
|
||||
|
||||
Path の native 変更処理は namespace 管理 lock を取得してから snapshot を再検証する。
|
||||
移動・コピー・一括 unlink は最大 10,000 Path、30 秒の transaction 予算で実行し、
|
||||
`%`・`_` をワイルドカードにしない索引範囲を使う。予算超過時に部分適用はしない。
|
||||
再帰移動では City 境界、ユーザー領域の公開例外、保存した navigator の Path を同じ transaction で更新する。
|
||||
既存の locality ACL によって保護された namespace の移動・削除は引き続き拒否する。
|
||||
ACL を自動的に別の Path へ拡張する機能や DB guard の迂回は追加しない。
|
||||
公開 manifest と navigation generation は既存の native trigger で失効する。
|
||||
|
||||
native のファイル名変更は、言語別 HEAD の参照 hash を保つ新 Revision を追加する。
|
||||
コピーでは新 Object/Revision UUID を割り当て、全 Revision と埋め込み参照の対応を同じ transaction で複製する。
|
||||
本文 blob は再利用する。unlink は履歴と blob を消さず、City の定義も自動削除しない。
|
||||
コピー履歴で同時刻の旧 HEAD を維持する条件は、全件 import の順序問題と合わせて確認する。
|
||||
|
||||
City の読み取りは PostgreSQL の repeatable-read snapshot を利用する。
|
||||
整理操作は topology version を管理 lock 内で照合し、Page placement、Spot relation、監査記録を一括更新する。
|
||||
Page の URL は Spot 整理では変更しない。境界検査は最大 4,096 node に制限し、
|
||||
Spot 整理の既存の選択数・展開 Page 数・relation 数上限も維持する。
|
||||
Asset 復元は payload の hash を検証してから期待 HEAD を比較更新する。
|
||||
削除は mutable Asset を取り除くが、履歴と既存 Revision からの参照は保持する。
|
||||
|
||||
payload 永続化後に競合や DB エラーが起きた場合、参照のない不変 blob が残ることは許容する。
|
||||
同じ hash を別 writer が利用する可能性があるため、保存失敗時に blob を削除しない。
|
||||
参照のない blob の回収は pin と保持期間を持つ GC の役割とし、同時削除を行う構成では
|
||||
pin 契約の接続が済むまで新パイプラインを有効にしない。
|
||||
|
||||
portable core の v1 定義は変更せず、参照と outbox のスキーマを v2 として追加した。
|
||||
試験用 v1 の既存行を維持した移行、DDL 途中失敗時の rollback、再試行を共通テストで確認する。
|
||||
これは本番 SQLite からのデータ移行機能ではない。
|
||||
|
||||
既存 SQLite Store では、Revision の outbox 用 Domain 取得時に未知の SQL エラーを
|
||||
無視しないよう是正した。outbox 自体がない最小 fixture は従来どおり扱うが、
|
||||
outbox がある状態での取得・書き込みエラーは Revision と一緒に rollback する。
|
||||
|
||||
共通テストでは Domain 分離、言語の分離、同時 CAS、重複作成時の orphan 防止、
|
||||
Revision の不変性、復元時の競合、Asset 履歴の故障注入 rollback を検証する。
|
||||
保存パイプラインでは payload 書き込み失敗、埋め込み検証失敗、保存 I/O 中の別編集、
|
||||
参照・outbox の故障注入、異なる Object の並行 sequence 採番も検証する。
|
||||
これらは変更系 Web/API の PostgreSQL 対応完了を意味しない。
|
||||
|
||||
`PAGESWIKI_TEST_POSTGRES_DSN` に使い捨て PostgreSQL の URL を指定すると、
|
||||
`go test -race -count=1 ./internal/db ./internal/metadata ./internal/documentrefs` が実 DB と参照固定のテストを実行する。
|
||||
各テストはランダムな schema を作成し、終了時にその schema だけを削除する。
|
||||
試験用ユーザーには CREATE SCHEMA 権限が必要で、本番 DSN は使用しない。
|
||||
DSN を指定しない通常テストでは実 DB 部分だけが skip されるため、
|
||||
PostgreSQL 対応のリリースゲートでは DSN 指定を必須とする。
|
||||
初期検証対象は PostgreSQL 17。最低対応版の確約は CI matrix の整備後とする。
|
||||
|
||||
展示環境では metadata の PostgreSQL 化だけで NFS 対応完了とはしない。
|
||||
Document の no-replace publish と directory fsync、カレンダー正本の lock と atomic rename、
|
||||
秘密鍵とバックアップの配置を別々に検証する。PostgreSQL 自体の PGDATA を
|
||||
未検証の共有 NFS へ移すことで SQLite の問題を置き換える運用は採用しない。
|
||||
DB 用の適切な永続領域または外部 PostgreSQL を確保する。
|
||||
|
||||
#### 移植診断と停止状態の往復リハーサル
|
||||
|
||||
`pageswiki schema portability --db FILE --json` は既存 SQLite を読み取り専用で開き、
|
||||
テーブルの担当領域、未対応テーブル・view・trigger・index、portable migration 履歴、
|
||||
既知 core の catalog の差を報告する。行の値、DDL 本文、DSN、秘密鍵は出力しない。
|
||||
DB がなければ作成せず終了し、初期化や migration は行わない。
|
||||
アプリ全体が未対応の間は `application_ready=false` と終了コード 1 を返す。
|
||||
`core_compatible=true` は下記の限定リハーサルに必要な契約であり、本番移行の許可ではない。
|
||||
未対応テーブルを削除してこの診断を通すことはしない。
|
||||
|
||||
`InspectPortability` は SQLite では既知 core の生成 DDL と索引を厳密に照合し、
|
||||
PostgreSQL では列型、NOT NULL、default、collation、identity、主キー・一意制約・外部キー・CHECK、
|
||||
通常索引の定義と有効状態、sequence の設定を検査する。未知の unique index も無視しない。
|
||||
PostgreSQL の RLS、独自 policy・trigger・routine・rule・sequence、継承・partition 等の未移植の状態は拒否する。
|
||||
trigger は情報スキーマの権限による非表示に依存せず catalog から確認する。
|
||||
PostgreSQL で検査する範囲は現在の schema であり、別 schema のアプリを含む全 DB の認証評価ではない。
|
||||
backend 固有の migration ledger は checksum を検査するが、相手側へコピーしない。
|
||||
|
||||
`CloneCoreForRehearsal` は内部テスト用の core コピーであり、本番用 CLI には公開しない。
|
||||
対象は Object、origin、Path、Revision、Asset、Asset 履歴、Revision の Document 参照、
|
||||
activity outbox と型別 watermark の 9 テーブルに限る。認証、設定、タスク等の
|
||||
追加テーブルがある実稼働 DB は拒否する。全 writer と GC の停止を呼び出し側の前提とし、
|
||||
オンライン移行や二重書き込みを行う機能ではない。
|
||||
|
||||
コピーでは一つの source snapshot から主キー順に行を読み、型付きの値を保って
|
||||
空の staging DB/schema に挿入する。一度に保持するのは一行分であり、最大の一行自体の
|
||||
メモリ使用量は別の制約になる。移行元の DB を変更せず、移行先が空でない場合、
|
||||
同じ DB を別 connection pool から指定した場合、未知の schema がある場合は拒否する。
|
||||
移行先の全 core テーブルを保護し、途中でエラーが起きた場合はコピーした全行を rollback する。
|
||||
文字コード不正、NUL、TEXT 列の BLOB、整数型の不整合、参照欠損を黙って変換しない。
|
||||
|
||||
照合は件数だけではなく、テーブル名・列順・主キー順の各行を長さ付きで SHA-256 に入力する。
|
||||
64bit 整数を浮動小数へ変換せず、日本語、大小文字、異なる Unicode 表現、`%`・`_` を保つ。
|
||||
履歴の採番は現存する最大 ID と generator の保持状態の両方から次の値を決め、
|
||||
過去の ID が再利用されないようにする。PostgreSQL では非 transactional な `setval` ではなく、
|
||||
コピー transaction 内で identity を `RESTART` する。
|
||||
|
||||
参照先の不変 Document は、別途保持する同一の Geduldh Store で SHA-256 を検証する。
|
||||
この関数は blob、カレンダー正本、鍵、インスタンス設定をコピーしない。
|
||||
SQL だけの指紋一致をインスタンス全体のバックアップや復元の成功とは扱わない。
|
||||
実 DB テストでは PostgreSQL 側の追加更新を含めて別の SQLite へ戻し、内容指紋の一致と
|
||||
その後の採番を確認する。未知の制約・RLS・索引の拒否、コピー途中失敗の rollback、
|
||||
source の不変性、Document 欠損、同一 DB への自己コピーも検証する。
|
||||
|
||||
#### 本番 PostgreSQL 移行の受入ゲート
|
||||
|
||||
今回の対象は、全 writer を停止した状態での SQLite→PostgreSQL の片方向インポートと、
|
||||
移行後の通常運用である。PostgreSQL→SQLite の逆移行、自動切り戻し、二重書き込み、
|
||||
オンライン差分同期は今回の実装・受入条件から除外する。
|
||||
既存の core 往復テストは回帰テストとして残すが、本番全件の逆移行へ拡張する作業は行わない。
|
||||
移行前バックアップと片方向インポートの整合性検証は引き続き必須とする。
|
||||
|
||||
2026-11-14 の棚卸しでは Store の SQL 呼び出し点は約 100 ファイル・967 か所ある。
|
||||
これはすべてが SQLite 固有であるという意味ではないが、接続層と core だけでは
|
||||
通常起動・認可・全アプリ機能を満たさない。次の未完了ゲートを残す。
|
||||
|
||||
2026-11-15 時点の受入条件は以下の 8 工程に整理する。1 と 2 の主要 DB 処理は共通テストを通過したが、
|
||||
通常 HTTP/scheduler/CLI 経由の PostgreSQL 統合は工程 7 と合わせて検証する。
|
||||
工程数は受入ゲートの区分であり、8 回の変更や応答で完了するという工数見積もりではない。
|
||||
|
||||
| 順序 | 残工程 | 本番切替前の受入条件 |
|
||||
| --- | --- | --- |
|
||||
| 1 | 認証と権限の統合 | 認証・公開・承認・ユーザー領域の DB 処理は移植済み。残る管理経路の直接 SQL、通常 HTTP 認証と navigation cache の PostgreSQL 統合を確認 |
|
||||
| 2 | Domain と Campus と federation の統合 | namespace CRUD・管理者継承・token・instance・署名 manifest は移植済み。controller/managed プロセスの再起動、鍵・instance identity の保持と HTTP 配信を確認 |
|
||||
| 3 | core と Document と City の接続 | 全 Store 変更系、GC pin、検証 I/O の予算、Document root identity、Asset 復元、Path 移動・削除時の City 境界・ACL・関連リンク・filename Revision・snapshot 検査・cache 失効を接続 |
|
||||
| 4 | タスクと CalDAV と各拡張 | プロジェクト所属、VTODO、ETag/ctag/sync-token、通知、連絡先・RSS・ブログ等の repository と対応テーブルを移植 |
|
||||
| 5 | ジョブと通知 | claim/lease/fencing、キャンセル、outbox 消費、重複排除、再起動時の処理、ログ保持を検証 |
|
||||
| 6 | 検索と管理 | FTS5/rowid/PRAGMA/JSON 関数・照合順序への依存を分離し、権限付き検索・一覧と管理設定を検証 |
|
||||
| 7 | 通常起動と設定 | backend 別 bootstrap、全 Ensure、secret 参照、schema 診断、readiness、CLI/standby の接続を統一し、PostgreSQL で全体試験を通過 |
|
||||
| 8 | 全件インポートと切替 | 全正本テーブルの片方向インポート、再生成可能 cache の分類と再構築、canonical files・鍵の保持、件数・参照・内容指紋・採番の整合性、移行前 backup と PostgreSQL backup/restore を検証し、停止状態で本番切替 |
|
||||
|
||||
MySQL、S3、Redis、HA、逆移行はこの 8 工程の完了条件に追加しない。
|
||||
|
||||
#### 初回切替を短縮する場合の残件
|
||||
|
||||
初回は単一インスタンス・ローカル Document root・停止状態での片方向 import を対象とする。
|
||||
利用者は必須残件を先行し、任意機能をリリース後へ回す方針を了承した。
|
||||
Push 配信・zombie GC・ログの保持期限処理・hosted editor session には native 停止ゲートを実装した。
|
||||
その他の機能を停止する場合も利用者へ制限を示したうえで、
|
||||
起動時・HTTP・CLI・worker の全入口を同じ機能ゲートで拒否する。
|
||||
`NewPostgreSQLWithDocumentRoot` で native の通常 Store を初期化し、
|
||||
合成データによる Server 起動・HTTP 操作・停止後の再初期化まで接続した。
|
||||
`cmd/pageswiki/main.go` は `PAGESWIKI_DB_BACKEND=postgresql` による選択と、
|
||||
全件 import の完了記録・鍵・Document root の一致検査を接続した。未指定時は SQLite のままである。
|
||||
全件転送は `pageswiki migrate-postgres` を使用する。
|
||||
`internal/metadata/portability.go` の core 限定リハーサルとは別の実装である。
|
||||
|
||||
切替に直結する実装順序は次のとおりとする。
|
||||
|
||||
1. ジョブ基盤と outbox の統合。DB 層の claim・完了 token・再起動時の中断扱い・重複排除、
|
||||
Task/Calendar/RSS のイベント登録は移植済み。Server の起動で worker owner を取得し、
|
||||
停止と join 後に解放する。native 通常 Store による HTTP・再初期化・worker 二重起動拒否を検証済み。
|
||||
2. 通常起動・検索・管理経路の接続。backend 明示の Store 初期化、主要画面、
|
||||
内蔵エディタ保存、認証失効、DAV discovery まで接続済み。
|
||||
本番用 backend 選択も接続済み。認証、ACL、公開範囲は省略しない。
|
||||
3. 全件 import。全正本テーブル・鍵・設定・受付/操作履歴・同期番号を列挙し、
|
||||
cache と区別して保全する。identity 採番、本文参照、件数・内容指紋と再実行を検証する。
|
||||
4. 隔離した通常 HTTP 起動と再起動試験、backup/restore 検証後に停止移行する。
|
||||
実行する機能の CRUD、DAV 条件付き更新、権限失効、失敗時の rollback を通してからリリースする。
|
||||
|
||||
| 後回しにできる候補 | 初回切替で必要な条件と制限 |
|
||||
| --- | --- |
|
||||
| HA/LB/DR、無停止・オンライン移行 | 単一インスタンス、旧系の writer を停止した import に限定。PostgreSQL の backup/restore は省略しない |
|
||||
| PostgreSQL から SQLite への逆移行 | 既定どおり対象外。旧 backup からの復旧は、切替後の更新を引き継ぐ逆移行とは別物 |
|
||||
| 自動 GC の同時実行と pin 機構 | native zombie GC は定期実行・手動 API・Store の全入口を拒否する。本文ファイルの GC を新設せず、容量を監視する。本文参照・hash の整合性検証は残す |
|
||||
| 高度な全文検索・ランキング最適化 | native は権限付き Path/ファイル名検索と City メタデータ検索を実装。Page 本文・H1 の検索とサジェストへの H1 付与は未対応。FTS5 や全本文走査で代用しない |
|
||||
| hosted editor | native は起動・ticket 交換・session 取得・保存を拒否し、選択肢から除外する。設定と既存 session は保全し、内蔵エディタを使用する。session の base revision 更新と本文保存をまたぐ整合性は後続対応 |
|
||||
| サムネイル再生成・リソース履歴のグラフ | 対象イベントと API を明示的に無効化。正本と派生 cache/履歴の分類を先に確定し、未分類テーブルを捨てない |
|
||||
| Push/SMTP/外部サービスへの送信 | 配信だけを停止する制限運用は可能だが、Inbox と受付履歴は保持する。不明な送信結果を成功や再送待ちへ変換しない |
|
||||
| ログの自動保持期限処理 | native では定期 cleanup を実行せず、手動呼び出しも拒否する。ログと集計を保全する代わりに容量監視が必要。policy 別の件数・実行時間を制限した cleanup は後続対応 |
|
||||
| MySQL、S3、Redis 等の追加 backend | 今回の PostgreSQL 切替とは別プロジェクトとして扱う |
|
||||
|
||||
全件 import、通常起動、認証/公開権限、正本ファイルと鍵の保全、必要機能の統合試験、
|
||||
移行前 backup は短縮対象にしない。機能縮小で省けるのは各機能の稼働対応であり、
|
||||
その機能が持つユーザーデータの保全ではない。
|
||||
|
||||
#### ジョブと HTTP 接続の移植状況
|
||||
|
||||
`store-event-jobs` は PostgreSQL 用の独立 migration とし、定義、キュー、ログ、集計と設定を作成する。
|
||||
native worker は専用接続の advisory lock により同一 DB/schema に一つだけ起動できる。
|
||||
これは単一インスタンス用の排他であり、HA の lease/failover を提供するものではない。
|
||||
接続が利用不能なら新規 claim を拒否する。
|
||||
`Server.NewChecked` は worker owner 取得・イベント登録の失敗を起動エラーとして返す。
|
||||
`Runner.Stop` は受付を止めて実行中処理を cancel し、完了状態保存を待つ。
|
||||
`Server.Close` は scheduler loop・保守処理・worker の終了後に排他を解放する。
|
||||
停止待ちの timeout は終了完了を意味せず、実行中処理が残る間は排他を保持する。
|
||||
main の SIGTERM/Ctrl+C は Campus 同期の新規開始を止め、進行中同期の終了を待ち、
|
||||
HTTP と worker の停止へ接続した。各外部同期要求は従来の network timeout を使う。
|
||||
|
||||
ジョブの claim、同時実行数とキュー深さの検査、再試行は writer transaction 内で行う。
|
||||
完了には claim ごとの token を要求し、再試行・再起動前の worker の完了を拒否する。
|
||||
完了状態の保存後にログ保存が失敗しても、副作用のあるジョブを自動で再実行しない。
|
||||
再起動時に残る running は結果不明として failed にし、明示的な再試行を待つ。
|
||||
schema 初期化だけで稼働中のジョブを奪わない。
|
||||
|
||||
定期イベントの enqueue と次回日時の更新は同一 transaction で行い、キュー満杯なら両方を rollback する。
|
||||
組み込みイベントの登録は管理者による無効化や周期変更を上書きしない。
|
||||
Task の活動通知、期限通知、Focus、日次メール、RSS、Calendar sweep の登録を接続した。
|
||||
外部配信の実行可否は別の制限であり、登録の移植完了をもって配信を有効化しない。
|
||||
|
||||
outbox の正本 journal と型別 watermark は core 所有のままとし、
|
||||
`store-activity-consumers` が通知先ごとの checkpoint と検索 index を追加する。
|
||||
型別に件数を制限して読み込み、別 consumer の処理済み状態に影響されず、
|
||||
checkpoint の更新は単調増加とする。投影処理と同一 transaction で rollback できる API を維持する。
|
||||
|
||||
HTTP に残っていた Object alias、Calendar の active member、City zone kind、ブログ射影先、
|
||||
設定変更対象の Campus 解決、個人表示設定の直接 SQL は Store API に移した。
|
||||
参照は件数/時間を制限し、alias と射影先の権限判定は引き続き HTTP 側で行う。
|
||||
管理対象を解決できない場合は現在の Campus へ fallback せず拒否する。
|
||||
個人表示設定は現在の membership を再検査し、CAS と read-only 拒否を適用する。
|
||||
`EnsureTasksSchema` は native foundation と productivity migration へ接続した。
|
||||
|
||||
アナウンス、プレビュー解析 cache、リソース履歴も native migration と Store API に接続した。
|
||||
アナウンスは Domain を跨いだ同一 UUID の上書きを拒否し、削除時は Inbox との transaction を維持する。
|
||||
プレビュー解析 cache は PostgreSQL の byte 長で容量を制限し、resource sample は job UUID で重複排除する。
|
||||
これらの変更は起動依存の移植であり、新しい表示機能の追加ではない。
|
||||
|
||||
Push は鍵・購読・配送状態を保持する schema のみ作成し、Inbox INSERT trigger は設置しない。
|
||||
Store の登録・claim・完了・cleanup、HTTP、キー生成、配信の入口で拒否する。
|
||||
native 定期 enqueue と claim は Push を除外し、既存定義の日時や不明な配送状態を書き換えない。
|
||||
停止されたログの期限処理と zombie GC も、機能設定の変更で再有効化できない backend 制限とする。
|
||||
|
||||
HTTP 検索の SQLite 直接生成を Store 経由に変更し、native では Path と元ファイル名を検索する。
|
||||
最新 revision の MIME/拡張子/更新日時、Object 作成日時による絞り込みを候補 LIMIT 前に適用する。
|
||||
候補は最大 1,000 件、返却は最大 200 件、SQL と権限判定の予算は 2 秒とし、
|
||||
本文ファイル・legacy body 列を検索やタイトル補完のために走査しない。
|
||||
候補 cursor を閉じてから権限判定するため、1 接続の pool でも権限問い合わせを実行できる。
|
||||
検索・サジェスト HTTP の `X-PagesWiki-Search-Profile: metadata-only` はこの制限を表す。
|
||||
対象範囲に候補が 1,000 件を超える場合、候補制限による取りこぼしはあり得る。
|
||||
|
||||
City 検索は native の read snapshot 内で種類ごと最大 1,000 件のメタデータを集め、
|
||||
transaction を閉じてから Quartier・Street を含む親階層の権限を確認する。
|
||||
Domain/Path 境界、日付条件、未定義 Spot の Street へのリンクを維持する。
|
||||
Page のファイル種別指定時には City 要素を混ぜない。
|
||||
|
||||
hosted editor は `store-adapter-sessions` に ticket/session/base revision を保全する。
|
||||
native では Store と HTTP の操作入口を拒否し、アダプタ選択肢から editor plugin を除外する。
|
||||
閲覧用 plugin と内蔵エディタはこの制限の対象外である。
|
||||
既定 editor の設定を削除したり、残存 ticket を期限切れ cleanup で消したりしない。
|
||||
|
||||
この検索・保全ガードは合成データの PostgreSQL 17 と SQLite で検証した。
|
||||
`go test ./...`、対象の race 検査、`go vet ./...`、`cmd/pageswiki` のビルドを通過した。
|
||||
検索検査には 1 接続での権限問い合わせ、Domain・親階層の拒否、文字列のリテラル一致、
|
||||
1,100 件の候補に対する LIMIT 前の絞り込み、過去 revision の除外、cancel を含む。
|
||||
検索の repository 統合検証とは別に、次の通常 HTTP 起動検証を行う。
|
||||
|
||||
`NewPostgreSQLWithDocumentRoot` は鍵と Document root を先に検査し、native schema を依存順に初期化する。
|
||||
欠落・不正な `auth.key` を生成し直さず、`PAGESWIKI_AUTH_KEY` を指定しない場合は既存の 32 byte 鍵を要求する。
|
||||
Campus/Domain 未配置、所有 Campus が存在しない Domain、Domain が存在しない Path は起動を拒否する。
|
||||
既存の無効 Campus を既定 Campus へ付け替えず、SQLite の legacy body backfill も実行しない。
|
||||
`BackfillDocuments` の native 呼び出しは明示的に拒否する。
|
||||
正本ファイル全件の hash 照合は import/restore 工程の責務であり、この constructor だけでは保証しない。
|
||||
|
||||
合成データの PostgreSQL 17 で通常 Store と `Server.NewChecked` を組み合わせ、
|
||||
実 HTTP listener の本文取得、主要画面と管理パネル、内蔵エディタ保存と競合 409、
|
||||
無効・失効セッションの 401、CalDAV/Task DAV/CardDAV の discovery を検証した。
|
||||
同じ DB と Document root で Server を停止・再初期化し、本文と認証 session の継続を確認する。
|
||||
稼働中の別 worker 起動は拒否し、停止完了後に再取得できる。
|
||||
resource event の保存は同じ job UUID で再実行しても重複しない。
|
||||
管理画面の最近の Object は native query に分岐し、Domain 内の代表 Path と最新 revision のみを扱う。
|
||||
返却上限 200 件・SQL 予算 2 秒とし、Page 本文は読み込まない。
|
||||
|
||||
全件 import と内容照合、本番 backend 選択、合成データの PostgreSQL dump/restore は接続した。
|
||||
停止切替のコマンドと制限は [PostgreSQL 移行手順](../operations/postgresql-migration.md) にまとめる。
|
||||
本番切替には、移行前の一貫したバックアップと、そのインスタンスの schema preflight・全件 import の成功が必要である。
|
||||
合成データの試験通過は、本番データの移行完了を意味しない。
|
||||
|
||||
`application_import_catalog.go` は保全対象と SQLite 専用索引・migration ledger の列を固定する。
|
||||
未知のテーブル・列・trigger/view 定義を検出したら拒否し、cache と推測して捨てない。
|
||||
移行先に既存データがある場合は schema 準備の前に拒否する。
|
||||
データ転送は単一 transaction とし、schema が用意した既定行のみを置換する。
|
||||
一時停止するのは派生状態を更新する user trigger だけで、FK・CHECK・UNIQUE は維持する。
|
||||
各テーブルの型付き内容指紋・件数、Document の SHA-256、namespace の参照を照合する。
|
||||
SQLite の削除済み最大採番も保持し、transactional identity restart で番号の再利用を防ぐ。
|
||||
タスク DAV の内部索引キーと削除 tombstone は NUL 区切りから native v1 base64url 形式に変換する。
|
||||
この変換を report に明示し、正本 JSON とクライアントに公開する href は変更しない。
|
||||
失敗時の全件 rollback、修正後の再試行、重複 import 拒否、認証 session・画像・日本語タグ、
|
||||
プロジェクト別タスク・連絡先 sync-token の継続を合成データで検証する。
|
||||
同じ正本ファイルを保持した schema 単位の `pg_dump`/`pg_restore` 後も本文・認証・DAV を検証する。
|
||||
|
||||
#### 次の実装単位と検証状況
|
||||
|
||||
工程 3 の通常保存・履歴取得・復元は Store 境界へ接続済みである。
|
||||
エディタ保存 API は Domain と期待 HEAD を明示し、PostgreSQL 分岐から本文永続化・参照固定・
|
||||
Revision・outbox を一つの契約で扱う。SQLite は既存保存動作を維持する。
|
||||
PostgreSQL の通常 HTTP 経路では保存と期待 HEAD 競合を確認済み。
|
||||
履歴復元を含む残る操作の HTTP 確認は工程 7 に残る。
|
||||
|
||||
`path_mutation.go`/`path_snapshot.go` の通常操作、`city_operations.go`/`city_organize.go`、
|
||||
City map 読み取りと Asset Store API は移植済みである。
|
||||
`city.go` の個別作成・削除・relation、`city_plan.go` の一括変更計画、ページ一覧と
|
||||
アーカイブ import 管理も移植済みである。City の作成・配置・再帰削除と計画適用は
|
||||
共通 namespace lock の中で topology version と監査イベントを更新し、途中の SQL 失敗は rollback する。
|
||||
`EnsureCitySchema` は native schema と公開制御の migration へ接続した。
|
||||
|
||||
自動生成の候補取得と名前判定は Store API に集約し、候補 cursor を閉じてから階層を解決する。
|
||||
同じ境界の解決結果は一回の plan 構築中に再利用し、DB 故障を「既存要素なし」として扱わない。
|
||||
候補は 20,000 件を上限とし、超過時に途中までの City を適用しない。
|
||||
定期・編集イベントからの実行は scheduler 移植と統合して検証する。
|
||||
|
||||
children は Domain 全件を Go へ読み込まず、対象 subtree から直下要素を SQL で抽出する。
|
||||
DB generation とプロセス内の cache epoch で別 Store の変更・取得中の失効に追従する。
|
||||
Browser と children の列挙上限は 20,000 件、問い合わせは timeout 付きとし、超過を明示する。
|
||||
PostgreSQL の Browser title は本文 cache ではなく canonical HTML の先頭 64 KiB から取得する。
|
||||
一回の title 読み取りは 32 MiB と timeout で制限するが、DocumentSize の個別 I/O は残る。
|
||||
権限付き Object metadata 一覧は既存の候補・alias 上限と継続 cursor を維持する。
|
||||
|
||||
アーカイブ import は独立 migration `store-imports` でセッション、計画、journal を作成する。
|
||||
Path rollback とセッション状態更新は一つの transaction とし、import 時と Object が一致する
|
||||
binding だけを削除する。City cleanup はそれに先行する再試行可能な別フェーズであり、
|
||||
City と Path を一括 rollback する transaction ではない。
|
||||
旧 worker の void 状態更新 API は失敗を呼び出し元へ返せないため、scheduler 移植時に
|
||||
エラー伝播・状態遷移・再試行を併せて整理する。
|
||||
|
||||
工程 3 の残件は
|
||||
`domain_paths.go` 等の legacy schema adoption と bootstrap の分離、残る直接 SQL writer の棚卸し、
|
||||
import worker のエラー伝播、Path 変更を消費するイベント処理との統合である。
|
||||
同時 GC の pin、Document root identity、巨大 payload 検証の I/O 予算も起動前のゲートとして残す。
|
||||
すべての Path/Revision writer の同じ排他契約への接続が済むまで PostgreSQL の通常起動は許可しない。
|
||||
|
||||
工程 4 は task/CalDAV、calendar、contacts/CardDAV、feeds、periodicals の順に
|
||||
保存スキーマ・変更処理・一覧・ETag/同期 token を一組ずつ移植する。
|
||||
タスクやカレンダーの処理を停止して起動検証だけを通す方式は採用しない。
|
||||
工程 5 と 6 の job claim、notification、全文検索も同様に機能契約を保つ。
|
||||
|
||||
タスク本体は `store-tasks-foundation` の native 空 DB schema と Store 操作を追加した。
|
||||
保存・削除・DAV 設定更新は権限の再確認前に共通 namespace lock を取得する。
|
||||
task head、履歴、変更 journal、outbox、DAV index、通知予約、達成度 index は
|
||||
一つの保存 transaction に含める。削除は tombstone を残し、Wiki とカレンダーを削除しない。
|
||||
一覧では JSON 抽出、文字列検索、パラメータ番号、階層 cursor の真偽値を backend 別に扱う。
|
||||
|
||||
SQLite の DAV 内部索引は NUL 区切りを含むが、PostgreSQL の TEXT は NUL を格納できない。
|
||||
native 内部キーは `v1:{base64url(project)}:{base64url(value)}` とし、既定 collection も同じ方式にする。
|
||||
外部 URI・resource 名・UID・canonical JSON は変更しない。全件 import では derived DAV index を
|
||||
canonical head から再構築し、削除 tombstone の `dav_name`/`dav_uid` も旧 NUL 形式から変換する。
|
||||
この変換なしに SQLite の該当列をそのままコピーしてはならない。
|
||||
|
||||
個人既定値、作業時間、Pomodoro、日次集計、ダッシュボードと予定枠の DB 予約は移植済みである。
|
||||
設定と配置の version を独立させ、作業時間の訂正・取消は監査履歴と同時に commit する。
|
||||
writer は権限確認前に共通 namespace lock を取り、対話的な更新は 5 秒で timeout する。
|
||||
`store-task-productivity` の追加 migration で Focus span と予定枠の title を導入し、
|
||||
既存の `store-tasks-foundation` migration は変更しない。
|
||||
|
||||
`store-notification-inbox` は Inbox のみを作成する。通知挿入は一意キーの競合を
|
||||
transaction 内で処理し、別プロセスからの重複要求も同じ UUID を返す。
|
||||
タイマー完了・期限/着手日の通知は、Inbox 挿入と元の状態更新を同じ transaction に含める。
|
||||
通知保存が失敗した場合は元の状態も rollback し、次回に再試行できる。
|
||||
活動 journal の補完は既存 outbox の冪等キーを再利用する。
|
||||
配信候補は最大 100 件・3 秒に制限し、読めなくなった所属先の情報を通知へ複製しない。
|
||||
|
||||
タスク capture/Wiki 同時作成と再作成、プロジェクト配下のメモ作成は移植済みである。
|
||||
native では Object の作成元 Domain と primary Path を明示し、本文は canonical Document に置く。
|
||||
Task・受付記録・Object・Path・Revision・outbox は同じ transaction で commit する。
|
||||
入力本文は HTML とマクロをエスケープし、削除後の明示的再作成でも既存の Wiki 本文を変更しない。
|
||||
受付記録だけ残っている不整合は自動復活させず、修復が必要なエラーとして扱う。
|
||||
|
||||
メールダイジェストの claim・送信履歴・明示的再試行も移植済みである。
|
||||
共通 namespace lock と状態条件で scheduled/manual claim を一意にし、同意・設定 version・
|
||||
所属権限を再確認する。送信結果が不明な試行は自動再送せず、本人の重複送信了承を必要とする。
|
||||
90 日経過で詳細履歴を削除しても、日次予約と再試行の受付記録は重複防止のため保持する。
|
||||
保守処理は 3 秒・中断判定 100 件・履歴削除 500 件に制限する。
|
||||
|
||||
カレンダーは `calendar-cache` migration で PostgreSQL の月別/イベント索引を作成する。
|
||||
正本は従来どおり月別 ICS と `calendar.json` の manifest であり、DB には移さない。
|
||||
明示的な backend 選択を HTTP の Repository 初期化へ接続し、予約済みタスク予定枠の
|
||||
正本作成・移動後の再試行と日次予定の cache projection を検証する。
|
||||
manifest 更新後の索引再構築失敗は transaction を rollback し、次回アクセスで修復する。
|
||||
cache 更新は 30 秒で打ち切る。全 writer は同じ正本 root と有効な flock を共有する必要があり、
|
||||
独立した filesystem を持つ replica 間の HA は未対応である。
|
||||
任意の DAV filename を UID として復号する際は UTF-8 と制御文字を検証し、
|
||||
非テキストの復号結果を PostgreSQL の TEXT パラメータへ渡さない。
|
||||
|
||||
連絡先は `store-contacts` migration で native schema と変更 journal の trigger を導入する。
|
||||
vCard 正本・履歴 hash・任意 resource 名・tombstone を維持し、更新と変更番号を同時 commit する。
|
||||
CardDAV の inventory と sync-token は PostgreSQL の REPEATABLE READ で同じ snapshot から取得する。
|
||||
metadata inventory は vCard 本文を返さず、UTF-8 の byte 長を返す。
|
||||
保存と snapshot 取得は 5 秒で timeout し、保存は権限・資格情報確認前に共通 namespace lock を取る。
|
||||
全 DB import 時は contact epoch と変更番号を保全し、identity sequence を最大値に合わせる。
|
||||
|
||||
ブログ・日記は `store-domain-entry` の設定 schema を再利用し、追加の `store-periodicals`
|
||||
migration で記事索引・変更履歴・投稿受付記録・射影設定を作成する。
|
||||
書き込みは権限確認前に共通 namespace lock を取得する。記事新規作成では Object・primary Path・
|
||||
Revision・埋め込み参照・受付記録・記事 metadata・変更履歴・outbox を同じ transaction で確定する。
|
||||
本文は canonical Document に保存し、Asset 名は当該 Domain の HEAD を不変 hash に解決して保持する。
|
||||
記事の登録解除は索引だけを削除し、Page 自体は残す。再登録は変更履歴から version を継続する。
|
||||
公開タイトルの問い合わせは許可済み navigation の UUID と Domain に限定し、入力件数も予算内に制限する。
|
||||
射影は同じ Campus の有効な local Domain 同士に限り、多段射影・循環と無効な配信元を拒否する。
|
||||
全件 import では投稿受付記録と記事変更履歴も正本として保全し、履歴の identity 採番を復元する。
|
||||
|
||||
RSS は `store-feeds` migration で購読元・記事 cache・ローカル既読状態・取り込み受付と
|
||||
FreshRSS の明示的操作 receipt を保持する。書き込みは namespace lock 取得後に
|
||||
native actor と拡張設定を検証する。読み取りは 3 秒、書き込みは 5 秒で制限する。
|
||||
FreshRSS 分類は PostgreSQL の JSONB 関数で検索し、カテゴリなしの null/空配列を扱う。
|
||||
壊れた拡張設定は当該 Domain を処理対象から外し、他の購読元の候補検索を中断させない。
|
||||
送信結果が不明な操作は購読元の変更・削除後も履歴として保持し、自動再送しない。
|
||||
期限切れ claim の回収は 1 transaction あたり 100 件・3 秒に制限する。
|
||||
次の実装単位は scheduler の native schema と claim/lease/ログ保持、各イベント登録である。
|
||||
Push 配信と scheduler のイベント登録・実行は未統合である。
|
||||
外部カレンダーの内容は検証に用いない。
|
||||
`EnsureTasksSchema` と `EnsureNotificationSchema` の native 通常起動は引き続き未対応として拒否する。
|
||||
旧 free helper を使う未移植の呼び出し元は SQLite 用であり、native Store の receiver へ順次接続する。
|
||||
DAV HTTP の discovery/REPORT/条件付き PUT/DELETE、ETag・ctag の再起動後整合性と
|
||||
カレンダー共通 endpoint の実機統合も残る。task payload 永続化中に writer transaction を保持する
|
||||
既存動作と GC pin は、Document の I/O 予算・参照固定の残件として扱う。
|
||||
|
||||
2026-11-15 の合成データによる検証では、SQLite とローカル PostgreSQL 17 を使った
|
||||
全 Go テスト、移植済み Store の共通契約・PostgreSQL migration テストの race 検出、
|
||||
`go vet ./...`、ローカルバイナリ生成が成功した。
|
||||
公開・承認、ユーザー領域・電話番号の同意、Domain 同時作成・移動、管理者継承の取消、
|
||||
Federation の token 失効・署名改ざん・replay・同時初回受信・checkpoint 故障時の rollback を含む。
|
||||
通常保存の追加試験では別 Store からの同時 CAS と無条件 append、言語別履歴、
|
||||
create/save/bind の origin 制約、別 Domain への移動後の拒否、現在の Asset HEAD を変更した後の
|
||||
履歴復元、本文・埋め込み payload の欠損、Page/Revision の outbox 故障時の rollback を確認する。
|
||||
Path/City/Asset の追加共通試験では、再帰移動・コピー・重複選択の削除、literal な `%`/`_`、
|
||||
stale snapshot の拒否、保護 namespace の拒否、コピーと filename Revision の参照保持、
|
||||
navigator の移動先重複解消、Spot 統合後の所属と URL 保持、同時 topology version 更新、
|
||||
Asset の同時 CAS・履歴復元・欠損拒否・監査保存故障時の rollback を確認する。
|
||||
City CRUD・一覧・アーカイブ import の追加試験では、stale plan、再帰削除中の監査故障、
|
||||
archived 名の再利用、literal prefix、12 件以上の指定順序、canonical H1、metadata cursor の
|
||||
重複防止、別 Store からの cache 失効、import 番号重複時の計画保持、Domain 分離、
|
||||
Path rollback と状態更新の原子性、付け替え済み Path の保護、read-only 拒否を確認する。
|
||||
タスクの追加試験では、プロジェクト別の同一 DAV UID、同一 collection 内の衝突拒否、
|
||||
既定 collection の分離、親子階層、日付・優先度・添付の絞り込みと pagination、履歴、
|
||||
完了時の通知予約削除、削除の再試行と復活拒否、DAV 資格情報の発行・失効を確認する。
|
||||
PostgreSQL の別 Store からの同時保存は一方だけ成功し、outbox 故障では保存と削除が rollback する。
|
||||
readonly Principal の書き込み拒否と、Membership 失効後の読み取り拒否も確認する。
|
||||
生産性機能の共通試験では、個人設定と配置の独立した CAS、作業時間の訂正・取消・履歴の
|
||||
pagination、Pomodoro の停止区間を除いた集計、完了通知の一度だけの登録、予定枠の状態変更、
|
||||
期限通知の再送防止と活動 journal の重複排除を確認する。
|
||||
PostgreSQL では別 Store の同時設定更新・作業開始・タイマー開始・完了処理も試験し、
|
||||
監査失敗時に作業時間/Focus span が途中で更新されないこと、Inbox 故障後の再試行、
|
||||
同時通知挿入の一意性、受信者 scope と失効後の拒否を確認する。
|
||||
capture/メモの共通試験では日本語 Path と本文、HTML/マクロの無害化、受付の再送、
|
||||
同一 Path の衝突拒否、削除後の明示的再作成と本文保持、stale project の拒否を確認する。
|
||||
PostgreSQL では受付記録の故障による全関連 metadata の rollback、別 Store からの同時 capture、
|
||||
canonical 本文の読み戻し、作成元 Domain・primary Path・cache generation を確認する。
|
||||
メール管理の共通試験では日次 claim の重複拒否、重複送信了承・受付再送・履歴 pagination、
|
||||
同意変更後の取消、保持期限後の予約・受付保持を確認する。PostgreSQL では複数 Store の
|
||||
scheduled/manual claim と、送信結果保存失敗時の rollback、中断試行の自動再送防止を確認する。
|
||||
カレンダーの共通試験では月またぎ移動、ETag 競合、DAV filename 保持、既定 calendar、
|
||||
共同編集者の読み取り権限と失効、全 cache 消去後の再構築、削除を確認する。
|
||||
PostgreSQL では別 Repository の同時編集、再構築・削除の DB 故障時の正本保持と再試行を検証する。
|
||||
タスク予定枠の再送が移動済み予定を上書きしないこと、日次 projection が現在の時刻を返すこと、
|
||||
外部 source は snapshot 読み込み前に拒否されることも合成 metadata で確認する。
|
||||
連絡先の共通試験では日本語検索、未知の vCard property 保持、同一 UID import の再送、
|
||||
削除の差分配信、履歴復元、metadata の byte 長、差分 pagination、資格情報失効を確認する。
|
||||
PostgreSQL では同時 CAS と履歴保存故障時の連絡先・同期番号の rollback を確認する。
|
||||
ブログ・日記の共通試験では日本語記事の作成・再送、変更された再送入力の拒否、
|
||||
記事 metadata の CAS、登録解除後の Page 保持と再登録、Asset HEAD 更新後の本文参照固定、
|
||||
欠損 Asset の拒否、公開タイトルの Domain 分離と件数上限、射影の循環・無効な配信元の拒否を確認する。
|
||||
PostgreSQL では変更履歴の保存故障時に Path と受付記録が rollback すること、
|
||||
別 Store からの同時投稿要求が一つの記事となること、Campus 無効化後の投稿拒否を確認する。
|
||||
RSS の共通試験では日本語・literal 検索、日時 cursor、既読状態の CAS、
|
||||
FreshRSS のカテゴリ/未分類/空フォルダ、購読 import と Wiki 取り込みの再送、
|
||||
削除済み購読元の復活拒否、外部操作の確認・期限切れ・読み取り専用再確認を検証する。
|
||||
購読元を変更・削除しても不明な外部操作を保持し、履歴の metadata 欠損を許容する。
|
||||
PostgreSQL では複数 Store の同時 claim、cache 保存故障時の lease rollback、
|
||||
100 件ずつの期限切れ回収、破損設定と Campus 無効化後の拒否を確認する。
|
||||
ジョブの共通試験では claim・完了・再試行・キャンセル・一覧・ログ・集計と read-only 拒否を確認する。
|
||||
PostgreSQL では同時定期登録の一意性、同時 claim の上限、二つ目の worker owner の拒否、
|
||||
再起動後の結果不明ジョブの保留、古い完了 token の拒否、完了ログ故障時の状態保持を確認する。
|
||||
outbox は型別・Domain 別の取得、consumer の独立性、同時 checkpoint の単調性、
|
||||
rollback と read-only 拒否を両 backend で検証する。
|
||||
HTTP 用 lookup と個人表示設定の共通試験では Domain 分離、無効 Principal、対象欠損時の拒否、
|
||||
設定 CAS を確認する。scheduler/outbox/lookup の対象テストは race detector でも検証する。
|
||||
これらは component テストであり、PostgreSQL を指定した通常アプリ起動や本番データ import の成功ではない。
|
||||
|
||||
検証は `GOPROXY=off GOSUMDB=off` とし、実 PostgreSQL の共通テストには
|
||||
`PAGESWIKI_TEST_POSTGRES_DSN` で使い捨て DB を指定する。各テストは専用 schema を作成・削除するため、
|
||||
本番 DB を指定してはならない。実際の外部カレンダー、OIDC/LDAP/SMTP、Gitea へのアクセスは不要。
|
||||
|
||||
2026-11-15 に Gitea の復旧連絡を受領した。移植中は引き続きローカルビルドで検証し、
|
||||
全 repository・通常起動・インポートのリリースゲートを満たした後に、commit/push、
|
||||
SemVer タグ、Gitea CI の image build とリリースへ進む。復旧だけを理由に未完の移植を配布しない。
|
||||
ローカルビルドの成功は DB 切替の許可条件ではなく、上記ゲートと停止状態の全件インポート検証を
|
||||
満たした後、移行前 backup を確保し、同じ成果物で隔離起動試験を経て本番を起動する。
|
||||
|
||||
未対応機能を暗黙に初期化したり、読めないテーブルを cache とみなして捨てたりしない。
|
||||
外部 RDB 障害時に SQLite へ自動 fallback する設定は作らず、二つの DB が別々に
|
||||
更新を受ける状態を防ぐ。最低 PostgreSQL 対応版と CI matrix の確定も切替条件とする。
|
||||
|
||||
#### 片方向インポートと切替の設計
|
||||
|
||||
本番切替は全ゲートが通った後、次の順で行う。全件移行ツールは
|
||||
`pageswiki migrate-postgres` であり、core リハーサルをその代用にしない。
|
||||
|
||||
1. HTTP/CalDAV/CLI、scheduler、federation を含めた全 writer と GC を停止し、保守状態を確認する。
|
||||
2. SQLite の整合した snapshot、Document、カレンダー正本、設定・鍵・instance identity を保全し、復元検証する。
|
||||
3. 空の専用 PostgreSQL へ全件移行し、件数・内容指紋・参照・採番・payload を照合する。
|
||||
4. 読み取り検証でログイン、権限、公開ナビゲーション、検索、タスク/CalDAV、管理・配信を確認する。
|
||||
5. 反映する全プロセスを一斉に PostgreSQL 側へ向け、旧 SQLite は凍結したまま、書き込み再開時点を記録する。
|
||||
|
||||
書き込み再開前にインポートや検証が失敗した場合は移行を中止し、元の SQLite を維持する。
|
||||
書き込み再開後は PostgreSQL を正本とし、障害時は PostgreSQL 側の復旧・修正を基本とする。
|
||||
移行後の更新を SQLite に反映する機能は今回提供せず、接続先を旧 SQLite へ自動で戻さない。
|
||||
この制約を運用手順にも明記し、逆移行の完成を本番切替の前提条件にはしない。
|
||||
|
||||
### S3:運用と移行
|
||||
|
||||
- [ ] 編集可能な設定例と薄い backup / release / deploy script。
|
||||
@@ -404,6 +1217,10 @@ setup 完了後は token 再利用不可。機密欄の再表示・ログ出力
|
||||
## 12. 根拠資料
|
||||
|
||||
- [SQLite WAL:network filesystem の制約](https://sqlite.org/wal.html)
|
||||
- [PostgreSQL の transaction-level advisory lock](https://www.postgresql.org/docs/current/explicit-locking.html#ADVISORY-LOCKS)
|
||||
- [PostgreSQL の identity 変更](https://www.postgresql.org/docs/current/sql-altertable.html)
|
||||
- [PostgreSQL の RESTART と transaction](https://www.postgresql.org/docs/current/sql-altersequence.html)
|
||||
- [pgx の database/sql 接続](https://pkg.go.dev/github.com/jackc/pgx/v5/stdlib)
|
||||
- [S3 の conditional writes](https://docs.aws.amazon.com/AmazonS3/latest/userguide/conditional-writes.html)
|
||||
- [Redis persistence](https://redis.io/docs/latest/operate/oss_and_stack/management/persistence/)
|
||||
- [PostgreSQL backup / restore](https://www.postgresql.org/docs/current/backup.html)
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
# PostgreSQL への停止移行
|
||||
|
||||
SQLite から PostgreSQL への片方向移行を、単一インスタンス・ローカルの正本ファイル配置で行う。
|
||||
全 writer を停止し、空の専用 PostgreSQL database/schema へ転送する。既存 DB への merge、
|
||||
オンライン差分同期、PostgreSQL から SQLite への逆移行は提供しない。
|
||||
|
||||
## 対応範囲と制限
|
||||
|
||||
検証対象は PostgreSQL 17。通常起動の既定値は引き続き SQLite である。
|
||||
PostgreSQL 起動は verified TLS、全件 import の完了記録、既存の鍵と同じ絶対 Document root を必須とする。
|
||||
DB 接続障害や検査失敗で SQLite に自動 fallback しない。
|
||||
|
||||
- native 検索は権限付きの Path・ファイル名・City メタデータ検索。本文全文検索は後続対応。
|
||||
- hosted editor、Web Push 配信、zombie GC、ログの自動保持期限処理は native の停止ゲート対象。
|
||||
設定・受付記録・既存データは保全する。内蔵エディタと Inbox を使用し、ログ・ファイル容量を監視する。
|
||||
- DR/HA 構成への移行は拒否する。`pageswiki dr` と旧 `schema` コマンドは PostgreSQL 設定時に拒否する。
|
||||
- DB 直結の `data/pageswiki-cli` は SQLite 専用。PostgreSQL では HTTP の `pages-cli` を使用する。
|
||||
すべてのプロセスに backend 設定を統一し、旧 SQLite 用 CLI を起動しない。
|
||||
- 外部カレンダーの実データを使った検査・再取得は行わない。検証用の予定・連絡先・タスクは合成データを使う。
|
||||
|
||||
## 移行前の準備
|
||||
|
||||
1. HTTP、CalDAV/CardDAV、CLI、scheduler、federation、外部の直接 DB writer を停止する。
|
||||
コマンドは data directory の排他ロックを取るが、旧バイナリや直接 SQLite client の停止を代替しない。
|
||||
2. 停止状態の data directory 全体を別の場所に保全する。SQLite と WAL、`documents`、カレンダー・タスク等の
|
||||
正本ファイル、設定、`auth.key`、instance identity を同じ時点で保全する。
|
||||
`PAGESWIKI_AUTH_KEY` を使用していた場合はその secret も別途保全し、移行時・起動時に同じ値を渡す。
|
||||
3. 専用 PostgreSQL database/schema と、それを所有する移行ユーザーを用意する。
|
||||
schema/table/trigger/identity の作成・変更権限が必要。共有アプリ DB を移行先にしない。
|
||||
4. PostgreSQL の CA とホスト名を検証できる DSN を secret として用意する。
|
||||
`PAGESWIKI_POSTGRES_DSN` に `sslmode=verify-full` と必要な `sslrootcert` を設定する。
|
||||
DSN はコマンドラインやログに出さない。移行先の管理者による同時 schema 変更も停止する。
|
||||
|
||||
## コマンド
|
||||
|
||||
以下は、稼働時にも `/data` を使う場合の例。移行と起動で data directory の絶対パスを一致させる。
|
||||
Docker では同じ `/data` マウントを使用する。別のホスト側絶対パスで import してから
|
||||
コンテナ内 `/data` に切り替えると、root 検査により起動を拒否する。
|
||||
|
||||
```sh
|
||||
# schema の読取り検査。移行先への接続・データコピーは行わない。
|
||||
./pageswiki migrate-postgres --data-dir /data
|
||||
|
||||
# DSN は保護された環境設定から注入済みとする。
|
||||
./pageswiki migrate-postgres --data-dir /data --apply --timeout 30m
|
||||
|
||||
# import 成功・保全確認後、同じ正本 root と鍵で起動する。
|
||||
PAGESWIKI_DB_BACKEND=postgresql PAGESWIKI_DATA_DIR=/data ./pageswiki
|
||||
```
|
||||
|
||||
`--apply` がない場合は schema preflight のみで、payload の検証成功を意味しない。
|
||||
`--allow-insecure` は隔離試験専用の移行オプションであり、通常起動の TLS 検証は緩和しない。
|
||||
未知のテーブル・列・trigger/view がある場合は処理を止め、保全分類と schema を修正して再試行する。
|
||||
不明なデータを削除して検査を通さない。
|
||||
|
||||
## 転送と照合
|
||||
|
||||
保全対象は `internal/store/application_import_catalog.go` に列挙する。
|
||||
SQLite 専用 FTS 索引・派生 latest revision index・SQLite migration ledger は除外するが、
|
||||
設定、鍵への参照、認証、ACL、タスク、連絡先、同期番号、受付履歴、ジョブ・ログ等は保持する。
|
||||
正本ファイルは同じ場所に残す。ファイル自体を別の volume へコピーするコマンドではない。
|
||||
|
||||
転送は単一 transaction。転送前に移行先の空状態を検査し、型を区別した各行の内容指紋・件数、
|
||||
参照される Document の SHA-256、namespace の参照整合性を commit 前に検査する。
|
||||
タスク DAV の内部索引だけは backend 形式の違いを明示的に変換する。変換後の内容指紋を照合し、
|
||||
クライアント用 href・正本 JSON は保持する。削除履歴と削除済み最大採番も保全する。
|
||||
|
||||
JSON report は対象テーブルの件数と指紋、除外一覧、変換内容、Document 数、採番情報を返す。
|
||||
本文や資格情報は含まない。成功時の report をバックアップと一緒に保管する。
|
||||
失敗時はデータ転送を rollback する。準備済み schema は残り得るが、既定行だけの状態なら再試行できる。
|
||||
完了済み import や既存データへの上書きは拒否する。
|
||||
|
||||
## 起動確認と復旧
|
||||
|
||||
書き込み再開前に、同じバイナリと設定で起動し、ログイン・既存 session・権限・本文・内蔵編集・
|
||||
タスクのプロジェクト所属・DAV discovery/条件付き更新・連絡先・管理画面を確認する。
|
||||
外部送信や取得を伴う worker の再開対象は事前に点検する。
|
||||
この実装は import 済み DB を開くためのもので、空の PostgreSQL から既定 Campus を新規生成しない。
|
||||
|
||||
移行前 SQLite は凍結したまま保管する。書き込み再開後の新しい更新は PostgreSQL 側にしか存在しない。
|
||||
旧 SQLite へ接続を戻すことはデータを引き継ぐ切り戻しではない。
|
||||
|
||||
PostgreSQL 運用のバックアップは、writer を停止した同じ時点の `pg_dump -Fc` と data directory 全体、
|
||||
鍵・secret を組にする。復元先は空の専用 database/schema とし、`pg_restore --exit-on-error` で復元する。
|
||||
ファイルを元と同じ絶対 mount path に復元し、接続先 DSN のみ変更する。
|
||||
import 完了記録も dump に含める。DB 単独や正本ファイル単独のバックアップでは復元条件を満たさない。
|
||||
復元後は停止状態で本文・参照・認証・DAV 同期の検証を実施してから writer を再開する。
|
||||
|
||||
## 検証とリリース前チェック
|
||||
|
||||
- 合成データで全件移行、途中失敗の rollback、再試行、上書き拒否、schema drift 拒否を検証する。
|
||||
- 日本語本文・タグ、バイナリ画像、認証 session、プロジェクト別 DAV、削除 tombstone、連絡先 sync-token を照合する。
|
||||
- 隔離した PostgreSQL 17 に対し schema 単位の dump/restore と、同じ正本 root の読み戻しを検証する。
|
||||
- `go test ./...`、対象 race test、`go vet ./...`、通常バイナリ build を通す。
|
||||
- 実インスタンスの停止バックアップと import 結果は、合成データ試験とは別に確認する。
|
||||
- image build workflow は `tools/ci/test-postgres.sh` で PostgreSQL 17 の全 Go テストを実行してから publish する。
|
||||
DB container は外部ネットワーク・公開 port を持たず、テスト container がその network namespace を共有する。
|
||||
DSN 未設定で実行したローカルテストの skip を移植検証の成功として扱わない。
|
||||
|
||||
任意のテスト DSN に本番を指定しない。`PAGESWIKI_TEST_POSTGRES_DSN` は使い捨て DB に限定する。
|
||||
CLI と verified TLS の試験は `PAGESWIKI_TEST_POSTGRES_TLS_DSN` を指定する。
|
||||
`PAGESWIKI_TEST_POSTGRES_CONTAINER` を追加指定すると、同じ隔離 DB を持つ Docker container 内の
|
||||
`pg_dump`/`pg_restore` でテスト専用 schema の復元試験を実行する。
|
||||
@@ -7,17 +7,17 @@ require (
|
||||
github.com/go-ldap/ldap/v3 v3.4.8
|
||||
github.com/gomarkdown/markdown v0.0.0-20260417124207-7d523f7318df
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.13.0
|
||||
github.com/microcosm-cc/bluemonday v1.0.27
|
||||
golang.org/x/text v0.22.0
|
||||
golang.org/x/text v0.41.0
|
||||
modernc.org/sqlite v1.32.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/google/nftables v0.3.0 // indirect
|
||||
github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42 // indirect
|
||||
github.com/mdlayher/socket v0.5.0 // indirect
|
||||
golang.org/x/sync v0.11.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.3 // indirect
|
||||
golang.org/x/sync v0.22.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -38,9 +38,9 @@ require (
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/teambition/rrule-go v1.8.2
|
||||
golang.org/x/crypto v0.33.0
|
||||
golang.org/x/crypto v0.37.0
|
||||
golang.org/x/net v0.35.0
|
||||
golang.org/x/sys v0.30.0 // indirect
|
||||
golang.org/x/sys v0.32.0
|
||||
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 // indirect
|
||||
modernc.org/libc v1.55.3 // indirect
|
||||
modernc.org/mathutil v1.6.0 // indirect
|
||||
|
||||
@@ -27,10 +27,7 @@ github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17w
|
||||
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||
github.com/gomarkdown/markdown v0.0.0-20260417124207-7d523f7318df h1:Mwihr/o+v4L5h56rwHLOE20+hh7Okhwno5BHz3zDuao=
|
||||
github.com/gomarkdown/markdown v0.0.0-20260417124207-7d523f7318df/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/nftables v0.3.0 h1:bkyZ0cbpVeMHXOrtlFc8ISmfVqq5gPJukoYieyVmITg=
|
||||
github.com/google/nftables v0.3.0/go.mod h1:BCp9FsrbF1Fn/Yu6CLUc9GGZFw/+hsxfluNXXmxBfRM=
|
||||
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
|
||||
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
@@ -44,6 +41,14 @@ github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/C
|
||||
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.13.0 h1:Hl08k64maU0nrRyVUEbwCnK6/gT0EhMrtAqBHz9/gA4=
|
||||
github.com/jackc/pgx/v5 v5.13.0/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds=
|
||||
github.com/jackc/puddle/v2 v2.2.3 h1:f8HoREISb3l5eO9coPgK7U3hKLhWGokOFVcThl5jrao=
|
||||
github.com/jackc/puddle/v2 v2.2.3/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/jcmturner/aescts/v2 v2.0.0 h1:9YKLH6ey7H4eDBXW8khjYslgyqG2xZikXP0EQFKrle8=
|
||||
github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs=
|
||||
github.com/jcmturner/dnsutils/v2 v2.0.0 h1:lltnkeZGL0wILNvrNiVCR6Ro5PGU/SeBvVO/8c/iPbo=
|
||||
@@ -58,10 +63,6 @@ github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZ
|
||||
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
|
||||
github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4=
|
||||
github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4=
|
||||
github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42 h1:A1Cq6Ysb0GM0tpKMbdCXCIfBclan4oHk1Jb+Hrejirg=
|
||||
github.com/mdlayher/netlink v1.7.3-0.20250113171957-fbb4dce95f42/go.mod h1:BB4YCPDOzfy7FniQ/lxuYQ3dgmM2cZumHbK8RpTjN2o=
|
||||
github.com/mdlayher/socket v0.5.0 h1:ilICZmJcQz70vrWVes1MFera4jGiWNocSkykwwoy3XI=
|
||||
github.com/mdlayher/socket v0.5.0/go.mod h1:WkcBFfvyG8QENs5+hfQPl1X6Jpd2yeLIYgrGFmJiJxI=
|
||||
github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=
|
||||
github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA=
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
@@ -77,11 +78,14 @@ github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NF
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/teambition/rrule-go v1.8.2 h1:lIjpjvWTj9fFUZCmuoVDrKVOtdiyzbzc93qTmRVe/J8=
|
||||
github.com/teambition/rrule-go v1.8.2/go.mod h1:Ieq5AbrKGciP1V//Wq8ktsTXwSwJHDD5mD/wLBGl3p4=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
@@ -95,14 +99,15 @@ golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDf
|
||||
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
|
||||
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
|
||||
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
|
||||
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
|
||||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.38.0 h1:MECBjubtXD7yj4HrhIUcywNaGeNVUdfVnxmPajOk4yk=
|
||||
golang.org/x/mod v0.38.0/go.mod h1:V6Xz0pq8TQ3dGqVQ1FVHuelZpAL0uNhSkk9ogYP3c40=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
@@ -123,8 +128,8 @@ golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
|
||||
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
|
||||
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
@@ -137,8 +142,8 @@ golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
|
||||
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
|
||||
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
@@ -158,15 +163,16 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
|
||||
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
|
||||
golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
|
||||
golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||
golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE=
|
||||
golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package calendar
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
// NewPostgreSQL initializes disposable indexes, not canonical calendar data.
|
||||
// All writers sharing this DB must share Root and its working flock semantics.
|
||||
// Separate replicas with independent filesystems are not a supported HA mode.
|
||||
func NewPostgreSQL(ctx context.Context, root string, database *sql.DB) (*Repository, error) {
|
||||
if root == "" || database == nil {
|
||||
return nil, errors.New("calendar storage unavailable")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
err := db.ApplyPostgreSQLMigrations(ctx, database, "calendar-cache", []db.PostgreSQLMigration{{Version: 1, Name: "rebuildable monthly calendar indexes", Statements: []string{
|
||||
`CREATE TABLE calendar_index_state(domain TEXT COLLATE "C" NOT NULL,calendar TEXT COLLATE "C" NOT NULL,version TEXT NOT NULL,PRIMARY KEY(domain,calendar))`,
|
||||
`CREATE TABLE calendar_month_cache(domain TEXT COLLATE "C" NOT NULL,calendar TEXT COLLATE "C" NOT NULL,month TEXT COLLATE "C" NOT NULL,hash TEXT NOT NULL,PRIMARY KEY(domain,calendar,month))`,
|
||||
`CREATE TABLE calendar_event_cache(domain TEXT COLLATE "C" NOT NULL,calendar TEXT COLLATE "C" NOT NULL,uid TEXT COLLATE "C" NOT NULL,month TEXT COLLATE "C" NOT NULL,start BIGINT NOT NULL,"end" BIGINT NOT NULL,etag TEXT NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain,calendar,uid))`,
|
||||
`CREATE INDEX calendar_event_range ON calendar_event_cache(domain,calendar,start,"end")`,
|
||||
}, Assertions: []string{
|
||||
`SELECT domain,calendar,version FROM calendar_index_state WHERE 1=0`,
|
||||
`SELECT domain,calendar,month,hash FROM calendar_month_cache WHERE 1=0`,
|
||||
`SELECT domain,calendar,uid,month,start,"end",etag,data FROM calendar_event_cache WHERE 1=0`,
|
||||
}}})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Repository{Root: root, DB: database, postgres: true}, nil
|
||||
}
|
||||
|
||||
// Compose source-owned SQL; never translate arbitrary SQL at runtime.
|
||||
func (r *Repository) p(n int) string {
|
||||
if r.postgres {
|
||||
return fmt.Sprintf("$%d", n)
|
||||
}
|
||||
return "?"
|
||||
}
|
||||
|
||||
func (r *Repository) failure(err error) error {
|
||||
if !r.postgres || err == nil {
|
||||
return err
|
||||
}
|
||||
for _, known := range []error{ErrForbidden, ErrConflict, ErrPreconditionRequired, ErrMissing, ErrLimit, ErrInvalid, ErrNameTaken, sql.ErrNoRows, context.Canceled, context.DeadlineExceeded} {
|
||||
if errors.Is(err, known) {
|
||||
return known
|
||||
}
|
||||
}
|
||||
var safe *db.PostgreSQLError
|
||||
if errors.As(err, &safe) {
|
||||
return safe
|
||||
}
|
||||
var pg *pgconn.PgError
|
||||
if errors.As(err, &pg) {
|
||||
return &db.PostgreSQLError{Operation: "calendar cache", SQLState: pg.Code}
|
||||
}
|
||||
// Encoding/Scan errors can contain event contents too.
|
||||
return errors.New("calendar operation failed")
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
func DAVFilename(c Calendar, uid string) string {
|
||||
@@ -36,7 +38,7 @@ func DAVUID(c Calendar, name string) (string, error) {
|
||||
}
|
||||
}
|
||||
b, err := base64.RawURLEncoding.DecodeString(strings.TrimSuffix(name, ".ics"))
|
||||
if err != nil || len(b) == 0 || len(b) > 512 {
|
||||
if err != nil || len(b) == 0 || len(b) > 512 || !utf8.Valid(b) || strings.IndexFunc(string(b), unicode.IsControl) >= 0 {
|
||||
return "", ErrMissing
|
||||
}
|
||||
return string(b), nil
|
||||
@@ -44,7 +46,8 @@ func DAVUID(c Calendar, name string) (string, error) {
|
||||
|
||||
// DAVSnapshot is complete or fails; a truncated listing can cause client deletes.
|
||||
// The metadata lock keeps href mappings and event generations in one snapshot.
|
||||
func (r *Repository) DAVSnapshot(ctx context.Context, domain, id, actor string, withData bool) (Calendar, []Event, error) {
|
||||
func (r *Repository) DAVSnapshot(ctx context.Context, domain, id, actor string, withData bool) (result Calendar, itemsResult []Event, resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
c, _, unlock, err := r.locked(ctx, domain, id, actor)
|
||||
if err != nil {
|
||||
return c, nil, err
|
||||
@@ -54,7 +57,7 @@ func (r *Repository) DAVSnapshot(ctx context.Context, domain, id, actor string,
|
||||
if withData {
|
||||
cols = "uid,etag,data"
|
||||
}
|
||||
rows, err := r.DB.QueryContext(ctx, `SELECT `+cols+` FROM calendar_event_cache WHERE domain=? AND calendar=? ORDER BY uid LIMIT 10001`, domain, id)
|
||||
rows, err := r.DB.QueryContext(ctx, `SELECT `+cols+` FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` ORDER BY uid LIMIT 10001`, domain, id)
|
||||
if err != nil {
|
||||
return c, nil, err
|
||||
}
|
||||
@@ -77,7 +80,8 @@ func (r *Repository) DAVSnapshot(ctx context.Context, domain, id, actor string,
|
||||
|
||||
// ChangeDAV reserves resource identities in the same manifest commit as the
|
||||
// monthly snapshots. Existing resources still require an exact strong ETag.
|
||||
func (r *Repository) ChangeDAV(ctx context.Context, domain, id, actor, name, match string, item *Event, authorize func() error) (Event, bool, error) {
|
||||
func (r *Repository) ChangeDAV(ctx context.Context, domain, id, actor, name, match string, item *Event, authorize func() error) (eventResult Event, created bool, resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
if !ValidDAVFilename(name) {
|
||||
return Event{}, false, ErrInvalid
|
||||
}
|
||||
@@ -94,7 +98,7 @@ func (r *Repository) ChangeDAV(ctx context.Context, domain, id, actor, name, mat
|
||||
uid, _ := DAVUID(c, name)
|
||||
var exists bool
|
||||
if uid != "" {
|
||||
if err = r.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?)`, domain, id, uid).Scan(&exists); err != nil {
|
||||
if err = r.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND uid=`+r.p(3)+`)`, domain, id, uid).Scan(&exists); err != nil {
|
||||
return Event{}, false, err
|
||||
}
|
||||
}
|
||||
@@ -114,7 +118,7 @@ func (r *Repository) ChangeDAV(ctx context.Context, domain, id, actor, name, mat
|
||||
}
|
||||
uid = item.UID
|
||||
var tag string
|
||||
err = r.DB.QueryRowContext(ctx, `SELECT etag FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&tag)
|
||||
err = r.DB.QueryRowContext(ctx, `SELECT etag FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND uid=`+r.p(3), domain, id, uid).Scan(&tag)
|
||||
if err == nil {
|
||||
return Event{}, false, ErrConflict
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package calendar
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -9,6 +10,22 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestDAVUIDRejectsBinaryFallback(t *testing.T) {
|
||||
for _, raw := range []string{"\xff\xfe", "uid\x00value", "uid\nvalue"} {
|
||||
name := base64.RawURLEncoding.EncodeToString([]byte(raw)) + ".ics"
|
||||
if _, err := DAVUID(Calendar{}, name); !errors.Is(err, ErrMissing) {
|
||||
t.Fatalf("binary UID accepted: %v", err)
|
||||
}
|
||||
}
|
||||
name := "client.ics"
|
||||
if _, err := DAVUID(Calendar{}, name); !errors.Is(err, ErrMissing) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if uid, err := DAVUID(Calendar{DAVNames: map[string]string{"日本語@example.invalid": name}}, name); err != nil || uid != "日本語@example.invalid" {
|
||||
t.Fatal(uid, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendarDAVIdentityAtomicAndRebuild(t *testing.T) {
|
||||
r, domain, actor, c := fixture(t)
|
||||
ctx := context.Background()
|
||||
|
||||
@@ -51,6 +51,7 @@ type Repository struct {
|
||||
Root string
|
||||
DB *sql.DB
|
||||
PublicAllowed func(string) bool
|
||||
postgres bool
|
||||
}
|
||||
|
||||
func (r *Repository) canRead(domain string, c Calendar, actor string) bool {
|
||||
@@ -66,8 +67,8 @@ func New(root string, db *sql.DB) (*Repository, error) {
|
||||
}
|
||||
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS calendar_index_state(domain TEXT NOT NULL,calendar TEXT NOT NULL,version TEXT NOT NULL,PRIMARY KEY(domain,calendar));
|
||||
CREATE TABLE IF NOT EXISTS calendar_month_cache(domain TEXT NOT NULL,calendar TEXT NOT NULL,month TEXT NOT NULL,hash TEXT NOT NULL,PRIMARY KEY(domain,calendar,month));
|
||||
CREATE TABLE IF NOT EXISTS calendar_event_cache(domain TEXT NOT NULL,calendar TEXT NOT NULL,uid TEXT NOT NULL,month TEXT NOT NULL,start INTEGER NOT NULL,end INTEGER NOT NULL,etag TEXT NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain,calendar,uid));
|
||||
CREATE INDEX IF NOT EXISTS calendar_event_range ON calendar_event_cache(domain,calendar,start,end);`)
|
||||
CREATE TABLE IF NOT EXISTS calendar_event_cache(domain TEXT NOT NULL,calendar TEXT NOT NULL,uid TEXT NOT NULL,month TEXT NOT NULL,start INTEGER NOT NULL,"end" INTEGER NOT NULL,etag TEXT NOT NULL,data TEXT NOT NULL,PRIMARY KEY(domain,calendar,uid));
|
||||
CREATE INDEX IF NOT EXISTS calendar_event_range ON calendar_event_cache(domain,calendar,start,"end");`)
|
||||
return &Repository{Root: root, DB: db}, err
|
||||
}
|
||||
func validID(s string) bool { u, err := uuid.Parse(s); return err == nil && u.String() == s }
|
||||
@@ -347,7 +348,10 @@ func (r *Repository) Settings(ctx context.Context, domain, id, actor, version, n
|
||||
// with event/source writes. Cache rows are disposable, but deleting them
|
||||
// before the atomic directory rename means a failed rename can safely rebuild
|
||||
// them from the still-visible canonical files on the next access.
|
||||
func (r *Repository) Delete(ctx context.Context, domain, id, actor, version string) error {
|
||||
func (r *Repository) Delete(ctx context.Context, domain, id, actor, version string) (resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
dir, err := r.dir(domain, id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -382,7 +386,7 @@ func (r *Repository) Delete(ctx context.Context, domain, id, actor, version stri
|
||||
}
|
||||
defer tx.Rollback()
|
||||
for _, table := range []string{"calendar_event_cache", "calendar_month_cache", "calendar_index_state"} {
|
||||
if _, err = tx.ExecContext(ctx, `DELETE FROM `+table+` WHERE domain=? AND calendar=?`, domain, id); err != nil {
|
||||
if _, err = tx.ExecContext(ctx, `DELETE FROM `+table+` WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2), domain, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -413,9 +417,12 @@ func syncDirectory(path string) error {
|
||||
|
||||
// refresh reconciles only changed monthly generations. A crash after manifest
|
||||
// commit but before index commit repairs itself on the next request.
|
||||
func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir string) error {
|
||||
func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir string) (resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
var version string
|
||||
if err := r.DB.QueryRowContext(ctx, `SELECT version FROM calendar_index_state WHERE domain=? AND calendar=?`, domain, c.ID).Scan(&version); err == nil && version == c.Version {
|
||||
if err := r.DB.QueryRowContext(ctx, `SELECT version FROM calendar_index_state WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2), domain, c.ID).Scan(&version); err == nil && version == c.Version {
|
||||
return nil
|
||||
} else if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
@@ -425,7 +432,7 @@ func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir
|
||||
return e
|
||||
}
|
||||
defer tx.Rollback()
|
||||
rows, e := tx.QueryContext(ctx, `SELECT month,hash FROM calendar_month_cache WHERE domain=? AND calendar=?`, domain, c.ID)
|
||||
rows, e := tx.QueryContext(ctx, `SELECT month,hash FROM calendar_month_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2), domain, c.ID)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
@@ -445,10 +452,10 @@ func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir
|
||||
}
|
||||
for m, h := range known {
|
||||
if c.Months[m] != h {
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_event_cache WHERE domain=? AND calendar=? AND month=?`, domain, c.ID, m); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND month=`+r.p(3), domain, c.ID, m); e != nil {
|
||||
return e
|
||||
}
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_month_cache WHERE domain=? AND calendar=? AND month=?`, domain, c.ID, m); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `DELETE FROM calendar_month_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND month=`+r.p(3), domain, c.ID, m); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
@@ -480,15 +487,15 @@ func (r *Repository) refresh(ctx context.Context, domain string, c Calendar, dir
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_event_cache(domain,calendar,uid,month,start,end,etag,data) VALUES(?,?,?,?,?,?,?,?)`, domain, c.ID, event.UID, m, event.Start.Unix(), event.End.Unix(), event.ETag, event.Data); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_event_cache(domain,calendar,uid,month,start,"end",etag,data) VALUES(`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`,`+r.p(4)+`,`+r.p(5)+`,`+r.p(6)+`,`+r.p(7)+`,`+r.p(8)+`)`, domain, c.ID, event.UID, m, event.Start.Unix(), event.End.Unix(), event.ETag, event.Data); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_month_cache(domain,calendar,month,hash) VALUES(?,?,?,?)`, domain, c.ID, m, h); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_month_cache(domain,calendar,month,hash) VALUES(`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`,`+r.p(4)+`)`, domain, c.ID, m, h); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_index_state(domain,calendar,version) VALUES(?,?,?) ON CONFLICT(domain,calendar) DO UPDATE SET version=excluded.version`, domain, c.ID, c.Version); e != nil {
|
||||
if _, e = tx.ExecContext(ctx, `INSERT INTO calendar_index_state(domain,calendar,version) VALUES(`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`) ON CONFLICT(domain,calendar) DO UPDATE SET version=excluded.version`, domain, c.ID, c.Version); e != nil {
|
||||
return e
|
||||
}
|
||||
return tx.Commit()
|
||||
@@ -540,7 +547,8 @@ func (r *Repository) query(ctx context.Context, domain, id, actor string, start,
|
||||
func (r *Repository) Upcoming(ctx context.Context, domain, id, actor string, start, end time.Time) ([]Event, error) {
|
||||
return r.queryLimit(ctx, domain, id, actor, start, end, true, 20)
|
||||
}
|
||||
func (r *Repository) queryLimit(ctx context.Context, domain, id, actor string, start, end time.Time, withData bool, limit int) ([]Event, error) {
|
||||
func (r *Repository) queryLimit(ctx context.Context, domain, id, actor string, start, end time.Time, withData bool, limit int) (result []Event, resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
if !end.After(start) || end.Sub(start) > 370*24*time.Hour {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
@@ -553,7 +561,7 @@ func (r *Repository) queryLimit(ctx context.Context, domain, id, actor string, s
|
||||
if withData {
|
||||
columns = "data"
|
||||
}
|
||||
rows, e := r.DB.QueryContext(ctx, `SELECT `+columns+` FROM calendar_event_cache WHERE domain=? AND calendar=? AND start<? AND (end>? OR (start=end AND start>=?)) ORDER BY start,uid LIMIT ?`, domain, id, end.Unix(), start.Unix(), start.Unix(), limit)
|
||||
rows, e := r.DB.QueryContext(ctx, `SELECT `+columns+` FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND start<`+r.p(3)+` AND ("end">`+r.p(4)+` OR (start="end" AND start>=`+r.p(5)+`)) ORDER BY start,uid LIMIT `+r.p(6), domain, id, end.Unix(), start.Unix(), start.Unix(), limit)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
@@ -583,14 +591,15 @@ func (r *Repository) queryLimit(ctx context.Context, domain, id, actor string, s
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (r *Repository) Event(ctx context.Context, domain, id, actor, uid string) (Event, error) {
|
||||
func (r *Repository) Event(ctx context.Context, domain, id, actor, uid string) (result Event, resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
_, _, unlock, e := r.locked(ctx, domain, id, actor)
|
||||
if e != nil {
|
||||
return Event{}, e
|
||||
}
|
||||
defer unlock()
|
||||
var data string
|
||||
e = r.DB.QueryRowContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&data)
|
||||
e = r.DB.QueryRowContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND uid=`+r.p(3), domain, id, uid).Scan(&data)
|
||||
if e == sql.ErrNoRows {
|
||||
e = ErrMissing
|
||||
}
|
||||
@@ -611,13 +620,14 @@ func (r *Repository) Change(ctx context.Context, domain, id, actor, uid, match s
|
||||
return r.changeLocked(ctx, domain, actor, uid, match, item, c, dir)
|
||||
}
|
||||
|
||||
func (r *Repository) changeLocked(ctx context.Context, domain, actor, uid, match string, item *Event, c Calendar, dir string) (Event, error) {
|
||||
func (r *Repository) changeLocked(ctx context.Context, domain, actor, uid, match string, item *Event, c Calendar, dir string) (result Event, resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
id := c.ID
|
||||
if !c.CanWrite(actor) {
|
||||
return Event{}, ErrForbidden
|
||||
}
|
||||
var oldData, oldMonth, oldTag string
|
||||
e := r.DB.QueryRowContext(ctx, `SELECT data,month,etag FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&oldData, &oldMonth, &oldTag)
|
||||
e := r.DB.QueryRowContext(ctx, `SELECT data,month,etag FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND uid=`+r.p(3), domain, id, uid).Scan(&oldData, &oldMonth, &oldTag)
|
||||
exists := e == nil
|
||||
if e != nil && e != sql.ErrNoRows {
|
||||
return Event{}, e
|
||||
@@ -650,7 +660,7 @@ func (r *Repository) changeLocked(ctx context.Context, domain, actor, uid, match
|
||||
months[newMonth] = true
|
||||
}
|
||||
for month := range months {
|
||||
rows, e := r.DB.QueryContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=? AND calendar=? AND month=? AND uid<>? ORDER BY uid`, domain, id, month, uid)
|
||||
rows, e := r.DB.QueryContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND month=`+r.p(3)+` AND uid<>`+r.p(4)+` ORDER BY uid`, domain, id, month, uid)
|
||||
if e != nil {
|
||||
return Event{}, e
|
||||
}
|
||||
@@ -717,7 +727,7 @@ func (r *Repository) changeLocked(ctx context.Context, domain, actor, uid, match
|
||||
_ = os.Remove(file)
|
||||
}
|
||||
}
|
||||
// The committed files, not SQLite, determine success. A subsequent read
|
||||
// The committed files, not the database cache, determine success. A subsequent read
|
||||
// reconciles the cache; retries with the old ETag correctly get 412.
|
||||
return next, nil
|
||||
}
|
||||
@@ -25,14 +25,15 @@ func (r *Repository) ReadPersonalLocalIndex(ctx context.Context, domain, id, act
|
||||
// TaskBlock gets a current event, or creates it if absent and a draft is supplied.
|
||||
// Retries NEVER overwrite an event moved/edited in the calendar. Metadata policy
|
||||
// is checked under the same lock as snapshot access and the eventual commit.
|
||||
func (r *Repository) TaskBlock(ctx context.Context, domain, id, actor, uid string, draft *Event) (Calendar, Event, error) {
|
||||
func (r *Repository) TaskBlock(ctx context.Context, domain, id, actor, uid string, draft *Event) (calendarResult Calendar, eventResult Event, resultErr error) {
|
||||
defer func() { resultErr = r.failure(resultErr) }()
|
||||
c, dir, unlock, err := r.lockedWithGuard(ctx, domain, id, actor, func(c Calendar) bool { return c.PersonalLocal(actor) })
|
||||
if err != nil {
|
||||
return Calendar{}, Event{}, err
|
||||
}
|
||||
defer unlock()
|
||||
var data string
|
||||
err = r.DB.QueryRowContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=? AND calendar=? AND uid=?`, domain, id, uid).Scan(&data)
|
||||
err = r.DB.QueryRowContext(ctx, `SELECT data FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND uid=`+r.p(3), domain, id, uid).Scan(&data)
|
||||
if err == nil {
|
||||
event, e := Parse(data)
|
||||
return c, event, e
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
// PostgreSQLOptions is an instance-level secret-bearing configuration. Do not
|
||||
// log it. Application startup additionally requires a verified full-import
|
||||
// receipt; opening a connection alone does not authorize backend cutover.
|
||||
type PostgreSQLOptions struct {
|
||||
DSN string
|
||||
MaxOpenConns int
|
||||
MaxIdleConns int
|
||||
ConnectTimeout time.Duration
|
||||
// AllowInsecure permits plaintext or unverified TLS for isolated development
|
||||
// only. Production defaults require verified TLS and no plaintext fallback.
|
||||
AllowInsecure bool
|
||||
}
|
||||
|
||||
// PostgreSQLError deliberately excludes server messages, SQL, connection
|
||||
// strings and constraint details, all of which can contain credentials or data.
|
||||
// SQLState remains available for retry/conflict classification.
|
||||
type PostgreSQLError struct {
|
||||
Operation string
|
||||
SQLState string
|
||||
}
|
||||
|
||||
func (e *PostgreSQLError) Error() string {
|
||||
if e.SQLState != "" {
|
||||
return fmt.Sprintf("PostgreSQL %s failed (SQLSTATE %s)", e.Operation, e.SQLState)
|
||||
}
|
||||
return "PostgreSQL " + e.Operation + " failed"
|
||||
}
|
||||
|
||||
func postgresError(operation string, err error) error {
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return context.Canceled
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return context.DeadlineExceeded
|
||||
}
|
||||
var pgErr *pgconn.PgError
|
||||
result := &PostgreSQLError{Operation: operation}
|
||||
if errors.As(err, &pgErr) {
|
||||
result.SQLState = pgErr.Code
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func OpenPostgreSQL(ctx context.Context, options PostgreSQLOptions) (*sql.DB, error) {
|
||||
if options.DSN == "" {
|
||||
return nil, fmt.Errorf("PostgreSQL DSN is required")
|
||||
}
|
||||
if options.MaxOpenConns < 0 || options.MaxIdleConns < 0 || options.ConnectTimeout < 0 {
|
||||
return nil, fmt.Errorf("invalid PostgreSQL pool or timeout configuration")
|
||||
}
|
||||
config, err := pgx.ParseConfig(options.DSN)
|
||||
if err != nil {
|
||||
return nil, postgresError("configuration", err)
|
||||
}
|
||||
if !options.AllowInsecure {
|
||||
if config.TLSConfig == nil || config.TLSConfig.InsecureSkipVerify {
|
||||
return nil, fmt.Errorf("PostgreSQL requires verified TLS; use sslmode=verify-full")
|
||||
}
|
||||
for _, fallback := range config.Fallbacks {
|
||||
if fallback.TLSConfig == nil || fallback.TLSConfig.InsecureSkipVerify {
|
||||
return nil, fmt.Errorf("PostgreSQL insecure connection fallback is not allowed")
|
||||
}
|
||||
}
|
||||
}
|
||||
timeout := options.ConnectTimeout
|
||||
if timeout == 0 {
|
||||
timeout = 10 * time.Second
|
||||
}
|
||||
config.ConnectTimeout = timeout
|
||||
config.RuntimeParams["application_name"] = "pageswiki"
|
||||
database := stdlib.OpenDB(*config)
|
||||
maxOpen := options.MaxOpenConns
|
||||
if maxOpen == 0 {
|
||||
maxOpen = 10
|
||||
}
|
||||
maxIdle := options.MaxIdleConns
|
||||
if maxIdle == 0 {
|
||||
maxIdle = 2
|
||||
}
|
||||
if maxIdle > maxOpen {
|
||||
maxIdle = maxOpen
|
||||
}
|
||||
database.SetMaxOpenConns(maxOpen)
|
||||
database.SetMaxIdleConns(maxIdle)
|
||||
database.SetConnMaxLifetime(30 * time.Minute)
|
||||
database.SetConnMaxIdleTime(5 * time.Minute)
|
||||
pingCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
if err := database.PingContext(pingCtx); err != nil {
|
||||
_ = database.Close()
|
||||
return nil, postgresError("connect", err)
|
||||
}
|
||||
return database, nil
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PostgreSQLMigration contains native PostgreSQL SQL ($1 parameters, BIGINT
|
||||
// timestamps). SQLite migration definitions must not be translated implicitly.
|
||||
// Definitions are append-only after release. Assertions return no rows on success.
|
||||
type PostgreSQLMigration struct {
|
||||
Version int
|
||||
Name string
|
||||
Statements []string
|
||||
Assertions []string `json:",omitempty"`
|
||||
}
|
||||
|
||||
func postgresMigrationChecksum(step PostgreSQLMigration) string {
|
||||
encoded, _ := json.Marshal(step)
|
||||
sum := sha256.Sum256(encoded)
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// ApplyPostgreSQLMigrations uses one transaction-scoped advisory lock for the
|
||||
// migration namespace, including ledger creation. Component-specific locks
|
||||
// would race concurrent first creation of the shared ledger. READ COMMITTED
|
||||
// ensures a waiter sees the previous owner's committed migration history.
|
||||
// Callers must use the same search_path and lock convention for a database.
|
||||
func ApplyPostgreSQLMigrations(ctx context.Context, database *sql.DB, component string, steps []PostgreSQLMigration) error {
|
||||
if strings.TrimSpace(component) == "" {
|
||||
return fmt.Errorf("migration component is required")
|
||||
}
|
||||
for i, step := range steps {
|
||||
if step.Version != i+1 || strings.TrimSpace(step.Name) == "" || len(step.Statements)+len(step.Assertions) == 0 {
|
||||
return fmt.Errorf("invalid PostgreSQL migration sequence for %s", component)
|
||||
}
|
||||
}
|
||||
tx, err := database.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
|
||||
if err != nil {
|
||||
return postgresError("migration begin", err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
// Stable application-specific key, independent of Go hashes and versions.
|
||||
if _, err := tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(1885431653, 1835624306)`); err != nil {
|
||||
return postgresError("migration lock", err)
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `CREATE TABLE IF NOT EXISTS schema_migration_steps (
|
||||
component TEXT NOT NULL, version INTEGER NOT NULL, name TEXT NOT NULL,
|
||||
checksum TEXT NOT NULL, applied_at_ms BIGINT NOT NULL,
|
||||
PRIMARY KEY(component,version))`); err != nil {
|
||||
return postgresError("migration ledger", err)
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT version,name,checksum FROM schema_migration_steps WHERE component=$1 ORDER BY version`, component)
|
||||
if err != nil {
|
||||
return postgresError("migration history", err)
|
||||
}
|
||||
applied := 0
|
||||
for rows.Next() {
|
||||
var version int
|
||||
var name, checksum string
|
||||
if err := rows.Scan(&version, &name, &checksum); err != nil {
|
||||
rows.Close()
|
||||
return postgresError("migration history", err)
|
||||
}
|
||||
if version != applied+1 || version > len(steps) || steps[version-1].Name != name || postgresMigrationChecksum(steps[version-1]) != checksum {
|
||||
rows.Close()
|
||||
return fmt.Errorf("PostgreSQL migration history mismatch: component=%s version=%d", component, version)
|
||||
}
|
||||
applied++
|
||||
}
|
||||
err = rows.Err()
|
||||
closeErr := rows.Close()
|
||||
if err != nil {
|
||||
return postgresError("migration history", err)
|
||||
}
|
||||
if closeErr != nil {
|
||||
return postgresError("migration history close", closeErr)
|
||||
}
|
||||
for _, step := range steps[applied:] {
|
||||
for i, statement := range step.Statements {
|
||||
if _, err := tx.ExecContext(ctx, statement); err != nil {
|
||||
return postgresError(fmt.Sprintf("migration %s/%d statement %d", component, step.Version, i+1), err)
|
||||
}
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO schema_migration_steps(component,version,name,checksum,applied_at_ms) VALUES($1,$2,$3,$4,$5)`, component, step.Version, step.Name, postgresMigrationChecksum(step), time.Now().UnixMilli()); err != nil {
|
||||
return postgresError("migration record", err)
|
||||
}
|
||||
}
|
||||
if len(steps) > 0 {
|
||||
step := steps[len(steps)-1]
|
||||
for i, assertion := range step.Assertions {
|
||||
rows, err := tx.QueryContext(ctx, assertion)
|
||||
if err != nil {
|
||||
return postgresError("schema assertion", err)
|
||||
}
|
||||
violated := rows.Next()
|
||||
err = rows.Err()
|
||||
closeErr := rows.Close()
|
||||
if err != nil {
|
||||
return postgresError("schema assertion", err)
|
||||
}
|
||||
if closeErr != nil {
|
||||
return postgresError("schema assertion close", closeErr)
|
||||
}
|
||||
if violated {
|
||||
return fmt.Errorf("PostgreSQL schema contract %s/%d assertion %d failed", component, step.Version, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return postgresError("migration commit", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
func TestPostgreSQLConfigurationAndRedaction(t *testing.T) {
|
||||
for _, options := range []PostgreSQLOptions{
|
||||
{}, {DSN: "postgres://test:secret@localhost/db", MaxOpenConns: -1},
|
||||
{DSN: "postgres://test:secret@localhost/db?sslmode=disable"},
|
||||
{DSN: "postgres://test:secret@localhost/db?sslmode=require"},
|
||||
{DSN: "postgres://test:secret@localhost/db?sslmode=prefer"},
|
||||
{DSN: "postgres://test:secret@localhost/db?sslmode=invalid"},
|
||||
} {
|
||||
database, err := OpenPostgreSQL(context.Background(), options)
|
||||
if database != nil || err == nil {
|
||||
t.Fatal("invalid configuration accepted")
|
||||
}
|
||||
if strings.Contains(err.Error(), "secret") {
|
||||
t.Fatal("password disclosed")
|
||||
}
|
||||
}
|
||||
err := postgresError("write", &pgconn.PgError{Code: "23505", Message: "secret", Detail: "private row"})
|
||||
var safe *PostgreSQLError
|
||||
if !errors.As(err, &safe) || safe.SQLState != "23505" || strings.Contains(err.Error(), "secret") {
|
||||
t.Fatal("unsafe error classification")
|
||||
}
|
||||
if !errors.Is(postgresError("read", context.Canceled), context.Canceled) {
|
||||
t.Fatal("cancellation lost")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
database, err := OpenPostgreSQL(ctx, PostgreSQLOptions{DSN: "postgres://test:secret@127.0.0.1:1/db?sslmode=disable", AllowInsecure: true})
|
||||
if database != nil || !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("connection cancellation lost")
|
||||
}
|
||||
}
|
||||
|
||||
// These tests create and drop only a random schema. Use a disposable database
|
||||
// and URL DSN, never a production connection. No fixtures contain real events.
|
||||
func postgresTestDatabase(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("PAGESWIKI_TEST_POSTGRES_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("set PAGESWIKI_TEST_POSTGRES_DSN to a disposable PostgreSQL URL")
|
||||
}
|
||||
u, err := url.Parse(dsn)
|
||||
if err != nil || (u.Scheme != "postgres" && u.Scheme != "postgresql") {
|
||||
t.Fatal("test DSN must be a PostgreSQL URL")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
root, err := OpenPostgreSQL(ctx, PostgreSQLOptions{DSN: dsn, AllowInsecure: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schema := "pageswiki_test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
if _, err := root.ExecContext(ctx, `CREATE SCHEMA "`+schema+`"`); err != nil {
|
||||
root.Close()
|
||||
t.Fatal(postgresError("test schema", err))
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
cleanup, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if _, err := root.ExecContext(cleanup, `DROP SCHEMA "`+schema+`" CASCADE`); err != nil {
|
||||
t.Error(postgresError("test cleanup", err))
|
||||
}
|
||||
root.Close()
|
||||
})
|
||||
query := u.Query()
|
||||
query.Set("search_path", schema)
|
||||
u.RawQuery = query.Encode()
|
||||
database, err := OpenPostgreSQL(ctx, PostgreSQLOptions{DSN: u.String(), AllowInsecure: true, MaxOpenConns: 6})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
return database
|
||||
}
|
||||
|
||||
func TestPostgreSQLMigrationConcurrencyAndHistory(t *testing.T) {
|
||||
database := postgresTestDatabase(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
steps := []PostgreSQLMigration{{Version: 1, Name: "initial", Statements: []string{
|
||||
`CREATE TABLE sample (uuid TEXT PRIMARY KEY, created_at_ms BIGINT NOT NULL)`,
|
||||
`INSERT INTO sample VALUES ('synthetic', 1800000000000)`,
|
||||
}, Assertions: []string{`SELECT 1 WHERE NOT EXISTS (SELECT 1 FROM sample WHERE created_at_ms=1800000000000)`}}}
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, 6)
|
||||
for i := 0; i < 6; i++ {
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); errs <- ApplyPostgreSQLMigrations(ctx, database, "test", steps) }()
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var count int
|
||||
if err := database.QueryRowContext(ctx, `SELECT count(*) FROM sample`).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatalf("rows=%d error=%v", count, err)
|
||||
}
|
||||
if err := ApplyPostgreSQLMigrations(ctx, database, "test", nil); err == nil {
|
||||
t.Fatal("downgrade accepted")
|
||||
}
|
||||
changed := append([]PostgreSQLMigration(nil), steps...)
|
||||
changed[0].Name = "changed"
|
||||
if err := ApplyPostgreSQLMigrations(ctx, database, "test", changed); err == nil {
|
||||
t.Fatal("changed history accepted")
|
||||
}
|
||||
changed = append([]PostgreSQLMigration(nil), steps...)
|
||||
changed[0].Statements = []string{`SELECT 1`}
|
||||
if err := ApplyPostgreSQLMigrations(ctx, database, "test", changed); err == nil {
|
||||
t.Fatal("changed SQL checksum accepted")
|
||||
}
|
||||
if err := ApplyPostgreSQLMigrations(ctx, database, "other", []PostgreSQLMigration{{Version: 2, Name: "gap", Statements: []string{`SELECT 1`}}}); err == nil {
|
||||
t.Fatal("version gap accepted")
|
||||
}
|
||||
// Assertions are checked again even when no migrations remain to apply.
|
||||
if _, err := database.ExecContext(ctx, `DELETE FROM sample`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ApplyPostgreSQLMigrations(ctx, database, "test", steps); err == nil {
|
||||
t.Fatal("schema assertion skipped")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLMigrationRollbackAndLockCancellation(t *testing.T) {
|
||||
database := postgresTestDatabase(t)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
broken := []PostgreSQLMigration{{Version: 1, Name: "broken", Statements: []string{`CREATE TABLE partial (id INTEGER)`, `NOT VALID SQL`}}}
|
||||
if err := ApplyPostgreSQLMigrations(ctx, database, "test", broken); err == nil {
|
||||
t.Fatal("SQL error ignored")
|
||||
}
|
||||
var name sql.NullString
|
||||
if err := database.QueryRowContext(ctx, `SELECT to_regclass('partial')::text`).Scan(&name); err != nil || name.Valid {
|
||||
t.Fatal("DDL not rolled back", err)
|
||||
}
|
||||
if err := database.QueryRowContext(ctx, `SELECT to_regclass('schema_migration_steps')::text`).Scan(&name); err != nil || name.Valid {
|
||||
t.Fatal("ledger not rolled back", err)
|
||||
}
|
||||
broken[0].Statements = broken[0].Statements[:1]
|
||||
if err := ApplyPostgreSQLMigrations(ctx, database, "test", broken); err != nil {
|
||||
t.Fatal("retry failed", err)
|
||||
}
|
||||
assertionFailure := append(append([]PostgreSQLMigration(nil), broken...), PostgreSQLMigration{
|
||||
Version: 2, Name: "assertion failure", Statements: []string{`CREATE TABLE should_rollback (id INTEGER)`}, Assertions: []string{`SELECT 1`},
|
||||
})
|
||||
if err := ApplyPostgreSQLMigrations(ctx, database, "test", assertionFailure); err == nil {
|
||||
t.Fatal("assertion failure ignored")
|
||||
}
|
||||
if err := database.QueryRowContext(ctx, `SELECT to_regclass('should_rollback')::text`).Scan(&name); err != nil || name.Valid {
|
||||
t.Fatal("assertion DDL not rolled back", err)
|
||||
}
|
||||
var version int
|
||||
if err := database.QueryRowContext(ctx, `SELECT max(version) FROM schema_migration_steps WHERE component=$1`, "test").Scan(&version); err != nil || version != 1 {
|
||||
t.Fatal("assertion ledger not rolled back", err)
|
||||
}
|
||||
tx, err := database.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err = tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(1885431653, 1835624306)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
waitCtx, stop := context.WithTimeout(ctx, 100*time.Millisecond)
|
||||
defer stop()
|
||||
if err := ApplyPostgreSQLMigrations(waitCtx, database, "test", broken); err == nil {
|
||||
t.Fatal("blocked migration ignored deadline")
|
||||
}
|
||||
if err := tx.Rollback(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ApplyPostgreSQLMigrations(ctx, database, "test", broken); err != nil {
|
||||
t.Fatal(fmt.Sprintf("lock not released: %v", err))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
// Package documentrefs freezes mutable Asset references without interpreting
|
||||
// ordinary text or code examples. Resolvers must authorize the source Domain.
|
||||
package documentrefs
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
var ErrUnavailable = errors.New("embedded asset is unavailable")
|
||||
|
||||
type Resolver func(context.Context, string) (string, error)
|
||||
|
||||
// Freeze retains original bytes for unchanged tokens and deduplicates hashes
|
||||
// in appearance order. Repeated addresses resolve only once per payload.
|
||||
func Freeze(ctx context.Context, payload []byte, contentType string, resolve Resolver) ([]byte, []string, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil || !strings.EqualFold(mediaType, "text/html") {
|
||||
return payload, nil, nil
|
||||
}
|
||||
z := html.NewTokenizer(bytes.NewReader(payload))
|
||||
var output bytes.Buffer
|
||||
seen := map[string]bool{}
|
||||
resolved := map[string]string{}
|
||||
var hashes []string
|
||||
lookup := func(address string) (string, error) {
|
||||
if hash := resolved[address]; hash != "" {
|
||||
return hash, nil
|
||||
}
|
||||
if resolve == nil {
|
||||
return "", ErrUnavailable
|
||||
}
|
||||
hash, err := resolve(ctx, address)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(hash) != 64 {
|
||||
return "", fmt.Errorf("%w: %s", ErrUnavailable, address)
|
||||
}
|
||||
if _, err := hex.DecodeString(hash); err != nil {
|
||||
return "", fmt.Errorf("%w: %s", ErrUnavailable, address)
|
||||
}
|
||||
hash = strings.ToLower(hash)
|
||||
resolved[address] = hash
|
||||
if !seen[hash] {
|
||||
seen[hash] = true
|
||||
hashes = append(hashes, hash)
|
||||
}
|
||||
return hash, nil
|
||||
}
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
typ := z.Next()
|
||||
if typ == html.ErrorToken {
|
||||
if errors.Is(z.Err(), io.EOF) {
|
||||
return output.Bytes(), hashes, nil
|
||||
}
|
||||
return nil, nil, z.Err()
|
||||
}
|
||||
if typ != html.StartTagToken && typ != html.SelfClosingTagToken {
|
||||
output.Write(z.Raw())
|
||||
continue
|
||||
}
|
||||
// Token() may alter the tokenizer buffer. Copy original bytes first.
|
||||
raw := append([]byte(nil), z.Raw()...)
|
||||
token := z.Token()
|
||||
changed := false
|
||||
assetRef := attribute(token.Attr, "asset-ref")
|
||||
legacy := attribute(token.Attr, "immutable-ref")
|
||||
if assetRef == "" && strings.HasPrefix(legacy, "asset:") {
|
||||
assetRef = legacy
|
||||
}
|
||||
if strings.HasPrefix(assetRef, "asset:") {
|
||||
hash, err := lookup(assetRef)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
setAttribute(&token, "asset-ref", assetRef)
|
||||
setAttribute(&token, "immutable-ref", "sha256:"+hash)
|
||||
changed = true
|
||||
}
|
||||
for _, name := range []string{"src", "href", "poster", "data"} {
|
||||
provenance := "data-pageswiki-asset-" + name
|
||||
address := attribute(token.Attr, provenance)
|
||||
if address == "" {
|
||||
candidate := attribute(token.Attr, name)
|
||||
if strings.HasPrefix(candidate, "asset:") {
|
||||
address = candidate
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(address, "asset:") {
|
||||
continue
|
||||
}
|
||||
hash, err := lookup(address)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
setAttribute(&token, provenance, address)
|
||||
setAttribute(&token, name, "/_/obj/sha256:"+hash)
|
||||
changed = true
|
||||
}
|
||||
if changed {
|
||||
output.WriteString(token.String())
|
||||
} else {
|
||||
output.Write(raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func attribute(attrs []html.Attribute, name string) string {
|
||||
for _, a := range attrs {
|
||||
if strings.EqualFold(a.Key, name) {
|
||||
return strings.TrimSpace(a.Val)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func setAttribute(token *html.Token, name, value string) {
|
||||
for i := range token.Attr {
|
||||
if strings.EqualFold(token.Attr[i].Key, name) {
|
||||
token.Attr[i].Key = name
|
||||
token.Attr[i].Val = value
|
||||
return
|
||||
}
|
||||
}
|
||||
token.Attr = append(token.Attr, html.Attribute{Key: name, Val: value})
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package documentrefs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFreezePreservesUnchangedHTML(t *testing.T) {
|
||||
body := `<!DOCTYPE html><!-- sample --><DIV class='A' title="&">日本語<code><img src="asset:image:example"></code><script>const x="asset:image:sample";</script><a HREF='/plain?a=1&b=2'>Link</a></DIV>`
|
||||
got, refs, err := Freeze(context.Background(), []byte(body), "text/html; charset=utf-8", func(context.Context, string) (string, error) { t.Fatal("ordinary text resolved"); return "", nil })
|
||||
if err != nil || string(got) != body || len(refs) != 0 {
|
||||
t.Fatal("unchanged HTML lost bytes", err, string(got))
|
||||
}
|
||||
for _, mime := range []string{"text/plain", "application/octet-stream", "text/html-not-really"} {
|
||||
got, refs, err := Freeze(context.Background(), []byte(`<img src="asset:image:sample">`), mime, nil)
|
||||
if err != nil || len(refs) != 0 || string(got) != `<img src="asset:image:sample">` {
|
||||
t.Fatal("non-HTML modified", mime, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreezeDeduplicatesAndFollowsProvenance(t *testing.T) {
|
||||
hash := strings.Repeat("a", 64)
|
||||
calls := 0
|
||||
resolve := func(context.Context, string) (string, error) { calls++; return hash, nil }
|
||||
body := `<img src="asset:image:a"><video poster="asset:image:a"></video><object data="asset:image:b"></object><a href="asset:image:a">a</a><embedded-object immutable-ref="asset:image:a"></embedded-object>`
|
||||
got, refs, err := Freeze(context.Background(), []byte(body), "text/html", resolve)
|
||||
if err != nil || len(refs) != 1 || refs[0] != hash || calls != 2 {
|
||||
t.Fatal("deduplication", err, refs, calls)
|
||||
}
|
||||
if !strings.Contains(string(got), `immutable-ref="sha256:`+hash+`"`) || !strings.Contains(string(got), `data-pageswiki-asset-src="asset:image:a"`) {
|
||||
t.Fatal("provenance missing")
|
||||
}
|
||||
hash = strings.Repeat("b", 64)
|
||||
calls = 0
|
||||
updated, refs, err := Freeze(context.Background(), got, "text/html", resolve)
|
||||
if err != nil || len(refs) != 1 || refs[0] != hash || calls != 2 || strings.Contains(string(updated), strings.Repeat("a", 64)) {
|
||||
t.Fatal("resave did not follow HEAD", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFreezeRejectsMissingInvalidAndCanceled(t *testing.T) {
|
||||
for _, hash := range []string{"", strings.Repeat("x", 64)} {
|
||||
if _, _, err := Freeze(context.Background(), []byte(`<img src="asset:image:a">`), "text/html", func(context.Context, string) (string, error) { return hash, nil }); !errors.Is(err, ErrUnavailable) {
|
||||
t.Fatal("invalid hash accepted", err)
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if _, _, err := Freeze(ctx, []byte("text"), "text/plain", nil); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("cancellation ignored", err)
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,12 @@ type Runner struct {
|
||||
registry *Registry
|
||||
opts RunnerOptions
|
||||
mu sync.Mutex
|
||||
stopping bool
|
||||
stopCtx context.Context
|
||||
cancel context.CancelFunc
|
||||
workers sync.WaitGroup
|
||||
stopOnce sync.Once
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
func NewRunner(queue Queue, registry *Registry, opts RunnerOptions) *Runner {
|
||||
@@ -41,7 +47,30 @@ func NewRunner(queue Queue, registry *Registry, opts RunnerOptions) *Runner {
|
||||
if opts.Active == nil {
|
||||
opts.Active = new(atomic.Int64)
|
||||
}
|
||||
return &Runner{queue: queue, registry: registry, opts: opts}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &Runner{queue: queue, registry: registry, opts: opts, stopCtx: ctx, cancel: cancel, done: make(chan struct{})}
|
||||
}
|
||||
|
||||
// Stop closes admission permanently, cancels handlers and waits for persisted
|
||||
// completions. A timeout does not mean workers have exited: callers must keep
|
||||
// ownership/database resources alive and may call Stop again to await them.
|
||||
func (r *Runner) Stop(ctx context.Context) error {
|
||||
r.stopOnce.Do(func() {
|
||||
r.cancel()
|
||||
go func() {
|
||||
r.mu.Lock()
|
||||
r.stopping = true
|
||||
r.mu.Unlock()
|
||||
r.workers.Wait()
|
||||
close(r.done)
|
||||
}()
|
||||
})
|
||||
select {
|
||||
case <-r.done:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Runner) report(stage string, job *Job, err error) {
|
||||
@@ -57,7 +86,10 @@ func (r *Runner) Running() int64 { return r.opts.Active.Load() }
|
||||
func (r *Runner) Dispatch(ctx context.Context) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
for n := 0; n < 32 && ctx.Err() == nil; n++ {
|
||||
if r.stopping || r.stopCtx.Err() != nil {
|
||||
return
|
||||
}
|
||||
for n := 0; n < 32 && ctx.Err() == nil && r.stopCtx.Err() == nil; n++ {
|
||||
settings, err := r.queue.Settings(ctx)
|
||||
if err != nil {
|
||||
r.report("settings", nil, err)
|
||||
@@ -81,7 +113,15 @@ func (r *Runner) Dispatch(ctx context.Context) {
|
||||
}
|
||||
r.opts.Active.Add(1)
|
||||
r.report("start-log", job, r.queue.Started(ctx, job))
|
||||
go r.execute(ctx, job)
|
||||
workerCtx, cancel := context.WithCancel(ctx)
|
||||
stop := context.AfterFunc(r.stopCtx, cancel)
|
||||
r.workers.Add(1)
|
||||
go func() {
|
||||
defer r.workers.Done()
|
||||
defer cancel()
|
||||
defer stop()
|
||||
r.execute(workerCtx, job)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,53 @@ type finishedJob struct {
|
||||
err error
|
||||
at time.Time
|
||||
}
|
||||
|
||||
func TestRunnerStopWaitsAndClosesAdmission(t *testing.T) {
|
||||
q := newFakeQueue(1)
|
||||
reg := &Registry{}
|
||||
started := make(chan struct{})
|
||||
cancelled := make(chan struct{})
|
||||
exit := make(chan struct{})
|
||||
if err := reg.Register("synthetic", func(ctx context.Context, _ *Job) error {
|
||||
close(started)
|
||||
<-ctx.Done()
|
||||
close(cancelled)
|
||||
<-exit // simulate cleanup that cannot be interrupted
|
||||
return ctx.Err()
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := NewRunner(q, reg, RunnerOptions{})
|
||||
r.Dispatch(context.Background())
|
||||
receive(t, started)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if err := r.Stop(ctx); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
receive(t, cancelled)
|
||||
if r.Running() != 1 {
|
||||
t.Fatal("worker exited before cleanup")
|
||||
}
|
||||
r.Dispatch(context.Background())
|
||||
if q.claimed != 1 {
|
||||
t.Fatal("admission remained open")
|
||||
}
|
||||
close(exit)
|
||||
ctx, cancel = context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
if err := r.Stop(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
receive(t, q.finished)
|
||||
if r.Running() != 0 {
|
||||
t.Fatal("worker slot retained after stop")
|
||||
}
|
||||
if err := r.Stop(ctx); err != nil {
|
||||
t.Fatal("idempotent stop", err)
|
||||
}
|
||||
}
|
||||
|
||||
type fakeQueue struct {
|
||||
mu sync.Mutex
|
||||
settings Settings
|
||||
|
||||
@@ -13,6 +13,8 @@ var (
|
||||
)
|
||||
|
||||
type Job struct {
|
||||
// ClaimToken fences native database completion; never expose it to clients.
|
||||
ClaimToken string `json:"-"`
|
||||
UUID string `json:"uuid"`
|
||||
EventUUID string `json:"event_uuid,omitempty"`
|
||||
Type string `json:"event_type"`
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -36,6 +37,14 @@ type citySearchCandidate struct {
|
||||
// Scope selects a locality's own boundary, not an ancestor of the scope. Every
|
||||
// boundary in its parent chain must be readable before the result is exposed.
|
||||
func (s *SQLite) SearchCityInDomain(parent context.Context, domainUUID, scope, query string, limit int, allowed func(string) bool, options SearchOptions) (found []CitySearchResult, searchErr error) {
|
||||
return searchCity(s.db, false, parent, domainUUID, scope, query, limit, allowed, options)
|
||||
}
|
||||
|
||||
func (s *PostgreSQL) SearchCityInDomain(parent context.Context, domainUUID, scope, query string, limit int, allowed func(string) bool, options SearchOptions) ([]CitySearchResult, error) {
|
||||
return searchCity(s.db, true, parent, domainUUID, scope, query, limit, allowed, options)
|
||||
}
|
||||
|
||||
func searchCity(database *sql.DB, native bool, parent context.Context, domainUUID, scope, query string, limit int, allowed func(string) bool, options SearchOptions) (found []CitySearchResult, searchErr error) {
|
||||
if err := options.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -58,7 +67,7 @@ func (s *SQLite) SearchCityInDomain(parent context.Context, domainUUID, scope, q
|
||||
searchErr = ctx.Err()
|
||||
}
|
||||
}()
|
||||
tx, err := s.db.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
tx, err := database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -70,22 +79,35 @@ func (s *SQLite) SearchCityInDomain(parent context.Context, domainUUID, scope, q
|
||||
sql.Named("prefix", prefix), sql.Named("upper", upper),
|
||||
sql.Named("query", query), sql.Named("limit", CitySearchCandidateLimit),
|
||||
}
|
||||
if native {
|
||||
args = []any{domainUUID, scope, prefix, upper, query, CitySearchCandidateLimit}
|
||||
}
|
||||
dateColumn := "n.updated_at_ms"
|
||||
if options.DateField == "created" {
|
||||
dateColumn = "n.created_at_ms"
|
||||
}
|
||||
dateWhere := ""
|
||||
if options.FromMS != nil {
|
||||
dateWhere += " AND " + dateColumn + ">=:from_ms"
|
||||
args = append(args, sql.Named("from_ms", *options.FromMS))
|
||||
if native {
|
||||
args = append(args, *options.FromMS)
|
||||
dateWhere += " AND " + dateColumn + ">=" + fmt.Sprintf("$%d", len(args))
|
||||
} else {
|
||||
dateWhere += " AND " + dateColumn + ">=:from_ms"
|
||||
args = append(args, sql.Named("from_ms", *options.FromMS))
|
||||
}
|
||||
}
|
||||
if options.BeforeMS != nil {
|
||||
dateWhere += " AND " + dateColumn + "<:before_ms"
|
||||
args = append(args, sql.Named("before_ms", *options.BeforeMS))
|
||||
if native {
|
||||
args = append(args, *options.BeforeMS)
|
||||
dateWhere += " AND " + dateColumn + "<" + fmt.Sprintf("$%d", len(args))
|
||||
} else {
|
||||
dateWhere += " AND " + dateColumn + "<:before_ms"
|
||||
args = append(args, sql.Named("before_ms", *options.BeforeMS))
|
||||
}
|
||||
}
|
||||
candidates := make([]citySearchCandidate, 0)
|
||||
for _, kind := range []string{"quartier", "street", "spot"} {
|
||||
rows, err := tx.QueryContext(ctx, citySearchSQL(kind, dateWhere), args...)
|
||||
rows, err := tx.QueryContext(ctx, citySearchDialectSQL(kind, dateWhere, native), args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -173,6 +195,18 @@ func citySearchKindOrder(kind string) int {
|
||||
// All interpolated SQL fragments are package-owned constants. User input is
|
||||
// bound, so %, _, quotes and backslashes are literal substring characters.
|
||||
func citySearchSQL(kind, dateWhere string) string {
|
||||
return citySearchDialectSQL(kind, dateWhere, false)
|
||||
}
|
||||
|
||||
func citySearchDialectSQL(kind, dateWhere string, native bool) string {
|
||||
domain, scope, prefix, upper, query, limit := ":domain", ":scope", ":prefix", ":upper", ":query", ":limit"
|
||||
collation, position, greatest := " COLLATE BINARY", "instr", "max"
|
||||
absolute := citySearchAbsolutePathSQL
|
||||
if native {
|
||||
domain, scope, prefix, upper, query, limit = "$1", "$2", "$3", "$4", "$5", "$6"
|
||||
collation, position, greatest = ` COLLATE "C"`, "strpos", "greatest"
|
||||
absolute = func(column string) string { return `(substr(` + column + `,1,1)='/' AND length(` + column + `)<=4096)` }
|
||||
}
|
||||
from, description := "zones n", "n.description"
|
||||
navigationPath := "n.path_prefix"
|
||||
parentColumns := "'','','','',''"
|
||||
@@ -181,28 +215,32 @@ func citySearchSQL(kind, dateWhere string) string {
|
||||
case "street":
|
||||
from += ` JOIN zones q ON q.uuid=n.parent_uuid AND q.domain_uuid=n.domain_uuid AND q.kind='quartier' AND q.state!='archived'`
|
||||
parentColumns = "substr(COALESCE(NULLIF(q.title,''),q.name),1,256),'',q.path_prefix,'',q.name"
|
||||
where = "n.kind='street' AND " + citySearchAbsolutePathSQL("q.path_prefix")
|
||||
where = "n.kind='street' AND " + absolute("q.path_prefix")
|
||||
case "spot":
|
||||
navigationPath = `CASE WHEN substr(n.path_prefix,-11)='/_undefined' THEN st.path_prefix ELSE n.path_prefix END`
|
||||
if native {
|
||||
navigationPath = `CASE WHEN right(n.path_prefix,11)='/_undefined' THEN st.path_prefix ELSE n.path_prefix END`
|
||||
}
|
||||
from = `spots n JOIN street_spots ss ON ss.spot_uuid=n.uuid
|
||||
JOIN zones st ON st.uuid=ss.street_uuid AND st.domain_uuid=n.domain_uuid AND st.kind='street' AND st.state!='archived'
|
||||
JOIN zones q ON q.uuid=st.parent_uuid AND q.domain_uuid=st.domain_uuid AND q.kind='quartier' AND q.state!='archived'`
|
||||
description = "n.summary"
|
||||
parentColumns = "substr(COALESCE(NULLIF(q.title,''),q.name),1,256),substr(COALESCE(NULLIF(st.title,''),st.name),1,256),q.path_prefix,st.path_prefix,q.name"
|
||||
where = citySearchAbsolutePathSQL("q.path_prefix") + " AND " + citySearchAbsolutePathSQL("st.path_prefix")
|
||||
where = absolute("q.path_prefix") + " AND " + absolute("st.path_prefix")
|
||||
}
|
||||
nameMatch := `(:query!='' AND (instr(lower(n.name),lower(:query))>0 OR instr(lower(n.title),lower(:query))>0))`
|
||||
pathMatch := `(:query!='' AND instr(lower(n.path_prefix),lower(:query))>0)`
|
||||
descriptionMatch := `(:query!='' AND instr(lower(` + description + `),lower(:query))>0)`
|
||||
contains := func(column string) string { return position + "(lower(" + column + "),lower(" + query + "))" }
|
||||
nameMatch := `(` + query + `!='' AND (` + contains("n.name") + `>0 OR ` + contains("n.title") + `>0))`
|
||||
pathMatch := `(` + query + `!='' AND ` + contains("n.path_prefix") + `>0)`
|
||||
descriptionMatch := `(` + query + `!='' AND ` + contains(description) + `>0)`
|
||||
return `SELECT n.uuid,substr(n.name,1,256),substr(n.title,1,256),
|
||||
substr(` + description + `,max(1,instr(lower(` + description + `),lower(:query))-160),512),
|
||||
substr(` + description + `,` + greatest + `(1,` + contains(description) + `-160),512),
|
||||
n.path_prefix,n.created_at_ms,n.updated_at_ms,` + nameMatch + `,` + pathMatch + `,` + descriptionMatch + `,` + parentColumns + `,` + navigationPath + `,
|
||||
EXISTS(SELECT 1 FROM paths cp WHERE cp.domain_uuid=n.domain_uuid AND cp.path=(` + navigationPath + `))
|
||||
FROM ` + from + ` WHERE n.domain_uuid=:domain AND n.state!='archived' AND ` + where + `
|
||||
AND ` + citySearchAbsolutePathSQL("n.path_prefix") + `
|
||||
AND (n.path_prefix COLLATE BINARY=:scope OR (n.path_prefix COLLATE BINARY>=:prefix AND n.path_prefix COLLATE BINARY<:upper))
|
||||
AND (:query='' OR ` + nameMatch + ` OR ` + pathMatch + ` OR ` + descriptionMatch + `)` + dateWhere + `
|
||||
ORDER BY ` + nameMatch + ` DESC,` + pathMatch + ` DESC,n.path_prefix COLLATE BINARY,n.uuid LIMIT :limit`
|
||||
FROM ` + from + ` WHERE n.domain_uuid=` + domain + ` AND n.state!='archived' AND ` + where + `
|
||||
AND ` + absolute("n.path_prefix") + `
|
||||
AND (n.path_prefix` + collation + `=` + scope + ` OR (n.path_prefix` + collation + `>=` + prefix + ` AND n.path_prefix` + collation + `<` + upper + `))
|
||||
AND (` + query + `='' OR ` + nameMatch + ` OR ` + pathMatch + ` OR ` + descriptionMatch + `)` + dateWhere + `
|
||||
ORDER BY ` + nameMatch + ` DESC,` + pathMatch + ` DESC,n.path_prefix` + collation + `,n.uuid LIMIT ` + limit
|
||||
}
|
||||
|
||||
func citySearchAbsolutePathSQL(column string) string {
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package mestionora
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PostgreSQL's initial search profile is metadata-only. It deliberately does
|
||||
// not scan canonical files or the legacy body column to emulate SQLite FTS.
|
||||
// The caller owns the pool and must provision the native metadata schema.
|
||||
type PostgreSQL struct{ db *sql.DB }
|
||||
|
||||
func NewPostgreSQL(db *sql.DB) *PostgreSQL { return &PostgreSQL{db: db} }
|
||||
|
||||
const pgSearchMIME = `lower(trim(split_part(COALESCE(r.content_type,''),';',1)))`
|
||||
|
||||
var pgSearchFileType = buildSearchFileTypeSQL(pgSearchMIME, func(suffixes ...string) string {
|
||||
clauses := make([]string, 0, len(suffixes))
|
||||
for _, suffix := range suffixes {
|
||||
clauses = append(clauses, searchFilenameSQL+fmt.Sprintf(` LIKE '%%.%s'`, suffix))
|
||||
}
|
||||
return "(" + strings.Join(clauses, " OR ") + ")"
|
||||
}, func(pattern string) string {
|
||||
// Only the package-owned MIME glob constants above reach this function.
|
||||
return pgSearchMIME + ` LIKE '` + strings.ReplaceAll(pattern, "*", "%") + `'`
|
||||
})
|
||||
|
||||
func (s *PostgreSQL) SearchPathsInDomainWithOptions(ctx context.Context, domainUUID, scope, query string, limit int, allowed func(string) bool, options SearchOptions) (found []SearchResult, err error) {
|
||||
if err := options.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scope, query = normalizePath(scope), strings.TrimSpace(query)
|
||||
if allowed == nil || query == "" && !options.HasFilters() {
|
||||
return nil, nil
|
||||
}
|
||||
if len(query) > 512 || len(strings.Fields(query)) > 16 {
|
||||
return nil, errors.New("search query is too long")
|
||||
}
|
||||
if limit <= 0 || limit > 200 {
|
||||
limit = 100
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel()
|
||||
defer func() {
|
||||
if ctx.Err() != nil {
|
||||
found, err = nil, ctx.Err()
|
||||
}
|
||||
}()
|
||||
var args []any
|
||||
bind := func(v any) string { args = append(args, v); return fmt.Sprintf("$%d", len(args)) }
|
||||
prefix := strings.TrimRight(scope, "/") + "/"
|
||||
upper := prefix[:len(prefix)-1] + "0"
|
||||
predicate := "p.domain_uuid=" + bind(domainUUID) + " AND (p.path=" + bind(scope) + " OR (p.path>=" + bind(prefix) + " AND p.path<" + bind(upper) + "))"
|
||||
if query != "" {
|
||||
q := bind(query)
|
||||
predicate += " AND (strpos(lower(p.path),lower(" + q + "))>0 OR strpos(lower(COALESCE(r.filename,'')),lower(" + q + "))>0)"
|
||||
}
|
||||
date := "r.created_at_ms"
|
||||
if options.DateField == "created" {
|
||||
date = "(SELECT o.created_at*1000 FROM objects o WHERE o.uuid=p.object_uuid)"
|
||||
}
|
||||
if options.FromMS != nil {
|
||||
predicate += " AND " + date + ">=" + bind(*options.FromMS)
|
||||
}
|
||||
if options.BeforeMS != nil {
|
||||
predicate += " AND " + date + "<" + bind(*options.BeforeMS)
|
||||
}
|
||||
if options.Extension != "" {
|
||||
ext := bind("." + options.Extension)
|
||||
predicate += " AND right(" + searchFilenameSQL + ",length(" + ext + "))=" + ext
|
||||
}
|
||||
if options.FileType != "" {
|
||||
predicate += " AND (" + pgSearchFileType + ")=" + bind(options.FileType)
|
||||
}
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT p.path,COALESCE(r.content_type,''),COALESCE(r.filename,''),COALESCE(r.document_hash,''),COALESCE(r.uuid,''),COALESCE(r.created_at_ms/1000,0)
|
||||
FROM paths p LEFT JOIN LATERAL (SELECT uuid,content_type,filename,document_hash,created_at_ms FROM revisions WHERE object_uuid=p.object_uuid ORDER BY created_at_ms DESC,uuid DESC LIMIT 1) r ON true
|
||||
WHERE `+predicate+` ORDER BY p.path LIMIT `+bind(SearchCandidateLimit), args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
candidates := make([]SearchResult, 0)
|
||||
for rows.Next() {
|
||||
var result SearchResult
|
||||
if err = rows.Scan(&result.Path, &result.ContentType, &result.Filename, &result.DocumentHash, &result.RevisionUUID, &result.UpdatedAt); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
result.PathMatch = query != "" && strings.Contains(strings.ToLower(result.Path), strings.ToLower(query))
|
||||
result.FilenameMatch = query != "" && strings.Contains(strings.ToLower(result.Filename), strings.ToLower(query))
|
||||
candidates = append(candidates, result)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Release the cursor before policy callbacks: authorization may need the
|
||||
// same single-connection pool. Never return a denied Path or load its body.
|
||||
sort.Slice(candidates, func(i, j int) bool {
|
||||
if len(candidates[i].Path) != len(candidates[j].Path) {
|
||||
return len(candidates[i].Path) < len(candidates[j].Path)
|
||||
}
|
||||
return candidates[i].Path < candidates[j].Path
|
||||
})
|
||||
for _, candidate := range candidates {
|
||||
if err = ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if allowed(candidate.Path) {
|
||||
found = append(found, candidate)
|
||||
if len(found) == limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQL) SearchPathSuggestionsInDomain(ctx context.Context, domainUUID, scope, query string, limit int, allowed func(string) bool) ([]PathSuggestion, error) {
|
||||
results, err := s.SearchPathsInDomainWithOptions(ctx, domainUUID, scope, query, limit, allowed, SearchOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]PathSuggestion, 0, len(results))
|
||||
for _, result := range results {
|
||||
out = append(out, PathSuggestion{SearchResult: result})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -71,18 +71,25 @@ func searchSuffixesSQL(suffixes ...string) string {
|
||||
// known suffix (e.g. a Python source uploaded as text/plain); unrecognized,
|
||||
// specific MIME types stay Other instead of pretending to be a different type.
|
||||
// This uses metadata only and never hydrates a Document to sniff its bytes.
|
||||
var searchFileTypeSQL = `CASE
|
||||
var searchFileTypeSQL = buildSearchFileTypeSQL(searchMIMESQL, searchSuffixesSQL, func(pattern string) string {
|
||||
return searchMIMESQL + ` GLOB '` + pattern + `'`
|
||||
})
|
||||
|
||||
// All patterns are package-owned constants. The dialect-specific builders do
|
||||
// not translate incoming SQL or interpolate user input.
|
||||
func buildSearchFileTypeSQL(searchMIMESQL string, searchSuffixesSQL func(...string) string, mimeGlob func(string) string) string {
|
||||
return `CASE
|
||||
WHEN ` + searchMIMESQL + ` IN ('text/html','application/xhtml+xml') THEN 'html'
|
||||
WHEN ` + searchMIMESQL + `='application/pdf' THEN 'pdf'
|
||||
WHEN ` + searchMIMESQL + ` LIKE 'image/%' THEN 'image'
|
||||
WHEN ` + searchMIMESQL + ` LIKE 'audio/%' THEN 'audio'
|
||||
WHEN ` + searchMIMESQL + ` LIKE 'video/%' THEN 'video'
|
||||
WHEN ` + searchMIMESQL + ` IN ('text/csv','application/csv','text/tab-separated-values') THEN 'csv'
|
||||
WHEN ` + searchMIMESQL + ` IN ('application/json','text/json') OR ` + searchMIMESQL + ` GLOB '*+json' THEN 'json'
|
||||
WHEN ` + searchMIMESQL + ` IN ('application/json','text/json') OR ` + mimeGlob("*+json") + ` THEN 'json'
|
||||
WHEN ` + searchMIMESQL + ` IN ('application/zip','application/x-zip-compressed','application/x-7z-compressed','application/vnd.rar','application/x-rar','application/x-rar-compressed','application/x-tar','application/gzip','application/x-gzip','application/x-xz','application/x-bzip2','application/zstd','application/x-zstd','application/x-compress','application/x-cpio') THEN 'archive'
|
||||
WHEN ` + searchMIMESQL + ` IN ('application/msword','application/vnd.ms-excel','application/vnd.ms-powerpoint','application/rtf','text/rtf') OR ` + searchMIMESQL + ` GLOB 'application/vnd.openxmlformats-officedocument.*' OR ` + searchMIMESQL + ` GLOB 'application/vnd.oasis.opendocument.*' OR ` + searchMIMESQL + ` GLOB 'application/vnd.ms-*.macroenabled.*' THEN 'office'
|
||||
WHEN ` + searchMIMESQL + ` IN ('application/msword','application/vnd.ms-excel','application/vnd.ms-powerpoint','application/rtf','text/rtf') OR ` + mimeGlob("application/vnd.openxmlformats-officedocument.*") + ` OR ` + mimeGlob("application/vnd.oasis.opendocument.*") + ` OR ` + mimeGlob("application/vnd.ms-*.macroenabled.*") + ` THEN 'office'
|
||||
WHEN ` + searchMIMESQL + ` IN ('text/markdown','text/x-markdown') THEN 'text'
|
||||
WHEN ` + searchMIMESQL + ` IN ('application/javascript','application/x-javascript','text/javascript','application/typescript','text/typescript','application/xml','text/xml','application/yaml','application/x-yaml','text/yaml','application/toml','application/x-sh','application/x-shellscript','application/x-python','application/x-python-code','application/x-httpd-php','application/sql','text/css') OR ` + searchMIMESQL + ` GLOB 'text/x-*' OR ` + searchMIMESQL + ` GLOB '*+xml' THEN 'code'
|
||||
WHEN ` + searchMIMESQL + ` IN ('application/javascript','application/x-javascript','text/javascript','application/typescript','text/typescript','application/xml','text/xml','application/yaml','application/x-yaml','text/yaml','application/toml','application/x-sh','application/x-shellscript','application/x-python','application/x-python-code','application/x-httpd-php','application/sql','text/css') OR ` + mimeGlob("text/x-*") + ` OR ` + mimeGlob("*+xml") + ` THEN 'code'
|
||||
WHEN ` + searchMIMESQL + ` NOT IN ('','text/plain','application/octet-stream','binary/octet-stream','application/x-empty') THEN CASE WHEN ` + searchMIMESQL + ` LIKE 'text/%' THEN 'text' ELSE 'other' END
|
||||
WHEN ` + searchSuffixesSQL("html", "htm", "xhtml") + ` THEN 'html'
|
||||
WHEN ` + searchSuffixesSQL("pdf") + ` THEN 'pdf'
|
||||
@@ -96,6 +103,7 @@ var searchFileTypeSQL = `CASE
|
||||
WHEN ` + searchSuffixesSQL("py", "pyw", "sh", "bash", "zsh", "fish", "ps1", "bat", "cmd", "js", "mjs", "cjs", "jsx", "ts", "tsx", "go", "rs", "c", "h", "cpp", "hpp", "cc", "cs", "java", "kt", "kts", "swift", "rb", "php", "pl", "pm", "lua", "r", "sql", "css", "scss", "sass", "less", "xml", "xsl", "yaml", "yml", "toml", "ini", "conf", "vue", "svelte", "dart", "ex", "exs", "erl", "hs", "scala", "clj", "lisp", "el", "vim", "wasm") + ` THEN 'code'
|
||||
WHEN ` + searchMIMESQL + `='text/plain' OR ` + searchSuffixesSQL("txt", "text", "log", "md", "markdown", "rst", "adoc", "org") + ` THEN 'text'
|
||||
ELSE 'other' END`
|
||||
}
|
||||
|
||||
// The returned predicate is inserted before each candidate stream's LIMIT,
|
||||
// including FTS-first streams. Filtering after their cap silently loses hits.
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Asset struct {
|
||||
DomainUUID string
|
||||
Address string
|
||||
ObjectUUID string
|
||||
HeadDocumentHash string
|
||||
ContentType string
|
||||
Filename string
|
||||
SizeBytes int64
|
||||
CreatedAtMS int64
|
||||
UpdatedAtMS int64
|
||||
}
|
||||
|
||||
const assetColumns = `domain_uuid,address,object_uuid,head_document_hash,content_type,filename,size_bytes,created_at_ms,updated_at_ms`
|
||||
|
||||
func scanAsset(row interface{ Scan(...any) error }) (Asset, error) {
|
||||
var a Asset
|
||||
err := row.Scan(&a.DomainUUID, &a.Address, &a.ObjectUUID, &a.HeadDocumentHash, &a.ContentType, &a.Filename, &a.SizeBytes, &a.CreatedAtMS, &a.UpdatedAtMS)
|
||||
return a, err
|
||||
}
|
||||
|
||||
func (r *Repository) GetAsset(ctx context.Context, domain, address string) (Asset, error) {
|
||||
a, err := scanAsset(r.database.QueryRowContext(ctx, `SELECT `+assetColumns+` FROM assets WHERE domain_uuid=`+r.p(1)+` AND address=`+r.p(2), domain, address))
|
||||
return a, r.failure(err)
|
||||
}
|
||||
|
||||
// PutAsset publishes an already-durable Document hash and appends its audit
|
||||
// history in the same transaction. Empty expected is create-only; overwrites
|
||||
// compare the previous hash and keep the asset's object UUID stable.
|
||||
func (r *Repository) PutAsset(ctx context.Context, a Asset, expected, actor string) (Asset, error) {
|
||||
a.Address = strings.TrimSpace(a.Address)
|
||||
parts := strings.SplitN(a.Address, ":", 3)
|
||||
if a.DomainUUID == "" || len(parts) != 3 || parts[0] != "asset" || strings.TrimSpace(parts[1]) == "" || strings.TrimSpace(parts[2]) == "" || strings.ContainsAny(a.Address, "\x00\r\n") || !validHash(a.HeadDocumentHash) || strings.TrimSpace(a.ContentType) == "" || a.SizeBytes < 0 {
|
||||
return Asset{}, ErrInvalid
|
||||
}
|
||||
err := r.write(ctx, "domain/"+a.DomainUUID, func(tx *sql.Tx) error {
|
||||
old, err := scanAsset(tx.QueryRowContext(ctx, `SELECT `+assetColumns+` FROM assets WHERE domain_uuid=`+r.p(1)+` AND address=`+r.p(2), a.DomainUUID, a.Address))
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return r.failure(err)
|
||||
}
|
||||
if old.HeadDocumentHash != expected {
|
||||
return ErrConflict
|
||||
}
|
||||
if r.applicationNamespace && err == nil && old.HeadDocumentHash == a.HeadDocumentHash && old.ContentType == a.ContentType && old.Filename == a.Filename && old.SizeBytes == a.SizeBytes {
|
||||
a = old
|
||||
return nil
|
||||
}
|
||||
a.UpdatedAtMS = time.Now().UnixMilli()
|
||||
if err == nil {
|
||||
a.ObjectUUID = old.ObjectUUID
|
||||
a.CreatedAtMS = old.CreatedAtMS
|
||||
if a.UpdatedAtMS <= old.UpdatedAtMS {
|
||||
a.UpdatedAtMS = old.UpdatedAtMS + 1
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `UPDATE assets SET head_document_hash=`+r.p(1)+`,content_type=`+r.p(2)+`,filename=`+r.p(3)+`,size_bytes=`+r.p(4)+`,updated_at_ms=`+r.p(5)+` WHERE domain_uuid=`+r.p(6)+` AND address=`+r.p(7), a.HeadDocumentHash, a.ContentType, a.Filename, a.SizeBytes, a.UpdatedAtMS, a.DomainUUID, a.Address)
|
||||
} else {
|
||||
a.ObjectUUID = uuid.NewString()
|
||||
a.CreatedAtMS = a.UpdatedAtMS
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO objects(uuid,kind,created_at) VALUES (`+r.p(1)+`,'asset',`+r.p(2)+`)`, a.ObjectUUID, a.CreatedAtMS/1000); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO object_origins(object_uuid,domain_uuid,created_at) VALUES (`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`)`, a.ObjectUUID, a.DomainUUID, a.CreatedAtMS/1000); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO assets (`+assetColumns+`) VALUES (`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`,`+r.p(4)+`,`+r.p(5)+`,`+r.p(6)+`,`+r.p(7)+`,`+r.p(8)+`,`+r.p(9)+`)`, a.DomainUUID, a.Address, a.ObjectUUID, a.HeadDocumentHash, a.ContentType, a.Filename, a.SizeBytes, a.CreatedAtMS, a.UpdatedAtMS)
|
||||
}
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO asset_head_history(domain_uuid,address,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms) VALUES (`+r.p(1)+`,`+r.p(2)+`,'put',`+r.p(3)+`,`+r.p(4)+`,`+r.p(5)+`,`+r.p(6)+`,`+r.p(7)+`,`+r.p(8)+`)`, a.DomainUUID, a.Address, a.HeadDocumentHash, a.ContentType, a.Filename, a.SizeBytes, actor, a.UpdatedAtMS)
|
||||
return r.failure(err)
|
||||
})
|
||||
if err != nil {
|
||||
return Asset{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
|
||||
"pageswiki/internal/documentrefs"
|
||||
"pageswiki/internal/geduldh"
|
||||
)
|
||||
|
||||
type SavedDocument struct {
|
||||
Revision Revision
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
// RestoreDocument preserves the historical immutable embedded references; it
|
||||
// must not parse Asset provenance and rebind it to today's mutable Asset HEAD.
|
||||
func (r *Repository) RestoreDocument(ctx context.Context, documents geduldh.Store, domain, revision, expected, message, actor string) (Revision, error) {
|
||||
if documents == nil {
|
||||
return Revision{}, ErrInvalid
|
||||
}
|
||||
v, err := r.GetRevision(ctx, domain, revision)
|
||||
if err != nil {
|
||||
return Revision{}, err
|
||||
}
|
||||
refs, err := r.revisionReferences(ctx, domain, revision)
|
||||
if err != nil {
|
||||
return Revision{}, err
|
||||
}
|
||||
for _, hash := range append([]string{v.DocumentHash}, refs...) {
|
||||
if err := documents.Verify(ctx, hash); err != nil {
|
||||
return Revision{}, fmt.Errorf("verify restored Document: %w", err)
|
||||
}
|
||||
}
|
||||
v.Message = message
|
||||
return r.appendRevision(ctx, domain, v, expected, refs, actor)
|
||||
}
|
||||
|
||||
// SaveDocument freezes Domain-local Asset HEADs, verifies embedded payloads,
|
||||
// persists the immutable Document, then atomically publishes Revision, refs and
|
||||
// outbox. No SQL transaction/lock is held during filesystem or remote I/O.
|
||||
// A failed metadata commit may leave an unreferenced immutable blob; it must
|
||||
// never delete that blob (another writer may already reference the same hash).
|
||||
// Concurrent GC needs a separate pin protocol before this pipeline is enabled
|
||||
// in a deployment with background deletion. This is not an authorization API.
|
||||
func (r *Repository) SaveDocument(ctx context.Context, documents geduldh.Store, domain string, v Revision, payload []byte, expected, actor string) (SavedDocument, error) {
|
||||
return r.saveDocument(ctx, documents, domain, v, payload, &expected, actor)
|
||||
}
|
||||
|
||||
// SaveDocumentUnconditionally preserves append-without-CAS semantics for import
|
||||
// and legacy clients. Concurrent saves still append immutable revisions with
|
||||
// monotonic timestamps; it must not be used to silently retry a failed CAS.
|
||||
func (r *Repository) SaveDocumentUnconditionally(ctx context.Context, documents geduldh.Store, domain string, v Revision, payload []byte, actor string) (SavedDocument, error) {
|
||||
return r.saveDocument(ctx, documents, domain, v, payload, nil, actor)
|
||||
}
|
||||
|
||||
func (r *Repository) saveDocument(ctx context.Context, documents geduldh.Store, domain string, v Revision, payload []byte, expected *string, actor string) (SavedDocument, error) {
|
||||
if documents == nil {
|
||||
return SavedDocument{}, ErrInvalid
|
||||
}
|
||||
lang, err := normalizedLanguage(v.Language)
|
||||
if err != nil {
|
||||
return SavedDocument{}, err
|
||||
}
|
||||
v.Language = lang
|
||||
if _, _, err := mime.ParseMediaType(v.ContentType); err != nil {
|
||||
return SavedDocument{}, ErrInvalid
|
||||
}
|
||||
var bound bool
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM objects o WHERE o.uuid=`+r.p(1)+` AND (`+r.objectDomainScope("o.uuid", r.p(2))+`))`, v.ObjectUUID, domain).Scan(&bound); err != nil {
|
||||
return SavedDocument{}, r.failure(err)
|
||||
}
|
||||
if !bound {
|
||||
return SavedDocument{}, ErrNotFound
|
||||
}
|
||||
// Fail stale saves before expensive payload work; repeat CAS in the commit
|
||||
// transaction because a different writer may commit after this preflight.
|
||||
head, err := r.Head(ctx, domain, v.ObjectUUID, v.Language)
|
||||
if err != nil && !errors.Is(err, ErrNotFound) {
|
||||
return SavedDocument{}, err
|
||||
}
|
||||
if expected != nil && head.UUID != *expected {
|
||||
return SavedDocument{}, ErrConflict
|
||||
}
|
||||
frozen, refs, err := documentrefs.Freeze(ctx, payload, v.ContentType, func(ctx context.Context, address string) (string, error) {
|
||||
a, err := r.GetAsset(ctx, domain, address)
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
return "", documentrefs.ErrUnavailable
|
||||
}
|
||||
return a.HeadDocumentHash, err
|
||||
})
|
||||
if err != nil {
|
||||
return SavedDocument{}, err
|
||||
}
|
||||
for _, hash := range refs {
|
||||
if err := documents.Verify(ctx, hash); err != nil {
|
||||
return SavedDocument{}, fmt.Errorf("verify embedded Document: %w", err)
|
||||
}
|
||||
}
|
||||
sum := sha256.Sum256(frozen)
|
||||
v.DocumentHash = hex.EncodeToString(sum[:])
|
||||
if err := documents.PutIfAbsent(ctx, v.DocumentHash, int64(len(frozen)), bytes.NewReader(frozen)); err != nil {
|
||||
return SavedDocument{}, fmt.Errorf("persist Document: %w", err)
|
||||
}
|
||||
created, err := r.appendRevisionExpected(ctx, domain, v, expected, refs, actor)
|
||||
if err != nil {
|
||||
return SavedDocument{}, err
|
||||
}
|
||||
return SavedDocument{Revision: created, Payload: frozen}, nil
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/documentrefs"
|
||||
"pageswiki/internal/geduldh"
|
||||
)
|
||||
|
||||
type faultDocuments struct {
|
||||
geduldh.Store
|
||||
putErr error
|
||||
verifyErr error
|
||||
afterPut func()
|
||||
}
|
||||
|
||||
func (f faultDocuments) PutIfAbsent(ctx context.Context, h string, n int64, reader io.Reader) error {
|
||||
if f.putErr != nil {
|
||||
return f.putErr
|
||||
}
|
||||
if err := f.Store.PutIfAbsent(ctx, h, n, reader); err != nil {
|
||||
return err
|
||||
}
|
||||
if f.afterPut != nil {
|
||||
f.afterPut()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (f faultDocuments) Verify(ctx context.Context, h string) error {
|
||||
if f.verifyErr != nil {
|
||||
return f.verifyErr
|
||||
}
|
||||
return f.Store.Verify(ctx, h)
|
||||
}
|
||||
|
||||
func documentFixture(t *testing.T, r *Repository) (geduldh.Store, string, Revision) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
documents, err := geduldh.NewLocal(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
content := []byte("synthetic image")
|
||||
h := hash(string(content))
|
||||
if err := documents.PutIfAbsent(ctx, h, int64(len(content)), bytes.NewReader(content)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.PutAsset(ctx, Asset{DomainUUID: "test", Address: "asset:image:cover", HeadDocumentHash: h, ContentType: "image/png", SizeBytes: int64(len(content))}, "", "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
object, err := r.CreatePage(ctx, "test", "/guide")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return documents, h, Revision{ObjectUUID: object, Language: "ja", ContentType: "text/html; charset=utf-8", Message: "synthetic save"}
|
||||
}
|
||||
|
||||
func TestDocumentSaveReferencesOutboxAndRestore(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx := context.Background()
|
||||
docs, assetHash, v := documentFixture(t, r)
|
||||
body := []byte(`<h1>Guide</h1><img src="asset:image:cover"><img src="asset:image:cover">`)
|
||||
if _, err := r.PutAsset(ctx, Asset{DomainUUID: "private-domain", Address: "asset:image:private", HeadDocumentHash: assetHash, ContentType: "image/png", SizeBytes: 15}, "", "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.SaveDocument(ctx, docs, "test", v, []byte(`<img src="asset:image:private">`), "", "actor"); !errors.Is(err, documentrefs.ErrUnavailable) {
|
||||
t.Fatal("foreign Asset resolved", err)
|
||||
}
|
||||
first, err := r.SaveDocument(ctx, docs, "test", v, body, "", "actor")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(first.Payload), "/_/obj/sha256:"+assetHash) {
|
||||
t.Fatal("mutable URL not frozen")
|
||||
}
|
||||
if err := docs.Verify(ctx, first.Revision.DocumentHash); err != nil {
|
||||
t.Fatal("metadata published without durable Document", err)
|
||||
}
|
||||
refs, err := r.revisionReferences(ctx, "test", first.Revision.UUID)
|
||||
if err != nil || len(refs) != 1 || refs[0] != assetHash {
|
||||
t.Fatal("embedded reference", err, refs)
|
||||
}
|
||||
var eventDomain, target, payload, actor string
|
||||
var sequence int64
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT domain_uuid,target_uuid,payload,actor_uuid,sequence FROM activity_outbox`).Scan(&eventDomain, &target, &payload, &actor, &sequence); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var envelope map[string]string
|
||||
if err := json.Unmarshal([]byte(payload), &envelope); err != nil || eventDomain != "test" || actor != "actor" || sequence != 1 || target != first.Revision.UUID || envelope["document_hash"] != first.Revision.DocumentHash {
|
||||
t.Fatal("outbox envelope mismatch", err)
|
||||
}
|
||||
newContent := []byte("next synthetic image")
|
||||
newHash := hash(string(newContent))
|
||||
if err := docs.PutIfAbsent(ctx, newHash, int64(len(newContent)), bytes.NewReader(newContent)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.PutAsset(ctx, Asset{DomainUUID: "test", Address: "asset:image:cover", HeadDocumentHash: newHash, ContentType: "image/png", SizeBytes: int64(len(newContent))}, assetHash, "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := r.SaveDocument(ctx, docs, "test", v, first.Payload, first.Revision.UUID, "actor")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(second.Payload), newHash) {
|
||||
t.Fatal("provenance not refreshed")
|
||||
}
|
||||
restored, err := r.RestoreRevision(ctx, "test", first.Revision.UUID, second.Revision.UUID, "restore")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
refs, err = r.revisionReferences(ctx, "test", restored.UUID)
|
||||
if err != nil || len(refs) != 1 || refs[0] != assetHash || restored.DocumentHash != first.Revision.DocumentHash {
|
||||
t.Fatal("restore followed new Asset HEAD instead of historical refs", err)
|
||||
}
|
||||
var count int
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM activity_outbox`).Scan(&count); err != nil || count != 3 {
|
||||
t.Fatal("save/restore event count", count, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentSaveFailureIsAtomic(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
for _, stage := range []string{"references", "outbox"} {
|
||||
t.Run(string(backend)+"/"+stage, func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx := context.Background()
|
||||
docs, _, v := documentFixture(t, r)
|
||||
body := []byte(`<img src="asset:image:cover">`)
|
||||
first, err := r.SaveDocument(ctx, docs, "test", v, body, "", "actor")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl := `CREATE UNIQUE INDEX synthetic_save_failure ON revision_document_refs(document_hash)`
|
||||
if stage == "outbox" {
|
||||
ddl = `CREATE UNIQUE INDEX synthetic_save_failure ON activity_outbox(domain_uuid)`
|
||||
}
|
||||
if _, err := r.database.ExecContext(ctx, ddl); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.SaveDocument(ctx, docs, "test", v, append([]byte(`<p>change</p>`), body...), first.Revision.UUID, "actor"); !errors.Is(err, ErrConflict) {
|
||||
t.Fatal("injected error ignored", err)
|
||||
}
|
||||
for _, table := range []string{"revisions", "revision_document_refs", "activity_outbox"} {
|
||||
var n int
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM `+table).Scan(&n); err != nil || n != 1 {
|
||||
t.Fatal("partial commit", table, n, err)
|
||||
}
|
||||
}
|
||||
var last int64
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT last_sequence FROM activity_outbox_heads WHERE domain_uuid='test'`).Scan(&last); err != nil || last != 1 {
|
||||
t.Fatal("sequence advanced on failure", err)
|
||||
}
|
||||
if _, err := r.database.ExecContext(ctx, `DROP INDEX synthetic_save_failure`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.SaveDocument(ctx, docs, "test", v, body, first.Revision.UUID, "actor"); err != nil {
|
||||
t.Fatal("retry after rollback", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDocumentSavePersistenceFailureAndConcurrentEdit(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
docs, _, v := documentFixture(t, r)
|
||||
body := []byte(`<img src="asset:image:cover">`)
|
||||
for _, failing := range []faultDocuments{{Store: docs, putErr: io.ErrClosedPipe}, {Store: docs, verifyErr: geduldh.ErrHashMismatch}} {
|
||||
if _, err := r.SaveDocument(ctx, failing, "test", v, body, "", "actor"); err == nil {
|
||||
t.Fatal("Document failure ignored")
|
||||
}
|
||||
var n int
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM revisions`).Scan(&n); err != nil || n != 0 {
|
||||
t.Fatal("failed persistence published revision", err)
|
||||
}
|
||||
}
|
||||
first, err := r.SaveDocument(ctx, docs, "test", v, body, "", "actor")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The callback writes using a different transaction during Document I/O.
|
||||
// It would deadlock if SaveDocument held SQL locks over the payload write.
|
||||
race := faultDocuments{Store: docs, afterPut: func() {
|
||||
peer, _ := New(r.database, backend)
|
||||
competing := v
|
||||
competing.DocumentHash = first.Revision.DocumentHash
|
||||
if _, err := peer.AppendRevision(ctx, "test", competing, first.Revision.UUID); err != nil {
|
||||
t.Fatal("I/O held metadata lock", err)
|
||||
}
|
||||
}}
|
||||
if _, err := r.SaveDocument(ctx, race, "test", v, []byte("<p>stale</p>"), first.Revision.UUID, "actor"); !errors.Is(err, ErrConflict) {
|
||||
t.Fatal("CAS not repeated after payload I/O", err)
|
||||
}
|
||||
var n int
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM activity_outbox`).Scan(&n); err != nil || n != 2 {
|
||||
t.Fatal("conflicting write emitted event", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestOutboxSequenceForConcurrentObjects(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
objects := make([]string, 6)
|
||||
for i := range objects {
|
||||
var err error
|
||||
objects[i], err = r.CreatePage(ctx, "test", "/"+strings.Repeat("a", i+1))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
errs := make(chan error, len(objects))
|
||||
for _, id := range objects {
|
||||
wg.Add(1)
|
||||
go func(id string) {
|
||||
defer wg.Done()
|
||||
peer, _ := New(r.database, backend)
|
||||
_, err := peer.AppendRevision(ctx, "test", Revision{ObjectUUID: id, DocumentHash: hash(id), ContentType: "text/html"}, "")
|
||||
errs <- err
|
||||
}(id)
|
||||
}
|
||||
wg.Wait()
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var count int
|
||||
var min, max int64
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*),min(sequence),max(sequence) FROM activity_outbox`).Scan(&count, &min, &max); err != nil || count != 6 || min != 1 || max != 6 {
|
||||
t.Fatal("invalid concurrent sequence", count, min, max, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A core-compatible schema is a rehearsal scope, not an application readiness
|
||||
// claim. New application tables and security triggers always block core-only
|
||||
// copying; unknown data must never be silently dropped as a "cache".
|
||||
type PortabilityReport struct {
|
||||
Scope string `json:"scope"`
|
||||
ApplicationReady bool `json:"application_ready"`
|
||||
CoreCompatible bool `json:"core_compatible"`
|
||||
Tables []TablePortability `json:"tables"`
|
||||
Blockers []string `json:"blockers"`
|
||||
}
|
||||
type TablePortability struct {
|
||||
Name string `json:"name"`
|
||||
Component string `json:"component"`
|
||||
State string `json:"state"`
|
||||
}
|
||||
type tableSpec struct {
|
||||
name string
|
||||
columns []string
|
||||
keys []string
|
||||
identity bool
|
||||
}
|
||||
|
||||
// Explicit columns and primary-key order define an unambiguous streaming
|
||||
// fingerprint and an FK-safe copy order. Migration ledgers stay backend-local.
|
||||
var coreTables = []tableSpec{
|
||||
{"objects", strings.Split("uuid,kind,created_at", ","), []string{"uuid"}, false},
|
||||
{"object_origins", strings.Split("object_uuid,domain_uuid,created_at", ","), []string{"object_uuid"}, false},
|
||||
{"paths", strings.Split("domain_uuid,path,object_uuid,created_at,is_primary", ","), []string{"domain_uuid", "path"}, false},
|
||||
{"revisions", strings.Split(revisionColumns+",created_at,body", ","), []string{"uuid"}, false},
|
||||
{"assets", strings.Split(assetColumns, ","), []string{"domain_uuid", "address"}, false},
|
||||
{"asset_head_history", strings.Split("id,domain_uuid,address,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms", ","), []string{"id"}, true},
|
||||
{"revision_document_refs", []string{"revision_uuid", "document_hash"}, []string{"revision_uuid", "document_hash"}, false},
|
||||
{"activity_outbox", strings.Split("uuid,domain_uuid,sequence,event_type,actor_uuid,target_kind,target_uuid,payload,idempotency_key,status,attempts,next_attempt_ms,last_error,created_at_ms,claimed_at_ms", ","), []string{"uuid"}, false},
|
||||
{"activity_outbox_heads", []string{"domain_uuid", "event_type", "last_sequence"}, []string{"domain_uuid", "event_type"}, false},
|
||||
}
|
||||
|
||||
func componentForTable(name string) string {
|
||||
for _, prefix := range []string{"auth", "principal", "group", "membership", "task", "calendar", "event", "notification", "activity", "federation", "domain", "campus", "config", "import", "resource", "adapter", "user", "profile", "contact", "feed", "periodical", "search", "locality", "city"} {
|
||||
if strings.HasPrefix(name, prefix) {
|
||||
return prefix
|
||||
}
|
||||
}
|
||||
if name == "zones" || name == "spots" || strings.HasPrefix(name, "spot_") || strings.HasPrefix(name, "street_") || name == "topology_events" {
|
||||
return "city"
|
||||
}
|
||||
return "unclassified"
|
||||
}
|
||||
|
||||
func (r *Repository) InspectPortability(ctx context.Context) (PortabilityReport, error) {
|
||||
tx, err := r.database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead})
|
||||
if err != nil {
|
||||
return PortabilityReport{}, r.failure(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
return r.inspectPortability(ctx, tx)
|
||||
}
|
||||
|
||||
func (r *Repository) inspectPortability(ctx context.Context, tx *sql.Tx) (PortabilityReport, error) {
|
||||
report := PortabilityReport{Scope: "portable-core-rehearsal-only", Tables: []TablePortability{}, Blockers: []string{}}
|
||||
known := map[string]tableSpec{}
|
||||
for _, s := range coreTables {
|
||||
known[s.name] = s
|
||||
}
|
||||
query := `SELECT name,type FROM sqlite_master WHERE type IN ('table','view','trigger') AND substr(name,1,7)!='sqlite_' ORDER BY name`
|
||||
if r.backend == PostgreSQL {
|
||||
query = `SELECT c.relname,CASE WHEN c.relkind IN ('r','p') THEN 'table' ELSE 'view' END FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND c.relkind IN ('r','p','v','m','f')
|
||||
UNION ALL SELECT t.tgname,'trigger' FROM pg_trigger t JOIN pg_class c ON c.oid=t.tgrelid JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND NOT t.tgisinternal
|
||||
UNION ALL SELECT p.proname,'routine' FROM pg_proc p JOIN pg_namespace n ON n.oid=p.pronamespace WHERE n.nspname=current_schema()
|
||||
UNION ALL SELECT r.rulename,'rule' FROM pg_rewrite r JOIN pg_class c ON c.oid=r.ev_class JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND r.rulename!='_RETURN'
|
||||
UNION ALL SELECT c.relname,'sequence' FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND c.relkind='S' AND NOT EXISTS(SELECT 1 FROM pg_depend d JOIN pg_class t ON t.oid=d.refobjid JOIN pg_namespace tn ON tn.oid=t.relnamespace WHERE d.objid=c.oid AND d.classid='pg_class'::regclass AND d.refclassid='pg_class'::regclass AND tn.nspname=current_schema() AND t.relname='asset_head_history' AND d.deptype='i') ORDER BY 1`
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, query)
|
||||
if err != nil {
|
||||
return report, r.failure(err)
|
||||
}
|
||||
types := map[string]string{}
|
||||
for rows.Next() {
|
||||
var name, kind string
|
||||
if err := rows.Scan(&name, &kind); err != nil {
|
||||
rows.Close()
|
||||
return report, r.failure(err)
|
||||
}
|
||||
if kind != "table" {
|
||||
report.Blockers = append(report.Blockers, fmt.Sprintf("unported %s: %s", kind, name))
|
||||
continue
|
||||
}
|
||||
types[name] = kind
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return report, r.failure(err)
|
||||
}
|
||||
names := make([]string, 0, len(types))
|
||||
for name := range types {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
for _, name := range names {
|
||||
kind := types[name]
|
||||
if kind != "table" {
|
||||
report.Blockers = append(report.Blockers, fmt.Sprintf("unported %s: %s", kind, name))
|
||||
continue
|
||||
}
|
||||
if name == "schema_migration_steps" {
|
||||
continue
|
||||
}
|
||||
item := TablePortability{Name: name, Component: componentForTable(name), State: "unported"}
|
||||
if _, ok := known[name]; ok {
|
||||
item.Component = "core-metadata"
|
||||
item.State = "shape-pending"
|
||||
} else {
|
||||
report.Blockers = append(report.Blockers, "unported table: "+name)
|
||||
}
|
||||
report.Tables = append(report.Tables, item)
|
||||
}
|
||||
for _, spec := range coreTables {
|
||||
if types[spec.name] != "table" {
|
||||
report.Blockers = append(report.Blockers, "missing core table: "+spec.name)
|
||||
}
|
||||
}
|
||||
if types["schema_migration_steps"] != "table" {
|
||||
report.Blockers = append(report.Blockers, "missing portable migration ledger")
|
||||
} else {
|
||||
if err := r.inspectCoreHistory(ctx, tx, &report); err != nil {
|
||||
return report, err
|
||||
}
|
||||
}
|
||||
for i := range report.Tables {
|
||||
item := &report.Tables[i]
|
||||
spec, ok := known[item.Name]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
columns, err := r.tableColumns(ctx, tx, item.Name)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
actual := map[string]bool{}
|
||||
for _, c := range columns {
|
||||
actual[c] = true
|
||||
}
|
||||
matches := len(actual) == len(spec.columns)
|
||||
for _, c := range spec.columns {
|
||||
matches = matches && actual[c]
|
||||
}
|
||||
if !matches {
|
||||
item.State = "schema-drift"
|
||||
report.Blockers = append(report.Blockers, "column mismatch: "+item.Name)
|
||||
} else {
|
||||
item.State = "core-shape-compatible"
|
||||
}
|
||||
}
|
||||
if err := r.inspectCatalog(ctx, tx, &report); err != nil {
|
||||
return report, err
|
||||
}
|
||||
sort.Strings(report.Blockers)
|
||||
report.CoreCompatible = len(report.Blockers) == 0
|
||||
// This core-only report does not certify full-application readiness. Use
|
||||
// the Store application importer for the production migration inventory.
|
||||
// ApplicationReady intentionally remains false here because non-core
|
||||
// repositories, canonical files and worker state are outside this check.
|
||||
return report, nil
|
||||
}
|
||||
|
||||
func (r *Repository) tableColumns(ctx context.Context, tx *sql.Tx, table string) ([]string, error) {
|
||||
query := `SELECT name FROM pragma_table_info(` + r.p(1) + `) ORDER BY cid`
|
||||
if r.backend == PostgreSQL {
|
||||
query = `SELECT column_name FROM information_schema.columns WHERE table_schema=current_schema() AND table_name=$1 ORDER BY ordinal_position`
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, query, table)
|
||||
if err != nil {
|
||||
return nil, r.failure(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []string
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
return nil, r.failure(err)
|
||||
}
|
||||
out = append(out, name)
|
||||
}
|
||||
return out, r.failure(rows.Err())
|
||||
}
|
||||
|
||||
func (r *Repository) inspectCoreHistory(ctx context.Context, tx *sql.Tx, report *PortabilityReport) error {
|
||||
sqliteSteps, pgSteps := r.migrationDefinitions()
|
||||
checksums := map[int]string{}
|
||||
names := map[int]string{}
|
||||
add := func(version int, name string, step any) {
|
||||
data, _ := json.Marshal(step)
|
||||
sum := sha256.Sum256(data)
|
||||
checksums[version] = hex.EncodeToString(sum[:])
|
||||
names[version] = name
|
||||
}
|
||||
for _, s := range sqliteSteps {
|
||||
add(s.Version, s.Name, s)
|
||||
}
|
||||
for _, s := range pgSteps {
|
||||
add(s.Version, s.Name, s)
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT component,version,name,checksum FROM schema_migration_steps ORDER BY component,version`)
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
applied := 0
|
||||
for rows.Next() {
|
||||
var component, name, checksum string
|
||||
var version int
|
||||
if err := rows.Scan(&component, &version, &name, &checksum); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if component != "core-metadata" {
|
||||
report.Blockers = append(report.Blockers, "unported migration component: "+component)
|
||||
continue
|
||||
}
|
||||
if version != applied+1 || names[version] != name || checksums[version] != checksum {
|
||||
report.Blockers = append(report.Blockers, "portable migration history mismatch")
|
||||
}
|
||||
applied++
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if applied != len(checksums) {
|
||||
report.Blockers = append(report.Blockers, "portable migrations are not current")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,230 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Only the owned portable-core schema is certified here. Existing application
|
||||
// tables are NOT adopted by translating arbitrary SQLite DDL to PostgreSQL.
|
||||
func (r *Repository) inspectCatalog(ctx context.Context, tx *sql.Tx, report *PortabilityReport) error {
|
||||
if r.backend == SQLite {
|
||||
return r.inspectSQLiteCatalog(ctx, tx, report)
|
||||
}
|
||||
return r.inspectPostgreSQLCatalog(ctx, tx, report)
|
||||
}
|
||||
|
||||
func (r *Repository) inspectSQLiteCatalog(ctx context.Context, tx *sql.Tx, report *PortabilityReport) error {
|
||||
steps, _ := r.migrationDefinitions()
|
||||
expected := map[string]string{}
|
||||
for _, step := range steps {
|
||||
for _, statement := range step.Statements {
|
||||
fields := strings.Fields(statement)
|
||||
if len(fields) > 2 && fields[0] == "CREATE" && (fields[1] == "TABLE" || fields[1] == "INDEX") {
|
||||
name := strings.SplitN(fields[2], "(", 2)[0]
|
||||
expected[name] = strings.TrimSpace(statement)
|
||||
}
|
||||
}
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT name,type,sql FROM sqlite_master WHERE type IN ('table','index') AND sql IS NOT NULL AND name!='schema_migration_steps' AND substr(name,1,7)!='sqlite_' ORDER BY name`)
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
seen := map[string]bool{}
|
||||
for rows.Next() {
|
||||
var name, kind, ddl string
|
||||
if err := rows.Scan(&name, &kind, &ddl); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if wanted, ok := expected[name]; ok {
|
||||
seen[name] = true
|
||||
if wanted != strings.TrimSpace(ddl) {
|
||||
report.Blockers = append(report.Blockers, "catalog mismatch: "+name)
|
||||
}
|
||||
} else if kind == "index" {
|
||||
report.Blockers = append(report.Blockers, "unported index: "+name)
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
for name := range expected {
|
||||
if !seen[name] {
|
||||
report.Blockers = append(report.Blockers, "missing catalog object: "+name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func numericColumn(column string) bool {
|
||||
switch column {
|
||||
case "created_at", "created_at_ms", "updated_at_ms", "is_primary", "size_bytes", "id", "sequence", "attempts", "next_attempt_ms", "claimed_at_ms", "last_sequence":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *Repository) inspectPostgreSQLCatalog(ctx context.Context, tx *sql.Tx, report *PortabilityReport) error {
|
||||
var ledgerPolicy bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT COALESCE(bool_or(c.relrowsecurity OR c.relforcerowsecurity OR EXISTS(SELECT 1 FROM pg_policy p WHERE p.polrelid=c.oid)),false) FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND c.relname='schema_migration_steps'`).Scan(&ledgerPolicy); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if ledgerPolicy {
|
||||
report.Blockers = append(report.Blockers, "unported policy: schema_migration_steps")
|
||||
}
|
||||
defaults := map[string]string{
|
||||
"paths.is_primary": "0", "revisions.filename": "''::text", "revisions.message": "''::text", "revisions.body": "''::text", "assets.filename": "''::text",
|
||||
"activity_outbox.actor_uuid": "''::text", "activity_outbox.target_kind": "''::text", "activity_outbox.target_uuid": "''::text", "activity_outbox.payload": "''::text", "activity_outbox.status": "'pending'::text", "activity_outbox.attempts": "0", "activity_outbox.last_error": "''::text", "activity_outbox.claimed_at_ms": "0",
|
||||
}
|
||||
constraints := map[string][]string{
|
||||
"objects": {"PRIMARY KEY (uuid)"},
|
||||
"object_origins": {"PRIMARY KEY (object_uuid)", "FOREIGN KEY (object_uuid) REFERENCES objects(uuid)"},
|
||||
"paths": {"PRIMARY KEY (domain_uuid, path)", "FOREIGN KEY (object_uuid) REFERENCES objects(uuid)"},
|
||||
"revisions": {"PRIMARY KEY (uuid)", "FOREIGN KEY (object_uuid) REFERENCES objects(uuid)"},
|
||||
"assets": {"PRIMARY KEY (domain_uuid, address)", "UNIQUE (object_uuid)", "FOREIGN KEY (object_uuid) REFERENCES objects(uuid)", "CHECK ((size_bytes >= 0))"},
|
||||
"asset_head_history": {"PRIMARY KEY (id)", "CHECK ((size_bytes >= 0))"},
|
||||
"revision_document_refs": {"PRIMARY KEY (revision_uuid, document_hash)", "FOREIGN KEY (revision_uuid) REFERENCES revisions(uuid)"},
|
||||
"activity_outbox": {"PRIMARY KEY (uuid)", "UNIQUE (domain_uuid, sequence)", "UNIQUE (domain_uuid, idempotency_key)"},
|
||||
"activity_outbox_heads": {"PRIMARY KEY (domain_uuid, event_type)"},
|
||||
}
|
||||
identityTablePresent := false
|
||||
for _, spec := range coreTables {
|
||||
var ordinary bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT c.relkind='r' AND c.relpersistence='p' AND NOT c.relrowsecurity AND NOT c.relforcerowsecurity AND NOT EXISTS(SELECT 1 FROM pg_policy p WHERE p.polrelid=c.oid) AND NOT EXISTS(SELECT 1 FROM pg_inherits x WHERE x.inhrelid=c.oid OR x.inhparent=c.oid) FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND c.relname=$1`, spec.name).Scan(&ordinary); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
report.Blockers = append(report.Blockers, "missing table: "+spec.name)
|
||||
continue
|
||||
}
|
||||
return r.failure(err)
|
||||
}
|
||||
if !ordinary {
|
||||
report.Blockers = append(report.Blockers, "unported policy or table mode: "+spec.name)
|
||||
}
|
||||
if spec.identity {
|
||||
identityTablePresent = true
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT a.attname,format_type(a.atttypid,a.atttypmod),a.attnotnull,COALESCE(pg_get_expr(d.adbin,d.adrelid),''),a.attidentity,a.attgenerated,COALESCE(col.collname,'') FROM pg_attribute a JOIN pg_class c ON c.oid=a.attrelid JOIN pg_namespace n ON n.oid=c.relnamespace LEFT JOIN pg_attrdef d ON d.adrelid=a.attrelid AND d.adnum=a.attnum LEFT JOIN pg_collation col ON col.oid=a.attcollation WHERE n.nspname=current_schema() AND c.relname=$1 AND a.attnum>0 AND NOT a.attisdropped`, spec.name)
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
for rows.Next() {
|
||||
var name, kind, def, identity, generated, collation string
|
||||
var required bool
|
||||
if err := rows.Scan(&name, &kind, &required, &def, &identity, &generated, &collation); err != nil {
|
||||
rows.Close()
|
||||
return r.failure(err)
|
||||
}
|
||||
wantType := "text"
|
||||
if numericColumn(name) {
|
||||
wantType = "bigint"
|
||||
}
|
||||
if name == "is_primary" || name == "attempts" {
|
||||
wantType = "integer"
|
||||
}
|
||||
wantIdentity := ""
|
||||
if spec.identity && name == "id" {
|
||||
wantIdentity = "a"
|
||||
}
|
||||
wantCollation := ""
|
||||
if wantType == "text" {
|
||||
wantCollation = "default"
|
||||
}
|
||||
if spec.name == "paths" && name == "path" || spec.name == "assets" && name == "address" {
|
||||
wantCollation = "C"
|
||||
}
|
||||
if kind != wantType || !required || def != defaults[spec.name+"."+name] || identity != wantIdentity || generated != "" || collation != wantCollation {
|
||||
report.Blockers = append(report.Blockers, fmt.Sprintf("column contract mismatch: %s.%s", spec.name, name))
|
||||
}
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
rows, err = tx.QueryContext(ctx, `SELECT pg_get_constraintdef(c.oid),c.convalidated,c.condeferrable,c.condeferred FROM pg_constraint c JOIN pg_class t ON t.oid=c.conrelid JOIN pg_namespace n ON n.oid=t.relnamespace WHERE n.nspname=current_schema() AND t.relname=$1`, spec.name)
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
wanted := map[string]bool{}
|
||||
for _, s := range constraints[spec.name] {
|
||||
wanted[s] = true
|
||||
}
|
||||
actual := map[string]bool{}
|
||||
matches := true
|
||||
constraintCount := 0
|
||||
for rows.Next() {
|
||||
constraintCount++
|
||||
var def string
|
||||
var valid, deferable, deferred bool
|
||||
if err := rows.Scan(&def, &valid, &deferable, &deferred); err != nil {
|
||||
rows.Close()
|
||||
return r.failure(err)
|
||||
}
|
||||
actual[def] = true
|
||||
matches = matches && valid && !deferable && !deferred && wanted[def]
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if !matches || len(actual) != len(wanted) || constraintCount != len(wanted) {
|
||||
report.Blockers = append(report.Blockers, "constraint contract mismatch: "+spec.name)
|
||||
}
|
||||
}
|
||||
var schema string
|
||||
if err := tx.QueryRowContext(ctx, `SELECT quote_ident(current_schema())`).Scan(&schema); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
indexes := map[string]string{
|
||||
"idx_paths_domain_object": "paths USING btree (domain_uuid, object_uuid)",
|
||||
"idx_revisions_metadata_head": "revisions USING btree (object_uuid, lang, created_at_ms DESC, uuid DESC)",
|
||||
"idx_asset_metadata_history": "asset_head_history USING btree (domain_uuid, address, id)",
|
||||
"idx_metadata_document_refs": "revision_document_refs USING btree (document_hash, revision_uuid)",
|
||||
"idx_metadata_outbox_pending": "activity_outbox USING btree (status, next_attempt_ms, domain_uuid, sequence)",
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT i.relname,pg_get_indexdef(i.oid),x.indisvalid,x.indisready FROM pg_index x JOIN pg_class i ON i.oid=x.indexrelid JOIN pg_class t ON t.oid=x.indrelid JOIN pg_namespace n ON n.oid=t.relnamespace WHERE n.nspname=current_schema() AND t.relname!='schema_migration_steps' AND NOT EXISTS(SELECT 1 FROM pg_constraint con WHERE con.conindid=i.oid)`)
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
seen := map[string]bool{}
|
||||
for rows.Next() {
|
||||
var name, def string
|
||||
var valid, ready bool
|
||||
if err := rows.Scan(&name, &def, &valid, &ready); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
suffix, ok := indexes[name]
|
||||
if !ok {
|
||||
report.Blockers = append(report.Blockers, "unported index: "+name)
|
||||
continue
|
||||
}
|
||||
seen[name] = true
|
||||
if def != "CREATE INDEX "+name+" ON "+schema+"."+suffix || !valid || !ready {
|
||||
report.Blockers = append(report.Blockers, "index contract mismatch: "+name)
|
||||
}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
for name := range indexes {
|
||||
if !seen[name] {
|
||||
report.Blockers = append(report.Blockers, "missing index: "+name)
|
||||
}
|
||||
}
|
||||
var sequenceMatches bool
|
||||
if identityTablePresent {
|
||||
err = tx.QueryRowContext(ctx, `SELECT seqstart=1 AND seqincrement=1 AND seqmin=1 AND seqmax=9223372036854775807 AND seqcache=1 AND NOT seqcycle FROM pg_sequence WHERE seqrelid=pg_get_serial_sequence('asset_head_history','id')::regclass`).Scan(&sequenceMatches)
|
||||
}
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return r.failure(err)
|
||||
}
|
||||
if !sequenceMatches {
|
||||
report.Blockers = append(report.Blockers, "identity sequence contract mismatch: asset_head_history.id")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPortabilityRejectsIndexDrift(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
for _, change := range []string{"missing", "wrong", "extra"} {
|
||||
t.Run(string(backend)+"/"+change, func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx := context.Background()
|
||||
if change != "extra" {
|
||||
if _, err := r.database.ExecContext(ctx, `DROP INDEX idx_paths_domain_object`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if change == "wrong" {
|
||||
if _, err := r.database.ExecContext(ctx, `CREATE INDEX idx_paths_domain_object ON paths(object_uuid)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if change == "extra" {
|
||||
if _, err := r.database.ExecContext(ctx, `CREATE UNIQUE INDEX unported_unique_rule ON paths(object_uuid)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
report, err := r.InspectPortability(ctx)
|
||||
if err != nil || report.CoreCompatible {
|
||||
t.Fatal("index drift accepted", err, report)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLPortabilityRejectsContractDrift(t *testing.T) {
|
||||
changes := map[string]string{
|
||||
"type": `ALTER TABLE assets ALTER COLUMN size_bytes TYPE INTEGER`,
|
||||
"default": `ALTER TABLE revisions ALTER COLUMN message SET DEFAULT 'different'`,
|
||||
"nullable": `ALTER TABLE revisions ALTER COLUMN content_type DROP NOT NULL`,
|
||||
"foreign-key": `ALTER TABLE paths DROP CONSTRAINT paths_object_uuid_fkey`,
|
||||
"collation": `ALTER TABLE paths ALTER COLUMN path TYPE TEXT COLLATE "default"`,
|
||||
"identity-mode": `ALTER TABLE asset_head_history ALTER COLUMN id SET GENERATED BY DEFAULT`,
|
||||
"identity-drop": `ALTER TABLE asset_head_history ALTER COLUMN id DROP IDENTITY`,
|
||||
"missing-table": `DROP TABLE asset_head_history`,
|
||||
"identity-step": `ALTER TABLE asset_head_history ALTER COLUMN id SET INCREMENT BY 7`,
|
||||
"rls": `ALTER TABLE objects ENABLE ROW LEVEL SECURITY`,
|
||||
"ledger-rls": `ALTER TABLE schema_migration_steps ENABLE ROW LEVEL SECURITY`,
|
||||
"routine": `CREATE FUNCTION unported_routine() RETURNS INTEGER LANGUAGE SQL AS 'SELECT 1'`,
|
||||
"sequence": `CREATE SEQUENCE unported_counter`,
|
||||
"rule": `CREATE RULE unported_rule AS ON DELETE TO paths DO INSTEAD NOTHING`,
|
||||
"duplicate-fk": `ALTER TABLE paths ADD CONSTRAINT duplicate_fk FOREIGN KEY (object_uuid) REFERENCES objects(uuid)`,
|
||||
}
|
||||
for name, q := range changes {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
r := testRepository(t, PostgreSQL)
|
||||
ctx := context.Background()
|
||||
if _, err := r.database.ExecContext(ctx, q); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
report, err := r.InspectPortability(ctx)
|
||||
if err != nil || report.CoreCompatible {
|
||||
t.Fatal("contract drift accepted", err, report)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,471 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
hashpkg "hash"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"pageswiki/internal/geduldh"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMigrationBlocked = errors.New("metadata migration blocked; inspect portability report")
|
||||
ErrDestinationNotEmpty = errors.New("metadata migration destination is not empty")
|
||||
ErrIntegrity = errors.New("metadata integrity check failed")
|
||||
)
|
||||
|
||||
type TableFingerprint struct {
|
||||
Table string `json:"table"`
|
||||
Rows int64 `json:"rows"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
type CoreSnapshot struct {
|
||||
Scope string `json:"scope"`
|
||||
Tables []TableFingerprint `json:"tables"`
|
||||
IdentityNext int64 `json:"asset_history_identity_next"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
type RehearsalOptions struct {
|
||||
// Caller asserts that all writers and GC are stopped for both instances.
|
||||
// A SQL snapshot alone is not a cross-store or online migration protocol.
|
||||
Offline bool
|
||||
Documents geduldh.Store
|
||||
}
|
||||
|
||||
// CloneCoreForRehearsal copies only a compatible portable core into an already
|
||||
// initialized, empty staging schema. It is deliberately not a production
|
||||
// migration entry point. Unknown tables/triggers/views/columns/ledger entries
|
||||
// block copying rather than silently losing application data or permissions.
|
||||
// Canonical payloads are verified in the caller's retained Document store;
|
||||
// calendar files, secrets, application configuration and blobs are NOT copied.
|
||||
func CloneCoreForRehearsal(ctx context.Context, source, destination *Repository, options RehearsalOptions) (CoreSnapshot, error) {
|
||||
if !options.Offline || options.Documents == nil || source == nil || destination == nil || source.database == destination.database {
|
||||
return CoreSnapshot{}, ErrMigrationBlocked
|
||||
}
|
||||
if same, err := sameDatabase(ctx, source, destination); err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
} else if same {
|
||||
return CoreSnapshot{}, ErrMigrationBlocked
|
||||
}
|
||||
src, err := source.database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead})
|
||||
if err != nil {
|
||||
return CoreSnapshot{}, source.failure(err)
|
||||
}
|
||||
defer src.Rollback()
|
||||
report, err := source.inspectPortability(ctx, src)
|
||||
if err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
}
|
||||
if !report.CoreCompatible {
|
||||
return CoreSnapshot{}, ErrMigrationBlocked
|
||||
}
|
||||
if err := source.verifyCoreRelations(ctx, src); err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
}
|
||||
// Reject an occupied/incompatible destination before verifying a large
|
||||
// Document store. Recheck while holding destination locks before copying.
|
||||
preflight, err := destination.database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead})
|
||||
if err != nil {
|
||||
return CoreSnapshot{}, destination.failure(err)
|
||||
}
|
||||
err = destination.checkEmpty(ctx, preflight)
|
||||
_ = preflight.Rollback()
|
||||
if err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
}
|
||||
if err := source.verifyDocuments(ctx, src, options.Documents); err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
}
|
||||
var result CoreSnapshot
|
||||
err = destination.write(ctx, "migration/portable-core-rehearsal", func(dst *sql.Tx) error {
|
||||
if destination.backend == PostgreSQL {
|
||||
// Lock all tables, not just a cooperative advisory key: an accidental
|
||||
// concurrent writer must not race the empty-destination check.
|
||||
names := []string{`"schema_migration_steps"`}
|
||||
for _, s := range coreTables {
|
||||
names = append(names, quote(s.name))
|
||||
}
|
||||
if _, err := dst.ExecContext(ctx, `LOCK TABLE `+strings.Join(names, ",")+` IN ACCESS EXCLUSIVE MODE`); err != nil {
|
||||
return destination.failure(err)
|
||||
}
|
||||
}
|
||||
if err := destination.checkEmpty(ctx, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
var expected []TableFingerprint
|
||||
for _, spec := range coreTables {
|
||||
fingerprint, err := source.streamTable(ctx, src, spec, func(values []any) error {
|
||||
marks := make([]string, len(values))
|
||||
for i := range values {
|
||||
marks[i] = destination.p(i + 1)
|
||||
}
|
||||
q := `INSERT INTO ` + quote(spec.name) + ` (` + quoted(spec.columns) + `)`
|
||||
if spec.identity && destination.backend == PostgreSQL {
|
||||
q += ` OVERRIDING SYSTEM VALUE`
|
||||
}
|
||||
_, err := dst.ExecContext(ctx, q+` VALUES (`+strings.Join(marks, ",")+`)`, values...)
|
||||
return destination.failure(err)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
expected = append(expected, fingerprint)
|
||||
}
|
||||
if err := destination.verifyCoreRelations(ctx, dst); err != nil {
|
||||
return err
|
||||
}
|
||||
next, err := source.identityNext(ctx, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := destination.restoreIdentity(ctx, dst, next); err != nil {
|
||||
return err
|
||||
}
|
||||
actual, err := destination.fingerprint(ctx, dst)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
want := snapshot(expected, next)
|
||||
if want.SHA256 != actual.SHA256 {
|
||||
return ErrIntegrity
|
||||
}
|
||||
result = actual
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// FingerprintCore reads a single snapshot and never mutates the database. It
|
||||
// identifies metadata content, not a backup of canonical payloads or secrets.
|
||||
func (r *Repository) FingerprintCore(ctx context.Context) (CoreSnapshot, error) {
|
||||
tx, err := r.database.BeginTx(ctx, &sql.TxOptions{ReadOnly: true, Isolation: sql.LevelRepeatableRead})
|
||||
if err != nil {
|
||||
return CoreSnapshot{}, r.failure(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
report, err := r.inspectPortability(ctx, tx)
|
||||
if err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
}
|
||||
if !report.CoreCompatible {
|
||||
return CoreSnapshot{}, ErrMigrationBlocked
|
||||
}
|
||||
if err := r.verifyCoreRelations(ctx, tx); err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
}
|
||||
return r.fingerprint(ctx, tx)
|
||||
}
|
||||
|
||||
func (r *Repository) fingerprint(ctx context.Context, tx *sql.Tx) (CoreSnapshot, error) {
|
||||
var tables []TableFingerprint
|
||||
for _, spec := range coreTables {
|
||||
f, err := r.streamTable(ctx, tx, spec, nil)
|
||||
if err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
}
|
||||
tables = append(tables, f)
|
||||
}
|
||||
next, err := r.identityNext(ctx, tx)
|
||||
if err != nil {
|
||||
return CoreSnapshot{}, err
|
||||
}
|
||||
return snapshot(tables, next), nil
|
||||
}
|
||||
|
||||
func snapshot(tables []TableFingerprint, next int64) CoreSnapshot {
|
||||
data, _ := json.Marshal(struct {
|
||||
Tables []TableFingerprint
|
||||
IdentityNext int64
|
||||
}{tables, next})
|
||||
sum := sha256.Sum256(data)
|
||||
return CoreSnapshot{Scope: "portable-core-rehearsal-only", Tables: tables, IdentityNext: next, SHA256: hex.EncodeToString(sum[:])}
|
||||
}
|
||||
|
||||
func quote(s string) string { return `"` + strings.ReplaceAll(s, `"`, `""`) + `"` }
|
||||
func quoted(names []string) string {
|
||||
parts := make([]string, len(names))
|
||||
for i, n := range names {
|
||||
parts[i] = quote(n)
|
||||
}
|
||||
return strings.Join(parts, ",")
|
||||
}
|
||||
func frame(h hashpkg.Hash, data []byte) {
|
||||
var size [8]byte
|
||||
binary.BigEndian.PutUint64(size[:], uint64(len(data)))
|
||||
h.Write(size[:])
|
||||
h.Write(data)
|
||||
}
|
||||
|
||||
func (r *Repository) streamTable(ctx context.Context, tx *sql.Tx, spec tableSpec, consume func([]any) error) (TableFingerprint, error) {
|
||||
order := make([]string, len(spec.keys))
|
||||
for i, k := range spec.keys {
|
||||
order[i] = quote(k)
|
||||
if !spec.identity {
|
||||
if r.backend == PostgreSQL {
|
||||
order[i] += ` COLLATE "C"`
|
||||
} else {
|
||||
order[i] += ` COLLATE BINARY`
|
||||
}
|
||||
}
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT `+quoted(spec.columns)+` FROM `+quote(spec.name)+` ORDER BY `+strings.Join(order, ","))
|
||||
if err != nil {
|
||||
return TableFingerprint{}, r.failure(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
h := sha256.New()
|
||||
frame(h, []byte(spec.name))
|
||||
data, _ := json.Marshal(spec.columns)
|
||||
frame(h, data)
|
||||
var count int64
|
||||
for rows.Next() {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return TableFingerprint{}, err
|
||||
}
|
||||
values := make([]any, len(spec.columns))
|
||||
pointers := make([]any, len(values))
|
||||
for i := range values {
|
||||
pointers[i] = &values[i]
|
||||
}
|
||||
if err := rows.Scan(pointers...); err != nil {
|
||||
return TableFingerprint{}, r.failure(err)
|
||||
}
|
||||
for i, v := range values {
|
||||
numeric := numericColumn(spec.columns[i])
|
||||
switch value := v.(type) {
|
||||
case string:
|
||||
if numeric {
|
||||
return TableFingerprint{}, fmt.Errorf("%w: noninteger in %s.%s", ErrIntegrity, spec.name, spec.columns[i])
|
||||
}
|
||||
if !utf8.ValidString(value) || strings.ContainsRune(value, 0) {
|
||||
return TableFingerprint{}, fmt.Errorf("%w: nonportable text in %s.%s", ErrIntegrity, spec.name, spec.columns[i])
|
||||
}
|
||||
if (spec.columns[i] == "document_hash" || spec.columns[i] == "head_document_hash") && !validHash(value) {
|
||||
return TableFingerprint{}, fmt.Errorf("%w: invalid hash in %s", ErrIntegrity, spec.name)
|
||||
}
|
||||
case int64:
|
||||
if !numeric {
|
||||
return TableFingerprint{}, fmt.Errorf("%w: nontext in %s.%s", ErrIntegrity, spec.name, spec.columns[i])
|
||||
}
|
||||
default:
|
||||
return TableFingerprint{}, fmt.Errorf("%w: unsupported value type in %s.%s", ErrIntegrity, spec.name, spec.columns[i])
|
||||
}
|
||||
}
|
||||
data, err := json.Marshal(values)
|
||||
if err != nil {
|
||||
return TableFingerprint{}, ErrIntegrity
|
||||
}
|
||||
frame(h, data)
|
||||
if consume != nil {
|
||||
if err := consume(values); err != nil {
|
||||
return TableFingerprint{}, err
|
||||
}
|
||||
}
|
||||
count++
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return TableFingerprint{}, r.failure(err)
|
||||
}
|
||||
return TableFingerprint{Table: spec.name, Rows: count, SHA256: hex.EncodeToString(h.Sum(nil))}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) verifyCoreRelations(ctx context.Context, tx *sql.Tx) error {
|
||||
checks := []struct{ name, query string }{
|
||||
{"object origin", `SELECT 1 FROM objects o WHERE NOT EXISTS(SELECT 1 FROM object_origins x WHERE x.object_uuid=o.uuid) LIMIT 1`},
|
||||
{"origin object", `SELECT 1 FROM object_origins x WHERE NOT EXISTS(SELECT 1 FROM objects o WHERE o.uuid=x.object_uuid) LIMIT 1`},
|
||||
{"path object", `SELECT 1 FROM paths p WHERE NOT EXISTS(SELECT 1 FROM objects o WHERE o.uuid=p.object_uuid) LIMIT 1`},
|
||||
{"revision object", `SELECT 1 FROM revisions r WHERE NOT EXISTS(SELECT 1 FROM objects o WHERE o.uuid=r.object_uuid) LIMIT 1`},
|
||||
{"asset object", `SELECT 1 FROM assets a WHERE NOT EXISTS(SELECT 1 FROM objects o WHERE o.uuid=a.object_uuid) LIMIT 1`},
|
||||
{"revision reference", `SELECT 1 FROM revision_document_refs x WHERE NOT EXISTS(SELECT 1 FROM revisions r WHERE r.uuid=x.revision_uuid) LIMIT 1`},
|
||||
{"outbox watermark", `SELECT 1 FROM activity_outbox o WHERE o.sequence<1 OR NOT EXISTS(SELECT 1 FROM activity_outbox_heads h WHERE h.domain_uuid=o.domain_uuid AND h.event_type=o.event_type AND h.last_sequence>=o.sequence) LIMIT 1`},
|
||||
{"revision event", `SELECT 1 FROM activity_outbox o WHERE o.event_type='revision.created' AND NOT EXISTS(SELECT 1 FROM revisions r WHERE r.uuid=o.target_uuid) LIMIT 1`},
|
||||
{"asset history id", `SELECT 1 FROM asset_head_history WHERE id<1 LIMIT 1`},
|
||||
}
|
||||
for _, check := range checks {
|
||||
var found int
|
||||
err := tx.QueryRowContext(ctx, check.query).Scan(&found)
|
||||
if err == nil {
|
||||
return fmt.Errorf("%w: %s", ErrIntegrity, check.name)
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
return r.failure(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) verifyDocuments(ctx context.Context, tx *sql.Tx, documents geduldh.Store) error {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT document_hash FROM revisions UNION SELECT head_document_hash FROM assets UNION SELECT document_hash FROM asset_head_history UNION SELECT document_hash FROM revision_document_refs ORDER BY 1`)
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var hash string
|
||||
if err := rows.Scan(&hash); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if !validHash(hash) {
|
||||
return ErrIntegrity
|
||||
}
|
||||
if err := documents.Verify(ctx, hash); err != nil {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
return fmt.Errorf("%w: Document verification failed", ErrIntegrity)
|
||||
}
|
||||
}
|
||||
return r.failure(rows.Err())
|
||||
}
|
||||
|
||||
func (r *Repository) checkEmpty(ctx context.Context, tx *sql.Tx) error {
|
||||
report, err := r.inspectPortability(ctx, tx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !report.CoreCompatible {
|
||||
return ErrMigrationBlocked
|
||||
}
|
||||
for _, s := range coreTables {
|
||||
var occupied bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM `+quote(s.name)+`)`).Scan(&occupied); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if occupied {
|
||||
return ErrDestinationNotEmpty
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) identityNext(ctx context.Context, tx *sql.Tx) (int64, error) {
|
||||
var max sql.NullInt64
|
||||
if err := tx.QueryRowContext(ctx, `SELECT max(id) FROM asset_head_history`).Scan(&max); err != nil {
|
||||
return 0, r.failure(err)
|
||||
}
|
||||
if max.Int64 < 0 || max.Int64 == math.MaxInt64 {
|
||||
return 0, ErrIntegrity
|
||||
}
|
||||
next := max.Int64 + 1
|
||||
if r.backend == SQLite {
|
||||
var seq sql.NullInt64
|
||||
if err := tx.QueryRowContext(ctx, `SELECT max(seq) FROM sqlite_sequence WHERE name='asset_head_history'`).Scan(&seq); err != nil {
|
||||
return 0, r.failure(err)
|
||||
}
|
||||
if seq.Int64 < 0 || seq.Int64 == math.MaxInt64 {
|
||||
return 0, ErrIntegrity
|
||||
}
|
||||
if seq.Int64+1 > next {
|
||||
next = seq.Int64 + 1
|
||||
}
|
||||
} else {
|
||||
var schema, name string
|
||||
if err := tx.QueryRowContext(ctx, `SELECT n.nspname,c.relname FROM pg_class c JOIN pg_namespace n ON n.oid=c.relnamespace WHERE c.oid=pg_get_serial_sequence('asset_head_history','id')::regclass`).Scan(&schema, &name); err != nil {
|
||||
return 0, r.failure(err)
|
||||
}
|
||||
var last int64
|
||||
var called bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT last_value,is_called FROM `+quote(schema)+`.`+quote(name)).Scan(&last, &called); err != nil {
|
||||
return 0, r.failure(err)
|
||||
}
|
||||
if last < 1 || last == math.MaxInt64 {
|
||||
return 0, ErrIntegrity
|
||||
}
|
||||
if called {
|
||||
last++
|
||||
}
|
||||
if last > next {
|
||||
next = last
|
||||
}
|
||||
}
|
||||
return next, nil
|
||||
}
|
||||
|
||||
func (r *Repository) restoreIdentity(ctx context.Context, tx *sql.Tx, next int64) error {
|
||||
if next < 1 {
|
||||
return ErrIntegrity
|
||||
}
|
||||
if r.backend == PostgreSQL {
|
||||
// RESTART rolls back with the transaction; setval would not.
|
||||
_, err := tx.ExecContext(ctx, `ALTER TABLE asset_head_history ALTER COLUMN id RESTART WITH `+strconv.FormatInt(next, 10))
|
||||
return r.failure(err)
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, `UPDATE sqlite_sequence SET seq=? WHERE name='asset_head_history'`, next-1)
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
n, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if n == 0 {
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO sqlite_sequence(name,seq) VALUES ('asset_head_history',?)`, next-1)
|
||||
}
|
||||
return r.failure(err)
|
||||
}
|
||||
|
||||
func sameDatabase(ctx context.Context, a, b *Repository) (bool, error) {
|
||||
if a.backend != b.backend {
|
||||
return false, nil
|
||||
}
|
||||
if a.backend == SQLite {
|
||||
path := func(r *Repository) (string, error) {
|
||||
var seq int
|
||||
var name, file string
|
||||
err := r.database.QueryRowContext(ctx, `SELECT seq,name,file FROM pragma_database_list WHERE name='main'`).Scan(&seq, &name, &file)
|
||||
if err != nil {
|
||||
return "", r.failure(err)
|
||||
}
|
||||
if file == "" {
|
||||
return "", ErrMigrationBlocked
|
||||
}
|
||||
return file, nil
|
||||
}
|
||||
x, err := path(a)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
y, err := path(b)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
xInfo, err := os.Stat(x)
|
||||
if err != nil {
|
||||
return false, ErrMigrationBlocked
|
||||
}
|
||||
yInfo, err := os.Stat(y)
|
||||
if err != nil {
|
||||
return false, ErrMigrationBlocked
|
||||
}
|
||||
return os.SameFile(xInfo, yInfo), nil
|
||||
}
|
||||
type identity struct{ database, schema, start string }
|
||||
read := func(r *Repository) (identity, error) {
|
||||
var i identity
|
||||
err := r.database.QueryRowContext(ctx, `SELECT current_database(),current_schema(),extract(epoch from pg_postmaster_start_time())::text`).Scan(&i.database, &i.schema, &i.start)
|
||||
return i, r.failure(err)
|
||||
}
|
||||
x, err := read(a)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
y, err := read(b)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return x == y, nil
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/geduldh"
|
||||
)
|
||||
|
||||
func TestPortabilityIsFailClosed(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
for _, change := range []string{"table", "view", "column", "history", "component"} {
|
||||
t.Run(string(backend)+"/"+change, func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx := context.Background()
|
||||
report, err := r.InspectPortability(ctx)
|
||||
if err != nil || !report.CoreCompatible || report.ApplicationReady {
|
||||
t.Fatal("fresh core readiness", err, report)
|
||||
}
|
||||
q := map[string]string{
|
||||
"table": `CREATE TABLE auth_private_users(secret TEXT)`,
|
||||
"view": `CREATE VIEW private_view AS SELECT uuid FROM objects`,
|
||||
"column": `ALTER TABLE objects ADD COLUMN unexpected TEXT`,
|
||||
"history": `UPDATE schema_migration_steps SET checksum='tampered' WHERE version=1`,
|
||||
"component": `INSERT INTO schema_migration_steps(component,version,name,checksum,applied_at_ms) VALUES ('future',1,'other','unknown',1)`,
|
||||
}[change]
|
||||
if _, err := r.database.ExecContext(ctx, q); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
report, err = r.InspectPortability(ctx)
|
||||
if err != nil || report.CoreCompatible || len(report.Blockers) == 0 {
|
||||
t.Fatal("unsupported state accepted", err, report)
|
||||
}
|
||||
if _, err := r.FingerprintCore(ctx); !errors.Is(err, ErrMigrationBlocked) {
|
||||
t.Fatal("unsupported state fingerprinted as portable", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPortabilityDoesNotHideSameNamedTrigger(t *testing.T) {
|
||||
r := testRepository(t, SQLite)
|
||||
ctx := context.Background()
|
||||
if _, err := r.database.ExecContext(ctx, `CREATE TRIGGER objects AFTER UPDATE ON objects BEGIN SELECT 1; END`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
report, err := r.InspectPortability(ctx)
|
||||
if err != nil || report.CoreCompatible {
|
||||
t.Fatal("security trigger hidden by table name", err)
|
||||
}
|
||||
if !strings.Contains(strings.Join(report.Blockers, " "), "trigger: objects") {
|
||||
t.Fatal("trigger not identified", report)
|
||||
}
|
||||
}
|
||||
|
||||
func seedRehearsal(t *testing.T, r *Repository) (geduldh.Store, Revision) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
docs, _, v := documentFixture(t, r)
|
||||
first, err := r.SaveDocument(ctx, docs, "test", v, []byte(`<h1>移行</h1><img src="asset:image:cover">`), "", "actor")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.BindPath(ctx, "test", "/alias", v.ObjectUUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, path := range []string{"/日本語%_", "/Case", "/case", "/é", "/e\u0301"} {
|
||||
if err := r.BindPath(ctx, "test", path, v.ObjectUUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// Values above JavaScript's safe-integer range must never pass through
|
||||
// float64 on the migration path. Also exercise identity/sequence recovery.
|
||||
const large = int64(9007199254741011)
|
||||
if _, err := r.database.ExecContext(ctx, `UPDATE asset_head_history SET id=`+r.p(1), large); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.database.ExecContext(ctx, `UPDATE activity_outbox SET sequence=`+r.p(1)+`,status='failed',attempts=3,last_error='synthetic'`, large); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.database.ExecContext(ctx, `UPDATE activity_outbox_heads SET last_sequence=`+r.p(1), large); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return docs, first.Revision
|
||||
}
|
||||
|
||||
func TestCoreFingerprintIncludesContentAndPurgedIdentity(t *testing.T) {
|
||||
source := testRepository(t, SQLite)
|
||||
target := testRepository(t, SQLite)
|
||||
ctx := context.Background()
|
||||
docs, v := seedRehearsal(t, source)
|
||||
before, err := source.FingerprintCore(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := source.database.ExecContext(ctx, `UPDATE objects SET kind='changed' WHERE uuid=?`, v.ObjectUUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
after, err := source.FingerprintCore(ctx)
|
||||
if err != nil || after.SHA256 == before.SHA256 {
|
||||
t.Fatal("same row counts hid content change", err)
|
||||
}
|
||||
// A retained generator high-water mark must survive even if the highest
|
||||
// history rows have been purged. Reusing an old history URL is not safe.
|
||||
if _, err := source.database.ExecContext(ctx, `UPDATE sqlite_sequence SET seq=9007199254741999 WHERE name='asset_head_history'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := CloneCoreForRehearsal(ctx, source, target, RehearsalOptions{Offline: true, Documents: docs})
|
||||
if err != nil || result.IdentityNext != 9007199254742000 {
|
||||
t.Fatal("purged sequence lost", err, result.IdentityNext)
|
||||
}
|
||||
a, err := target.GetAsset(ctx, "test", "asset:image:cover")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := target.PutAsset(ctx, a, a.HeadDocumentHash, "actor"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var id int64
|
||||
if err := target.database.QueryRowContext(ctx, `SELECT max(id) FROM asset_head_history`).Scan(&id); err != nil || id != result.IdentityNext {
|
||||
t.Fatal("old history ID reused", err, id)
|
||||
}
|
||||
if _, err := source.database.ExecContext(ctx, `UPDATE objects SET created_at='not-an-integer'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := source.FingerprintCore(ctx); !errors.Is(err, ErrIntegrity) {
|
||||
t.Fatal("invalid integer type accepted", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoreRoundTripSQLitePostgreSQLSQLite(t *testing.T) {
|
||||
source := testRepository(t, SQLite)
|
||||
target := testRepository(t, PostgreSQL)
|
||||
restored := testRepository(t, SQLite)
|
||||
ctx := context.Background()
|
||||
docs, first := seedRehearsal(t, source)
|
||||
before, err := source.FingerprintCore(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := CloneCoreForRehearsal(ctx, source, target, RehearsalOptions{Offline: true, Documents: docs})
|
||||
if err != nil || result.SHA256 != before.SHA256 {
|
||||
t.Fatal("SQLite to PostgreSQL", err, result)
|
||||
}
|
||||
got, err := target.Head(ctx, "test", first.ObjectUUID, first.Language)
|
||||
if err != nil || got != first {
|
||||
t.Fatal("revision identity/content changed", err)
|
||||
}
|
||||
asset, err := target.GetAsset(ctx, "test", "asset:image:cover")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
asset.Filename = "new.png"
|
||||
if _, err := target.PutAsset(ctx, asset, asset.HeadDocumentHash, "actor"); err != nil {
|
||||
t.Fatal("identity sequence not repaired", err)
|
||||
}
|
||||
var id int64
|
||||
if err := target.database.QueryRowContext(ctx, `SELECT max(id) FROM asset_head_history`).Scan(&id); err != nil || id != 9007199254741012 {
|
||||
t.Fatal("identity precision/restart", id, err)
|
||||
}
|
||||
if _, err := target.database.ExecContext(ctx, `ALTER TABLE asset_head_history ALTER COLUMN id RESTART WITH 9007199254743000`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
next, err := target.SaveDocument(ctx, docs, "test", first, []byte(`<h1>PostgreSQLで更新</h1>`), first.UUID, "actor")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
targetState, err := target.FingerprintCore(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
back, err := CloneCoreForRehearsal(ctx, target, restored, RehearsalOptions{Offline: true, Documents: docs})
|
||||
if err != nil || back.SHA256 != targetState.SHA256 {
|
||||
t.Fatal("PostgreSQL to SQLite", err)
|
||||
}
|
||||
if got, err := restored.Head(ctx, "test", first.ObjectUUID, first.Language); err != nil || got.UUID != next.Revision.UUID {
|
||||
t.Fatal("post-cutover update lost on rollback", err)
|
||||
}
|
||||
if got, err := source.FingerprintCore(ctx); err != nil || got.SHA256 != before.SHA256 {
|
||||
t.Fatal("source was modified", err)
|
||||
}
|
||||
if _, err := restored.PutAsset(ctx, asset, asset.HeadDocumentHash, "actor"); err != nil {
|
||||
t.Fatal("SQLite sequence not recovered", err)
|
||||
}
|
||||
if err := restored.database.QueryRowContext(ctx, `SELECT max(id) FROM asset_head_history`).Scan(&id); err != nil || id != 9007199254743000 {
|
||||
t.Fatal("PostgreSQL retained generator state lost", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoreCloneRejectsSameDatabaseThroughDifferentPools(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
var alias *Repository
|
||||
if backend == SQLite {
|
||||
var file string
|
||||
if err := r.database.QueryRow(`SELECT file FROM pragma_database_list WHERE name='main'`).Scan(&file); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
database, err := db.Open(file)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer database.Close()
|
||||
alias, _ = New(database, backend)
|
||||
} else {
|
||||
var schema string
|
||||
if err := r.database.QueryRow(`SELECT current_schema()`).Scan(&schema); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u, err := url.Parse(os.Getenv("PAGESWIKI_TEST_POSTGRES_DSN"))
|
||||
if err != nil {
|
||||
t.Fatal("invalid test DSN")
|
||||
}
|
||||
q := u.Query()
|
||||
q.Set("search_path", schema)
|
||||
u.RawQuery = q.Encode()
|
||||
database, err := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: u.String(), AllowInsecure: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer database.Close()
|
||||
alias, _ = New(database, backend)
|
||||
}
|
||||
documents, _ := geduldh.NewLocal(t.TempDir())
|
||||
if _, err := CloneCoreForRehearsal(ctx, r, alias, RehearsalOptions{Offline: true, Documents: documents}); !errors.Is(err, ErrMigrationBlocked) {
|
||||
t.Fatal("self-copy not rejected before locking", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoreCloneRejectsUnsafeConditionsAndRollsBack(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
source := testRepository(t, SQLite)
|
||||
target := testRepository(t, backend)
|
||||
ctx := context.Background()
|
||||
docs, _ := seedRehearsal(t, source)
|
||||
if _, err := CloneCoreForRehearsal(ctx, source, target, RehearsalOptions{Documents: docs}); !errors.Is(err, ErrMigrationBlocked) {
|
||||
t.Fatal("online migration permitted", err)
|
||||
}
|
||||
if _, err := CloneCoreForRehearsal(ctx, source, target, RehearsalOptions{Offline: true}); !errors.Is(err, ErrMigrationBlocked) {
|
||||
t.Fatal("unverified Documents permitted", err)
|
||||
}
|
||||
empty, err := target.FingerprintCore(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Extra uniqueness rules must block the plan before any copying.
|
||||
if _, err := target.database.ExecContext(ctx, `CREATE UNIQUE INDEX synthetic_rehearsal_failure ON paths(object_uuid)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := CloneCoreForRehearsal(ctx, source, target, RehearsalOptions{Offline: true, Documents: docs}); !errors.Is(err, ErrMigrationBlocked) {
|
||||
t.Fatal("unexpected unique index accepted", err)
|
||||
}
|
||||
if _, err := target.database.ExecContext(ctx, `DROP INDEX synthetic_rehearsal_failure`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := target.FingerprintCore(ctx); err != nil || got.SHA256 != empty.SHA256 {
|
||||
t.Fatal("partial destination rows survived", err)
|
||||
}
|
||||
// SQLite can contain a BLOB despite a TEXT declaration. Detect it
|
||||
// while streaming, after earlier tables have been inserted, and roll
|
||||
// back every destination row instead of coercing or truncating it.
|
||||
if _, err := source.database.ExecContext(ctx, `UPDATE paths SET path=CAST(path AS BLOB)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := CloneCoreForRehearsal(ctx, source, target, RehearsalOptions{Offline: true, Documents: docs}); !errors.Is(err, ErrIntegrity) {
|
||||
t.Fatal("nonportable row accepted", err)
|
||||
}
|
||||
if got, err := target.FingerprintCore(ctx); err != nil || got.SHA256 != empty.SHA256 {
|
||||
t.Fatal("mid-copy failure left rows", err)
|
||||
}
|
||||
if _, err := source.database.ExecContext(ctx, `UPDATE paths SET path=CAST(path AS TEXT)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := target.CreatePage(ctx, "test", "/do-not-overwrite"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
occupied, err := target.FingerprintCore(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := CloneCoreForRehearsal(ctx, source, target, RehearsalOptions{Offline: true, Documents: docs}); !errors.Is(err, ErrDestinationNotEmpty) {
|
||||
t.Fatal("occupied destination overwritten", err)
|
||||
}
|
||||
if got, err := target.FingerprintCore(ctx); err != nil || got.SHA256 != occupied.SHA256 {
|
||||
t.Fatal("occupied target changed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCoreCloneRejectsMissingDocumentsAndBrokenRelations(t *testing.T) {
|
||||
r := testRepository(t, SQLite)
|
||||
target := testRepository(t, SQLite)
|
||||
ctx := context.Background()
|
||||
docs, _ := seedRehearsal(t, r)
|
||||
if _, err := r.database.ExecContext(ctx, `UPDATE paths SET object_uuid='missing'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := CloneCoreForRehearsal(ctx, r, target, RehearsalOptions{Offline: true, Documents: docs}); !errors.Is(err, ErrIntegrity) {
|
||||
t.Fatal("broken references copied", err)
|
||||
}
|
||||
r = testRepository(t, SQLite)
|
||||
docs, _ = seedRehearsal(t, r)
|
||||
missing, _ := geduldh.NewLocal(t.TempDir())
|
||||
if _, err := CloneCoreForRehearsal(ctx, r, target, RehearsalOptions{Offline: true, Documents: missing}); !errors.Is(err, ErrIntegrity) {
|
||||
t.Fatal("missing Document accepted", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,445 @@
|
||||
// Package metadata contains the portable metadata persistence contract. It does
|
||||
// not authorize requests, store payloads, run legacy Store initialization, or
|
||||
// constitute a PostgreSQL-capable application. Payloads must be persisted and
|
||||
// verified in Geduldh before publishing their hash here.
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"golang.org/x/text/language"
|
||||
"modernc.org/sqlite"
|
||||
)
|
||||
|
||||
type Backend string
|
||||
|
||||
const (
|
||||
SQLite Backend = "sqlite"
|
||||
PostgreSQL Backend = "postgres"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("metadata not found")
|
||||
ErrConflict = errors.New("metadata conflict")
|
||||
ErrInvalid = errors.New("invalid metadata")
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
database *sql.DB
|
||||
backend Backend
|
||||
applicationNamespace bool
|
||||
}
|
||||
|
||||
// New never migrates a schema or creates keys. Ownership of database stays with
|
||||
// the caller. Backend must match the actual connection; no fallback is allowed.
|
||||
func New(database *sql.DB, backend Backend) (*Repository, error) {
|
||||
if database == nil || (backend != SQLite && backend != PostgreSQL) {
|
||||
return nil, ErrInvalid
|
||||
}
|
||||
return &Repository{database: database, backend: backend}, nil
|
||||
}
|
||||
|
||||
// NewApplication uses the application's namespace/administration lock before
|
||||
// the metadata Domain/Object locks. Namespace validation is repeated inside
|
||||
// the commit transaction. This does not replace request authorization, perform
|
||||
// migrations, or make an incomplete application Store ready to serve traffic.
|
||||
func NewApplication(database *sql.DB, backend Backend) (*Repository, error) {
|
||||
r, err := New(database, backend)
|
||||
if err == nil {
|
||||
r.applicationNamespace = true
|
||||
}
|
||||
return r, err
|
||||
}
|
||||
|
||||
// p generates a placeholder, rather than rewriting arbitrary SQL (which can
|
||||
// contain JSON operators, strings or comments with literal question marks).
|
||||
func (r *Repository) p(n int) string {
|
||||
if r.backend == PostgreSQL {
|
||||
return fmt.Sprintf("$%d", n)
|
||||
}
|
||||
return "?"
|
||||
}
|
||||
|
||||
func (r *Repository) failure(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ErrNotFound
|
||||
}
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return context.Canceled
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return context.DeadlineExceeded
|
||||
}
|
||||
var pg *pgconn.PgError
|
||||
if errors.As(err, &pg) {
|
||||
if pg.Code == "23505" || pg.Code == "40001" || pg.Code == "40P01" {
|
||||
return ErrConflict
|
||||
}
|
||||
return fmt.Errorf("PostgreSQL metadata operation failed (SQLSTATE %s)", pg.Code)
|
||||
}
|
||||
var se *sqlite.Error
|
||||
if errors.As(err, &se) {
|
||||
switch se.Code() {
|
||||
case 1555, 2067:
|
||||
return ErrConflict
|
||||
}
|
||||
return fmt.Errorf("SQLite metadata operation failed (code %d)", se.Code())
|
||||
}
|
||||
// Network errors may include a credential-bearing DSN or server addresses.
|
||||
return errors.New("metadata database operation failed")
|
||||
}
|
||||
|
||||
func canonicalPath(raw string) (string, error) {
|
||||
p := strings.TrimSpace(raw)
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
if p != "/" {
|
||||
p = strings.TrimRight(p, "/")
|
||||
}
|
||||
if p == "" || !utf8.ValidString(p) || strings.ContainsAny(p, "\x00\r\n") {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
if p != "/" {
|
||||
for _, part := range strings.Split(p[1:], "/") {
|
||||
if part == "" || part == "." || part == ".." {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
}
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func validHash(hash string) bool {
|
||||
if len(hash) != 64 {
|
||||
return false
|
||||
}
|
||||
for _, c := range hash {
|
||||
if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// write locks before any read. SQLite acquires its single-writer reservation;
|
||||
// PostgreSQL locks a stable, namespaced key for the whole transaction. Path
|
||||
// writers share a domain key; revisions additionally take an object key after
|
||||
// the domain key, even when an object is bound in more than one domain.
|
||||
func (r *Repository) write(ctx context.Context, key string, fn func(*sql.Tx) error) error {
|
||||
tx, err := r.database.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if r.backend == PostgreSQL {
|
||||
if r.applicationNamespace {
|
||||
if _, err := tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(1885431653,1853058931)`); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
}
|
||||
err = r.lock(ctx, tx, key)
|
||||
} else {
|
||||
_, err = tx.ExecContext(ctx, `UPDATE paths SET created_at=created_at WHERE 0`)
|
||||
}
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if r.applicationNamespace {
|
||||
if !strings.HasPrefix(key, "domain/") {
|
||||
return ErrInvalid
|
||||
}
|
||||
var local bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domains d JOIN campuses c ON c.uuid=d.campus_uuid WHERE d.uuid=`+r.p(1)+` AND d.enabled=1 AND c.enabled=1 AND d.placement='local')`, strings.TrimPrefix(key, "domain/")).Scan(&local); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if !local {
|
||||
return ErrNotFound
|
||||
}
|
||||
}
|
||||
if err = fn(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return r.failure(tx.Commit())
|
||||
}
|
||||
|
||||
func (r *Repository) lock(ctx context.Context, tx *sql.Tx, key string) error {
|
||||
if r.backend != PostgreSQL {
|
||||
return nil
|
||||
}
|
||||
h := sha256.Sum256([]byte("pageswiki/metadata/" + key))
|
||||
_, err := tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock($1)`, int64(binary.BigEndian.Uint64(h[:8])))
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) CreatePage(ctx context.Context, domain, path string) (string, error) {
|
||||
p, err := canonicalPath(path)
|
||||
if err != nil || strings.TrimSpace(domain) == "" {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
id := uuid.NewString()
|
||||
err = r.write(ctx, "domain/"+domain, func(tx *sql.Tx) error {
|
||||
now := time.Now().Unix()
|
||||
for _, q := range []struct {
|
||||
sql string
|
||||
args []any
|
||||
}{
|
||||
{`INSERT INTO objects(uuid,kind,created_at) VALUES (` + r.p(1) + `,'page',` + r.p(2) + `)`, []any{id, now}},
|
||||
{`INSERT INTO object_origins(object_uuid,domain_uuid,created_at) VALUES (` + r.p(1) + `,` + r.p(2) + `,` + r.p(3) + `)`, []any{id, domain, now}},
|
||||
{`INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES (` + r.p(1) + `,` + r.p(2) + `,` + r.p(3) + `,` + r.p(4) + `,1)`, []any{domain, p, id, now}},
|
||||
} {
|
||||
if _, err := tx.ExecContext(ctx, q.sql, q.args...); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
}
|
||||
if r.applicationNamespace {
|
||||
return r.appendPageOutbox(ctx, tx, domain, p, id)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// CreateObject records provenance for the explicit create/save/bind API.
|
||||
// It does not grant authority over an arbitrary unbound legacy Object.
|
||||
func (r *Repository) CreateObject(ctx context.Context, domain, kind string) (string, error) {
|
||||
if domain == "" || strings.TrimSpace(kind) == "" {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
id := uuid.NewString()
|
||||
err := r.write(ctx, "domain/"+domain, func(tx *sql.Tx) error {
|
||||
now := time.Now().Unix()
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO objects(uuid,kind,created_at) VALUES(`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`)`, id, kind, now); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO object_origins(object_uuid,domain_uuid,created_at) VALUES(`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`)`, id, domain, now)
|
||||
return r.failure(err)
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (r *Repository) ResolvePath(ctx context.Context, domain, path string) (string, error) {
|
||||
var id string
|
||||
err := r.database.QueryRowContext(ctx, `SELECT object_uuid FROM paths WHERE domain_uuid=`+r.p(1)+` AND path=`+r.p(2), domain, path).Scan(&id)
|
||||
return id, r.failure(err)
|
||||
}
|
||||
|
||||
// BindPath creates an alias without replacing an existing binding. Cross-domain
|
||||
// sharing requires a separate explicit authorization/import contract.
|
||||
func (r *Repository) BindPath(ctx context.Context, domain, path, object string) error {
|
||||
p, err := canonicalPath(path)
|
||||
if err != nil || domain == "" || object == "" {
|
||||
return ErrInvalid
|
||||
}
|
||||
return r.write(ctx, "domain/"+domain, func(tx *sql.Tx) error {
|
||||
var count int
|
||||
if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM object_origins WHERE object_uuid=`+r.p(1)+` AND domain_uuid=`+r.p(2), object, domain).Scan(&count); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if count == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES (`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`,`+r.p(4)+`,0)`, domain, p, object, time.Now().Unix())
|
||||
return r.failure(err)
|
||||
})
|
||||
}
|
||||
|
||||
// RenameTree moves both real and virtual folders without LIKE wildcard or
|
||||
// case-folding semantics. It does not merge into occupied destinations. City,
|
||||
// ACL, activity outbox and cache invalidation must be coordinated by the future
|
||||
// application-level transaction; do not substitute this for legacy UI rename yet.
|
||||
func (r *Repository) RenameTree(ctx context.Context, domain, source, destination string) error {
|
||||
from, e1 := canonicalPath(source)
|
||||
to, e2 := canonicalPath(destination)
|
||||
if e1 != nil || e2 != nil || domain == "" || from == "/" || to == "/" || from == to || strings.HasPrefix(to, from+"/") || strings.HasPrefix(from, to+"/") {
|
||||
return ErrInvalid
|
||||
}
|
||||
return r.write(ctx, "domain/"+domain, func(tx *sql.Tx) error {
|
||||
var occupied int
|
||||
// Binary/C collation makes [prefix+'/', prefix+'0') an exact,
|
||||
// indexable descendant range, including Unicode and literal %/_ names.
|
||||
err := tx.QueryRowContext(ctx, `SELECT 1 FROM paths WHERE domain_uuid=`+r.p(1)+` AND (path=`+r.p(2)+` OR (path>=`+r.p(3)+` AND path<`+r.p(4)+`)) LIMIT 1`, domain, to, to+"/", to+"0").Scan(&occupied)
|
||||
if err == nil {
|
||||
return ErrConflict
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
return r.failure(err)
|
||||
}
|
||||
// A single SQL update avoids loading the subtree or issuing one query
|
||||
// per path. substr counts characters (not UTF-8 bytes) on both DBs.
|
||||
result, err := tx.ExecContext(ctx, `UPDATE paths SET path=`+r.p(1)+` || substr(path,`+r.p(2)+`) WHERE domain_uuid=`+r.p(3)+` AND (path=`+r.p(4)+` OR (path>=`+r.p(5)+` AND path<`+r.p(6)+`))`, to, len([]rune(from))+1, domain, from, from+"/", from+"0")
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
count, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if count == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
type Revision struct {
|
||||
UUID string
|
||||
ObjectUUID string
|
||||
DocumentHash string
|
||||
Language string
|
||||
ContentType string
|
||||
Filename string
|
||||
Message string
|
||||
CreatedAtMS int64
|
||||
}
|
||||
|
||||
func normalizedLanguage(raw string) (string, error) {
|
||||
if strings.TrimSpace(raw) == "" {
|
||||
return "und", nil
|
||||
}
|
||||
tag, err := language.Parse(raw)
|
||||
if err != nil {
|
||||
return "", ErrInvalid
|
||||
}
|
||||
return tag.String(), nil
|
||||
}
|
||||
|
||||
const revisionColumns = `uuid,object_uuid,document_hash,lang,content_type,filename,message,created_at_ms`
|
||||
|
||||
// Application create/save/bind records creation provenance before any Path.
|
||||
// Provenance ceases to grant scope as soon as any live binding exists.
|
||||
// Expressions are owned SQL identifiers/placeholders, never request strings.
|
||||
func (r *Repository) objectDomainScope(object, domain string) string {
|
||||
scope := `SELECT domain_uuid FROM paths WHERE object_uuid=` + object
|
||||
if r.applicationNamespace {
|
||||
scope += ` UNION ALL SELECT domain_uuid FROM object_origins WHERE object_uuid=` + object + ` AND NOT EXISTS(SELECT 1 FROM paths WHERE object_uuid=` + object + `)`
|
||||
}
|
||||
return domain + ` IN (` + scope + `)`
|
||||
}
|
||||
|
||||
func scanRevision(row interface{ Scan(...any) error }) (Revision, error) {
|
||||
var v Revision
|
||||
err := row.Scan(&v.UUID, &v.ObjectUUID, &v.DocumentHash, &v.Language, &v.ContentType, &v.Filename, &v.Message, &v.CreatedAtMS)
|
||||
return v, err
|
||||
}
|
||||
|
||||
func (r *Repository) Head(ctx context.Context, domain, object, lang string) (Revision, error) {
|
||||
l, err := normalizedLanguage(lang)
|
||||
if err != nil {
|
||||
return Revision{}, err
|
||||
}
|
||||
v, err := scanRevision(r.database.QueryRowContext(ctx, `SELECT `+revisionColumns+` FROM revisions r WHERE object_uuid=`+r.p(1)+` AND lang=`+r.p(2)+` AND (`+r.objectDomainScope("r.object_uuid", r.p(3))+`) ORDER BY created_at_ms DESC,uuid DESC LIMIT 1`, object, l, domain))
|
||||
return v, r.failure(err)
|
||||
}
|
||||
|
||||
func (r *Repository) GetRevision(ctx context.Context, domain, revisionUUID string) (Revision, error) {
|
||||
v, err := scanRevision(r.database.QueryRowContext(ctx, `SELECT `+revisionColumns+` FROM revisions r WHERE uuid=`+r.p(1)+` AND (`+r.objectDomainScope("r.object_uuid", r.p(2))+`)`, revisionUUID, domain))
|
||||
return v, r.failure(err)
|
||||
}
|
||||
|
||||
// RestoreRevision appends a new revision pointing to the historical Document;
|
||||
// it never rewrites old revision metadata or bypasses the exact-language CAS.
|
||||
func (r *Repository) RestoreRevision(ctx context.Context, domain, revisionUUID, expected, message string) (Revision, error) {
|
||||
v, err := r.GetRevision(ctx, domain, revisionUUID)
|
||||
if err != nil {
|
||||
return Revision{}, err
|
||||
}
|
||||
v.Message = message
|
||||
refs, err := r.revisionReferences(ctx, domain, revisionUUID)
|
||||
if err != nil {
|
||||
return Revision{}, err
|
||||
}
|
||||
return r.appendRevision(ctx, domain, v, expected, refs, "")
|
||||
}
|
||||
|
||||
// AppendRevision uses exact-language compare-and-swap. Empty expected means
|
||||
// there is no previous revision in this language. All writes use this contract;
|
||||
// monotonic timestamps remove dependence on SQLite's hidden rowid ordering.
|
||||
func (r *Repository) AppendRevision(ctx context.Context, domain string, v Revision, expected string) (Revision, error) {
|
||||
return r.appendRevision(ctx, domain, v, expected, nil, "")
|
||||
}
|
||||
|
||||
func (r *Repository) appendRevision(ctx context.Context, domain string, v Revision, expected string, refs []string, actor string) (Revision, error) {
|
||||
return r.appendRevisionExpected(ctx, domain, v, &expected, refs, actor)
|
||||
}
|
||||
|
||||
func (r *Repository) appendRevisionExpected(ctx context.Context, domain string, v Revision, expected *string, refs []string, actor string) (Revision, error) {
|
||||
lang, err := normalizedLanguage(v.Language)
|
||||
if err != nil || !validHash(v.DocumentHash) || v.ObjectUUID == "" || strings.TrimSpace(v.ContentType) == "" {
|
||||
return Revision{}, ErrInvalid
|
||||
}
|
||||
v.UUID = uuid.NewString()
|
||||
v.Language = lang
|
||||
err = r.write(ctx, "domain/"+domain, func(tx *sql.Tx) error {
|
||||
// Always acquire Domain before Object. This also protects per-Domain
|
||||
// outbox sequence allocation for different objects and languages.
|
||||
if err := r.lock(ctx, tx, "object/"+v.ObjectUUID); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
var bound bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM objects o WHERE o.uuid=`+r.p(1)+` AND (`+r.objectDomainScope("o.uuid", r.p(2))+`))`, v.ObjectUUID, domain).Scan(&bound); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if !bound {
|
||||
return ErrNotFound
|
||||
}
|
||||
var current string
|
||||
var previous int64
|
||||
err := tx.QueryRowContext(ctx, `SELECT uuid,created_at_ms FROM revisions WHERE object_uuid=`+r.p(1)+` AND lang=`+r.p(2)+` ORDER BY created_at_ms DESC,uuid DESC LIMIT 1`, v.ObjectUUID, lang).Scan(¤t, &previous)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return r.failure(err)
|
||||
}
|
||||
if expected != nil && current != *expected {
|
||||
return ErrConflict
|
||||
}
|
||||
if previous == math.MaxInt64 {
|
||||
return ErrInvalid
|
||||
}
|
||||
v.CreatedAtMS = time.Now().UnixMilli()
|
||||
if v.CreatedAtMS <= previous {
|
||||
v.CreatedAtMS = previous + 1
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO revisions(uuid,object_uuid,document_hash,lang,content_type,filename,message,created_at_ms,created_at,body) VALUES (`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`,`+r.p(4)+`,`+r.p(5)+`,`+r.p(6)+`,`+r.p(7)+`,`+r.p(8)+`,`+r.p(9)+`,'')`, v.UUID, v.ObjectUUID, v.DocumentHash, v.Language, v.ContentType, v.Filename, v.Message, v.CreatedAtMS, v.CreatedAtMS/1000)
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, hash := range refs {
|
||||
if !validHash(hash) {
|
||||
return ErrInvalid
|
||||
}
|
||||
if seen[hash] {
|
||||
continue
|
||||
}
|
||||
seen[hash] = true
|
||||
if _, err := tx.ExecContext(ctx, `INSERT INTO revision_document_refs(revision_uuid,document_hash) VALUES (`+r.p(1)+`,`+r.p(2)+`)`, v.UUID, hash); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
}
|
||||
return r.appendRevisionOutbox(ctx, tx, domain, actor, v)
|
||||
})
|
||||
if err != nil {
|
||||
return Revision{}, err
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
func testRepository(t *testing.T, backend Backend) *Repository {
|
||||
t.Helper()
|
||||
var database *sql.DB
|
||||
var err error
|
||||
if backend == SQLite {
|
||||
database, err = db.Open(filepath.Join(t.TempDir(), "metadata.db"))
|
||||
} else {
|
||||
dsn := os.Getenv("PAGESWIKI_TEST_POSTGRES_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("set PAGESWIKI_TEST_POSTGRES_DSN to a disposable PostgreSQL URL")
|
||||
}
|
||||
u, e := url.Parse(dsn)
|
||||
if e != nil || (u.Scheme != "postgres" && u.Scheme != "postgresql") {
|
||||
t.Fatal("test DSN must be PostgreSQL URL")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
root, e := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: dsn, AllowInsecure: true})
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
schema := "pageswiki_metadata_test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
if _, e = root.ExecContext(ctx, `CREATE SCHEMA "`+schema+`"`); e != nil {
|
||||
root.Close()
|
||||
t.Fatal("create synthetic test schema failed")
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if _, e := root.ExecContext(ctx, `DROP SCHEMA "`+schema+`" CASCADE`); e != nil {
|
||||
t.Error("drop synthetic test schema failed")
|
||||
}
|
||||
root.Close()
|
||||
})
|
||||
query := u.Query()
|
||||
query.Set("search_path", schema)
|
||||
u.RawQuery = query.Encode()
|
||||
database, err = db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: u.String(), AllowInsecure: true})
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
r, err := New(database, backend)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.InitializeFresh(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.InitializeFresh(context.Background()); err != nil {
|
||||
t.Fatal("repeated initialization", err)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func hash(s string) string { v := sha256.Sum256([]byte(s)); return hex.EncodeToString(v[:]) }
|
||||
|
||||
func TestMetadataContract(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
mustPage := func(domain, path string) string {
|
||||
t.Helper()
|
||||
id, err := r.CreatePage(ctx, domain, path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
id := mustPage("domain-a", "/学区%_/通り/本")
|
||||
other := mustPage("domain-b", "/学区%_/通り/本")
|
||||
if id == other {
|
||||
t.Fatal("object UUID reused")
|
||||
}
|
||||
if got, err := r.ResolvePath(ctx, "domain-a", "/学区%_/通り/本"); err != nil || got != id {
|
||||
t.Fatal("path resolution", err)
|
||||
}
|
||||
if _, err := r.ResolvePath(ctx, "unknown", "/学区%_/通り/本"); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatal("domain isolation", err)
|
||||
}
|
||||
if _, err := r.CreatePage(ctx, "domain-a", "/学区%_/通り/本"); !errors.Is(err, ErrConflict) {
|
||||
t.Fatal("duplicate path accepted", err)
|
||||
}
|
||||
var count int
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM objects`).Scan(&count); err != nil || count != 2 {
|
||||
t.Fatal("failed create left orphan object", err, count)
|
||||
}
|
||||
if err := r.BindPath(ctx, "domain-a", "/alias", id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.BindPath(ctx, "domain-b", "/stolen", id); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatal("foreign alias accepted", err)
|
||||
}
|
||||
mustPage("domain-a", "/学区XX/通り/本")
|
||||
mustPage("domain-a", "/学区%_suffix/本")
|
||||
if err := r.RenameTree(ctx, "domain-a", "/学区%_", "/新しい学区"); err != nil {
|
||||
t.Fatal("virtual folder rename", err)
|
||||
}
|
||||
if got, err := r.ResolvePath(ctx, "domain-a", "/新しい学区/通り/本"); err != nil || got != id {
|
||||
t.Fatal("UUID not preserved", err)
|
||||
}
|
||||
for _, path := range []string{"/学区XX/通り/本", "/学区%_suffix/本"} {
|
||||
if _, err := r.ResolvePath(ctx, "domain-a", path); err != nil {
|
||||
t.Fatal("wildcard/boundary changed unrelated path", err)
|
||||
}
|
||||
}
|
||||
if got, err := r.ResolvePath(ctx, "domain-b", "/学区%_/通り/本"); err != nil || got != other {
|
||||
t.Fatal("rename crossed domain", err)
|
||||
}
|
||||
mustPage("domain-a", "/occupied/something")
|
||||
if err := r.RenameTree(ctx, "domain-a", "/新しい学区", "/occupied"); !errors.Is(err, ErrConflict) {
|
||||
t.Fatal("destination merged", err)
|
||||
}
|
||||
if _, err := r.ResolvePath(ctx, "domain-a", "/新しい学区/通り/本"); err != nil {
|
||||
t.Fatal("failed move changed source", err)
|
||||
}
|
||||
if err := r.RenameTree(ctx, "domain-a", "/新しい学区", "/新しい学区/inside"); !errors.Is(err, ErrInvalid) {
|
||||
t.Fatal("recursive destination accepted")
|
||||
}
|
||||
mustPage("domain-a", "/Case")
|
||||
mustPage("domain-a", "/case")
|
||||
real := mustPage("domain-a", "/real")
|
||||
child := mustPage("domain-a", "/real/child")
|
||||
if err := r.RenameTree(ctx, "domain-a", "/real", "/moved-real"); err != nil {
|
||||
t.Fatal("real folder move", err)
|
||||
}
|
||||
for path, id := range map[string]string{"/moved-real": real, "/moved-real/child": child} {
|
||||
if got, err := r.ResolvePath(ctx, "domain-a", path); err != nil || got != id {
|
||||
t.Fatal("real subtree identity", err)
|
||||
}
|
||||
}
|
||||
v := Revision{ObjectUUID: id, DocumentHash: hash("first"), Language: "ja", ContentType: "text/html", Message: "first"}
|
||||
first, err := r.AppendRevision(ctx, "domain-a", v, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.Head(ctx, "domain-b", id, "ja"); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatal("foreign revision readable", err)
|
||||
}
|
||||
if _, err := r.Head(ctx, "domain-a", id, "en"); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatal("language fallback leaked into exact head", err)
|
||||
}
|
||||
if _, err := r.AppendRevision(ctx, "domain-a", v, ""); !errors.Is(err, ErrConflict) {
|
||||
t.Fatal("stale revision accepted", err)
|
||||
}
|
||||
v.Language = "en"
|
||||
if _, err := r.AppendRevision(ctx, "domain-a", v, ""); err != nil {
|
||||
t.Fatal("translation creation", err)
|
||||
}
|
||||
v.Language = "ja"
|
||||
v.DocumentHash = hash("updated")
|
||||
// Independent Repository objects simulate writers that cannot share a Go mutex.
|
||||
wins := concurrent(t, 6, func() error {
|
||||
peer, _ := New(r.database, backend)
|
||||
_, err := peer.AppendRevision(ctx, "domain-a", v, first.UUID)
|
||||
return err
|
||||
})
|
||||
if wins != 1 {
|
||||
t.Fatal("CAS winners", wins)
|
||||
}
|
||||
head, err := r.Head(ctx, "domain-a", id, "ja")
|
||||
if err != nil || head.UUID == first.UUID || head.CreatedAtMS <= first.CreatedAtMS {
|
||||
t.Fatal("head ordering", err)
|
||||
}
|
||||
var originalHash string
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT document_hash FROM revisions WHERE uuid=`+r.p(1), first.UUID).Scan(&originalHash); err != nil || originalHash != first.DocumentHash {
|
||||
t.Fatal("immutable revision changed", err)
|
||||
}
|
||||
if _, err := r.GetRevision(ctx, "domain-b", first.UUID); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatal("foreign revision UUID readable", err)
|
||||
}
|
||||
restored, err := r.RestoreRevision(ctx, "domain-a", first.UUID, head.UUID, "restore old document")
|
||||
if err != nil || restored.UUID == first.UUID || restored.DocumentHash != first.DocumentHash || restored.CreatedAtMS <= head.CreatedAtMS {
|
||||
t.Fatal("restore did not append immutable revision", err)
|
||||
}
|
||||
if _, err := r.RestoreRevision(ctx, "domain-a", first.UUID, head.UUID, "stale restore"); !errors.Is(err, ErrConflict) {
|
||||
t.Fatal("restore ignored head conflict", err)
|
||||
}
|
||||
a := Asset{DomainUUID: "domain-a", Address: "asset:images:cover.png", HeadDocumentHash: hash("image1"), ContentType: "image/png", SizeBytes: 10}
|
||||
initial, err := r.PutAsset(ctx, a, "", "actor")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.GetAsset(ctx, "domain-b", a.Address); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatal("foreign asset readable", err)
|
||||
}
|
||||
a.HeadDocumentHash = hash("image2")
|
||||
if wins := concurrent(t, 6, func() error {
|
||||
peer, _ := New(r.database, backend)
|
||||
_, err := peer.PutAsset(ctx, a, initial.HeadDocumentHash, "actor")
|
||||
return err
|
||||
}); wins != 1 {
|
||||
t.Fatal("asset CAS winners", wins)
|
||||
}
|
||||
current, err := r.GetAsset(ctx, "domain-a", a.Address)
|
||||
if err != nil || current.ObjectUUID != initial.ObjectUUID || current.UpdatedAtMS <= initial.UpdatedAtMS {
|
||||
t.Fatal("asset identity/order", err)
|
||||
}
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM asset_head_history`).Scan(&count); err != nil || count != 2 {
|
||||
t.Fatal("history not atomic", err, count)
|
||||
}
|
||||
a.DomainUUID = "domain-b"
|
||||
if _, err := r.PutAsset(ctx, a, "", "actor"); err != nil {
|
||||
t.Fatal("asset namespace not domain-scoped", err)
|
||||
}
|
||||
if _, err := r.AppendRevision(ctx, "domain-a", Revision{ObjectUUID: id, DocumentHash: "bad", ContentType: "text/html"}, head.UUID); !errors.Is(err, ErrInvalid) {
|
||||
t.Fatal("invalid hash accepted", err)
|
||||
}
|
||||
canceled, stop := context.WithCancel(ctx)
|
||||
stop()
|
||||
if _, err := r.CreatePage(canceled, "domain-a", "/canceled"); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("cancellation lost", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataInputValidation(t *testing.T) {
|
||||
for _, path := range []string{"", "relative", "//", "///", "/a//b", "/a/../b", "/a/./b", "/a\x00b", "/a\nb", "/a\xffb"} {
|
||||
if _, err := canonicalPath(path); !errors.Is(err, ErrInvalid) {
|
||||
t.Fatalf("invalid path accepted: %q", path)
|
||||
}
|
||||
}
|
||||
for _, path := range []string{"/", "/日本語/📚", "/name%_", "/a/b/"} {
|
||||
if _, err := canonicalPath(path); err != nil {
|
||||
t.Fatalf("valid path rejected: %q", path)
|
||||
}
|
||||
}
|
||||
if _, err := New(nil, SQLite); !errors.Is(err, ErrInvalid) {
|
||||
t.Fatal("nil database accepted")
|
||||
}
|
||||
// Do not expose data carried by a wrapped cancellation error.
|
||||
r := Repository{}
|
||||
err := r.failure(errors.Join(errors.New("secret"), context.Canceled))
|
||||
if !errors.Is(err, context.Canceled) || strings.Contains(err.Error(), "secret") {
|
||||
t.Fatal("unsafe cancellation error")
|
||||
}
|
||||
}
|
||||
|
||||
func concurrent(t *testing.T, n int, fn func() error) int {
|
||||
t.Helper()
|
||||
var wg sync.WaitGroup
|
||||
results := make(chan error, n)
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); results <- fn() }()
|
||||
}
|
||||
wg.Wait()
|
||||
close(results)
|
||||
wins := 0
|
||||
for err := range results {
|
||||
if err == nil {
|
||||
wins++
|
||||
} else if !errors.Is(err, ErrConflict) {
|
||||
t.Fatal("unexpected concurrent write error", err)
|
||||
}
|
||||
}
|
||||
return wins
|
||||
}
|
||||
|
||||
func TestMetadataRefusesLegacyAdoption(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx := context.Background()
|
||||
if _, err := r.database.ExecContext(ctx, `DELETE FROM schema_migration_steps WHERE component='core-metadata'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := r.CreatePage(ctx, "test", "/preserved"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.InitializeFresh(ctx); err == nil {
|
||||
t.Fatal("unversioned schema silently adopted")
|
||||
}
|
||||
if _, err := r.ResolvePath(ctx, "test", "/preserved"); err != nil {
|
||||
t.Fatal("existing data changed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataAssetHistoryFailureIsAtomic(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx := context.Background()
|
||||
a := Asset{DomainUUID: "test", Address: "asset:images:original.png", HeadDocumentHash: hash("original"), ContentType: "image/png", SizeBytes: 8}
|
||||
original, err := r.PutAsset(ctx, a, "", "actor")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Force a history write failure after the asset HEAD write. The
|
||||
// synthetic unique index is solely fault injection, not the real schema.
|
||||
if _, err := r.database.ExecContext(ctx, `CREATE UNIQUE INDEX synthetic_history_failure ON asset_head_history(domain_uuid)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a.HeadDocumentHash = hash("changed")
|
||||
if _, err := r.PutAsset(ctx, a, original.HeadDocumentHash, "actor"); !errors.Is(err, ErrConflict) {
|
||||
t.Fatal("injected history failure ignored", err)
|
||||
}
|
||||
got, err := r.GetAsset(ctx, "test", a.Address)
|
||||
if err != nil || got.HeadDocumentHash != original.HeadDocumentHash || got.UpdatedAtMS != original.UpdatedAtMS {
|
||||
t.Fatal("partial HEAD update", err)
|
||||
}
|
||||
a.Address = "asset:images:new.png"
|
||||
if _, err := r.PutAsset(ctx, a, "", "actor"); !errors.Is(err, ErrConflict) {
|
||||
t.Fatal("injected creation failure ignored", err)
|
||||
}
|
||||
if _, err := r.GetAsset(ctx, "test", a.Address); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatal("partial asset created", err)
|
||||
}
|
||||
for _, table := range []string{"objects", "object_origins", "assets", "asset_head_history"} {
|
||||
var count int
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM `+table).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("rollback failed", table, count, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetadataDetectsMissingColumnWithoutRepair(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx := context.Background()
|
||||
if _, err := r.database.ExecContext(ctx, `ALTER TABLE revisions RENAME COLUMN filename TO misplaced_filename`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.InitializeFresh(ctx); err == nil {
|
||||
t.Fatal("missing column ignored")
|
||||
}
|
||||
var value string
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT misplaced_filename FROM revisions LIMIT 1`).Scan(&value); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("drift silently repaired", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// appendRevisionOutbox requires the Domain write lock. Sequence allocation uses
|
||||
// small per-type watermarks, not MAX over the ever-growing event journal.
|
||||
// This writes pending events only; delivery/leases remain a separate port.
|
||||
func (r *Repository) appendRevisionOutbox(ctx context.Context, tx *sql.Tx, domain, actor string, v Revision) error {
|
||||
payload, err := json.Marshal(map[string]string{"revision_uuid": v.UUID, "object_uuid": v.ObjectUUID, "document_hash": v.DocumentHash, "content_type": v.ContentType, "filename": v.Filename})
|
||||
if err != nil {
|
||||
return ErrInvalid
|
||||
}
|
||||
return r.appendOutbox(ctx, tx, domain, actor, "revision.created", "revision", v.UUID, string(payload))
|
||||
}
|
||||
|
||||
func (r *Repository) appendPageOutbox(ctx context.Context, tx *sql.Tx, domain, path, object string) error {
|
||||
payload, err := json.Marshal(map[string]string{"path": path, "object_uuid": object})
|
||||
if err != nil {
|
||||
return ErrInvalid
|
||||
}
|
||||
return r.appendOutbox(ctx, tx, domain, "", "page.created", "page", object, string(payload))
|
||||
}
|
||||
|
||||
// Caller holds the Domain lock; metadata and the pending event commit together.
|
||||
func (r *Repository) appendOutbox(ctx context.Context, tx *sql.Tx, domain, actor, eventType, targetKind, targetUUID, payload string) error {
|
||||
var last int64
|
||||
if err := tx.QueryRowContext(ctx, `SELECT COALESCE(MAX(last_sequence),0) FROM activity_outbox_heads WHERE domain_uuid=`+r.p(1), domain).Scan(&last); err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
if last == math.MaxInt64 {
|
||||
return ErrInvalid
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO activity_outbox(uuid,domain_uuid,sequence,event_type,actor_uuid,target_kind,target_uuid,payload,idempotency_key,next_attempt_ms,created_at_ms) VALUES (`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`,`+r.p(4)+`,`+r.p(5)+`,`+r.p(6)+`,`+r.p(7)+`,`+r.p(8)+`,`+r.p(9)+`,`+r.p(10)+`,`+r.p(11)+`)`, uuid.NewString(), domain, last+1, eventType, actor, targetKind, targetUUID, payload, eventType+":"+targetUUID, now, now)
|
||||
if err != nil {
|
||||
return r.failure(err)
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO activity_outbox_heads(domain_uuid,event_type,last_sequence) VALUES (`+r.p(1)+`,`+r.p(2)+`,`+r.p(3)+`) ON CONFLICT(domain_uuid,event_type) DO UPDATE SET last_sequence=excluded.last_sequence`, domain, eventType, last+1)
|
||||
return r.failure(err)
|
||||
}
|
||||
|
||||
func (r *Repository) revisionReferences(ctx context.Context, domain, revisionUUID string) ([]string, error) {
|
||||
rows, err := r.database.QueryContext(ctx, `SELECT document_hash FROM revision_document_refs WHERE revision_uuid=`+r.p(1)+` AND EXISTS (SELECT 1 FROM revisions r WHERE r.uuid=`+r.p(2)+` AND (`+r.objectDomainScope("r.object_uuid", r.p(3))+`)) ORDER BY document_hash`, revisionUUID, revisionUUID, domain)
|
||||
if err != nil {
|
||||
return nil, r.failure(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
var hashes []string
|
||||
for rows.Next() {
|
||||
var h string
|
||||
if err := rows.Scan(&h); err != nil {
|
||||
return nil, r.failure(err)
|
||||
}
|
||||
hashes = append(hashes, h)
|
||||
}
|
||||
return hashes, r.failure(rows.Err())
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
// InitializeFresh bootstraps an empty database/schema and is idempotent after
|
||||
// its first successful migration. Existing unversioned application tables are
|
||||
// deliberately rejected, not adopted or converted. Production import and
|
||||
// legacy schema reconciliation require a separate, verified migration plan.
|
||||
func (r *Repository) InitializeFresh(ctx context.Context) error {
|
||||
sqliteSteps, postgresSteps := r.migrationDefinitions()
|
||||
if r.backend == PostgreSQL {
|
||||
return db.ApplyPostgreSQLMigrations(ctx, r.database, "core-metadata", postgresSteps)
|
||||
}
|
||||
return db.ApplySQLiteMigrations(ctx, r.database, "core-metadata", sqliteSteps)
|
||||
}
|
||||
|
||||
// Keep the exact released definitions shared by initialization and offline
|
||||
// compatibility inspection. Neither inspection nor copying runs migrations.
|
||||
func (r *Repository) migrationDefinitions() ([]db.SQLMigration, []db.PostgreSQLMigration) {
|
||||
identity := "INTEGER PRIMARY KEY AUTOINCREMENT"
|
||||
addressType := "TEXT"
|
||||
if r.backend == PostgreSQL {
|
||||
identity = "BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY"
|
||||
// Path/asset identity must not depend on database locale or an ICU
|
||||
// nondeterministic collation chosen by the database administrator.
|
||||
addressType = `TEXT COLLATE "C"`
|
||||
}
|
||||
statements := []string{
|
||||
`CREATE TABLE objects(uuid TEXT PRIMARY KEY,kind TEXT NOT NULL,created_at BIGINT NOT NULL)`,
|
||||
`CREATE TABLE object_origins(object_uuid TEXT PRIMARY KEY REFERENCES objects(uuid),domain_uuid TEXT NOT NULL,created_at BIGINT NOT NULL)`,
|
||||
`CREATE TABLE paths(domain_uuid TEXT NOT NULL,path ` + addressType + ` NOT NULL,object_uuid TEXT NOT NULL REFERENCES objects(uuid),created_at BIGINT NOT NULL,is_primary INTEGER NOT NULL DEFAULT 0,PRIMARY KEY(domain_uuid,path))`,
|
||||
`CREATE INDEX idx_paths_domain_object ON paths(domain_uuid,object_uuid)`,
|
||||
`CREATE TABLE revisions(uuid TEXT PRIMARY KEY,object_uuid TEXT NOT NULL REFERENCES objects(uuid),document_hash TEXT NOT NULL,lang TEXT NOT NULL,content_type TEXT NOT NULL,filename TEXT NOT NULL DEFAULT '',message TEXT NOT NULL DEFAULT '',created_at_ms BIGINT NOT NULL,created_at BIGINT NOT NULL,body TEXT NOT NULL DEFAULT '')`,
|
||||
`CREATE INDEX idx_revisions_metadata_head ON revisions(object_uuid,lang,created_at_ms DESC,uuid DESC)`,
|
||||
`CREATE TABLE assets(domain_uuid TEXT NOT NULL,address ` + addressType + ` NOT NULL,object_uuid TEXT NOT NULL UNIQUE REFERENCES objects(uuid),head_document_hash TEXT NOT NULL,content_type TEXT NOT NULL,filename TEXT NOT NULL DEFAULT '',size_bytes BIGINT NOT NULL CHECK(size_bytes>=0),created_at_ms BIGINT NOT NULL,updated_at_ms BIGINT NOT NULL,PRIMARY KEY(domain_uuid,address))`,
|
||||
`CREATE TABLE asset_head_history(id ` + identity + `,domain_uuid TEXT NOT NULL,address TEXT NOT NULL,action TEXT NOT NULL,document_hash TEXT NOT NULL,content_type TEXT NOT NULL,filename TEXT NOT NULL,size_bytes BIGINT NOT NULL CHECK(size_bytes>=0),actor_uuid TEXT NOT NULL,created_at_ms BIGINT NOT NULL)`,
|
||||
`CREATE INDEX idx_asset_metadata_history ON asset_head_history(domain_uuid,address,id)`,
|
||||
}
|
||||
// Minimal shape checks also run on subsequent initialization. Full catalog
|
||||
// assertions for types, collation, constraints and indexes are a separate
|
||||
// adoption gate before this schema can replace a legacy production store.
|
||||
assertions := []string{
|
||||
`SELECT uuid,kind,created_at FROM objects WHERE 1=0`,
|
||||
`SELECT object_uuid,domain_uuid,created_at FROM object_origins WHERE 1=0`,
|
||||
`SELECT domain_uuid,path,object_uuid,created_at,is_primary FROM paths WHERE 1=0`,
|
||||
`SELECT ` + revisionColumns + `,created_at,body FROM revisions WHERE 1=0`,
|
||||
`SELECT ` + assetColumns + ` FROM assets WHERE 1=0`,
|
||||
`SELECT id,domain_uuid,address,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms FROM asset_head_history WHERE 1=0`,
|
||||
}
|
||||
referenceStatements := []string{
|
||||
`CREATE TABLE revision_document_refs(revision_uuid TEXT NOT NULL REFERENCES revisions(uuid),document_hash TEXT NOT NULL,PRIMARY KEY(revision_uuid,document_hash))`,
|
||||
`CREATE INDEX idx_metadata_document_refs ON revision_document_refs(document_hash,revision_uuid)`,
|
||||
`CREATE TABLE activity_outbox(uuid TEXT PRIMARY KEY,domain_uuid TEXT NOT NULL,sequence BIGINT NOT NULL,event_type TEXT NOT NULL,actor_uuid TEXT NOT NULL DEFAULT '',target_kind TEXT NOT NULL DEFAULT '',target_uuid TEXT NOT NULL DEFAULT '',payload TEXT NOT NULL DEFAULT '',idempotency_key TEXT NOT NULL,status TEXT NOT NULL DEFAULT 'pending',attempts INTEGER NOT NULL DEFAULT 0,next_attempt_ms BIGINT NOT NULL,last_error TEXT NOT NULL DEFAULT '',created_at_ms BIGINT NOT NULL,claimed_at_ms BIGINT NOT NULL DEFAULT 0,UNIQUE(domain_uuid,sequence),UNIQUE(domain_uuid,idempotency_key))`,
|
||||
`CREATE TABLE activity_outbox_heads(domain_uuid TEXT NOT NULL,event_type TEXT NOT NULL,last_sequence BIGINT NOT NULL,PRIMARY KEY(domain_uuid,event_type))`,
|
||||
`CREATE INDEX idx_metadata_outbox_pending ON activity_outbox(status,next_attempt_ms,domain_uuid,sequence)`,
|
||||
}
|
||||
currentAssertions := append(append([]string(nil), assertions...),
|
||||
`SELECT revision_uuid,document_hash FROM revision_document_refs WHERE 1=0`,
|
||||
`SELECT uuid,domain_uuid,sequence,event_type,actor_uuid,target_kind,target_uuid,payload,idempotency_key,status,attempts,next_attempt_ms,last_error,created_at_ms,claimed_at_ms FROM activity_outbox WHERE 1=0`,
|
||||
`SELECT domain_uuid,event_type,last_sequence FROM activity_outbox_heads WHERE 1=0`)
|
||||
if r.backend == PostgreSQL {
|
||||
return nil, []db.PostgreSQLMigration{
|
||||
{Version: 1, Name: "initial portable core", Statements: statements, Assertions: assertions},
|
||||
{Version: 2, Name: "revision references and outbox", Statements: referenceStatements, Assertions: currentAssertions},
|
||||
}
|
||||
}
|
||||
return []db.SQLMigration{
|
||||
{Version: 1, Name: "initial portable core", Statements: statements, Assertions: assertions},
|
||||
{Version: 2, Name: "revision references and outbox", Statements: referenceStatements, Assertions: currentAssertions},
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestReferenceMigrationPreservesV1AndRetriesAtomically(t *testing.T) {
|
||||
for _, backend := range []Backend{SQLite, PostgreSQL} {
|
||||
t.Run(string(backend), func(t *testing.T) {
|
||||
r := testRepository(t, backend)
|
||||
ctx := context.Background()
|
||||
id, err := r.CreatePage(ctx, "test", "/existing")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Reconstruct the preceding portable schema using its unchanged v1
|
||||
// ledger. None of this targets legacy or production application data.
|
||||
for _, q := range []string{`DROP TABLE revision_document_refs`, `DROP TABLE activity_outbox`, `DROP TABLE activity_outbox_heads`, `DELETE FROM schema_migration_steps WHERE component='core-metadata' AND version=2`} {
|
||||
if _, err := r.database.ExecContext(ctx, q); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
_, err = r.database.ExecContext(ctx, `INSERT INTO revisions(uuid,object_uuid,document_hash,lang,content_type,created_at_ms,created_at) VALUES ('existing-revision',`+r.p(1)+`,`+r.p(2)+`,'und','text/plain',1000,1)`, id, hash("old"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// An unrelated existing table must stop adoption. The earlier DDL in
|
||||
// the same migration must roll back and permit an explicit retry.
|
||||
if _, err := r.database.ExecContext(ctx, `CREATE TABLE activity_outbox(unrelated INTEGER)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.InitializeFresh(ctx); err == nil {
|
||||
t.Fatal("unmanaged outbox table adopted")
|
||||
}
|
||||
var count int
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM revision_document_refs`).Scan(&count); err == nil {
|
||||
t.Fatal("partial v2 DDL remained")
|
||||
}
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM schema_migration_steps WHERE component='core-metadata' AND version=2`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("failed migration recorded", err)
|
||||
}
|
||||
if _, err := r.database.ExecContext(ctx, `DROP TABLE activity_outbox`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := r.InitializeFresh(ctx); err != nil {
|
||||
t.Fatal("migration retry failed", err)
|
||||
}
|
||||
old, err := r.Head(ctx, "test", id, "und")
|
||||
if err != nil || old.UUID != "existing-revision" || old.DocumentHash != hash("old") {
|
||||
t.Fatal("v1 revision altered", err)
|
||||
}
|
||||
if got, err := r.ResolvePath(ctx, "test", "/existing"); err != nil || got != id {
|
||||
t.Fatal("v1 binding altered", err)
|
||||
}
|
||||
if _, err := r.AppendRevision(ctx, "test", Revision{ObjectUUID: id, DocumentHash: hash("new"), ContentType: "text/plain"}, old.UUID); err != nil {
|
||||
t.Fatal("post-migration save failed", err)
|
||||
}
|
||||
if err := r.database.QueryRowContext(ctx, `SELECT count(*) FROM activity_outbox`).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("historical revisions spuriously notified", err, count)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -202,6 +202,9 @@ func (s *Server) adapterActionsForPath(r *http.Request, semanticPath string) ([]
|
||||
return plugins[i].DefaultEditor && !plugins[j].DefaultEditor
|
||||
})
|
||||
for _, plugin := range plugins {
|
||||
if plugin.Mode == "editor" && !s.store.SupportsHostedEditors() {
|
||||
continue
|
||||
}
|
||||
if pluginMatches(plugin, strings.ToLower(mediaType), filename) {
|
||||
actions = append(actions, adapterAction{
|
||||
ID: plugin.ID, Name: plugin.Name, Mode: plugin.Mode,
|
||||
@@ -341,6 +344,10 @@ func (s *Server) handleOpenWith(w http.ResponseWriter, r *http.Request) {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if selected.Mode == "editor" && !s.store.SupportsHostedEditors() {
|
||||
http.Error(w, "hosted editors are unavailable on this database backend; use the built-in editor", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
actions, err := s.adapterActionsForPath(r, semanticPath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 404)
|
||||
|
||||
@@ -100,6 +100,10 @@ func (s *Server) authorizeAdapterSession(r *http.Request, session *store.Adapter
|
||||
}
|
||||
|
||||
func (s *Server) handleAdapterSessionAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.store.SupportsHostedEditors() {
|
||||
writeEventJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "hosted editors disabled for this database backend; use the built-in editor"})
|
||||
return
|
||||
}
|
||||
action := strings.Trim(strings.TrimPrefix(r.URL.Path, "/_/api/v1/adapter/session"), "/")
|
||||
if action == "exchange" && r.Method == http.MethodPost {
|
||||
var input struct {
|
||||
|
||||
@@ -89,6 +89,7 @@ func (s *Server) handleSaveRevisionAPI(
|
||||
return
|
||||
}
|
||||
domainUUID := s.domainUUIDForRequest(r)
|
||||
actorUUID := ""
|
||||
if req.EditorPrincipal != nil {
|
||||
principal, e := s.store.PrincipalForSession(domainUUID, s.authTokenFromRequest(r))
|
||||
id := ""
|
||||
@@ -106,10 +107,14 @@ func (s *Server) handleSaveRevisionAPI(
|
||||
api.WriteError(w, http.StatusUnauthorized, "authentication_required", "authentication required")
|
||||
return
|
||||
}
|
||||
var revisionCount int
|
||||
_ = s.store.DB.QueryRow(`SELECT COUNT(*) FROM revisions WHERE object_uuid=?`, req.ObjectUUID).Scan(&revisionCount)
|
||||
actorUUID = principal.UUID
|
||||
hasRevision, revisionErr := s.store.ObjectHasRevision(r.Context(), req.ObjectUUID)
|
||||
if revisionErr != nil {
|
||||
api.WriteError(w, 500, "storage_unavailable", "revision state unavailable")
|
||||
return
|
||||
}
|
||||
capability := "write"
|
||||
if revisionCount == 0 {
|
||||
if !hasRevision {
|
||||
capability = "create"
|
||||
}
|
||||
allowed := false
|
||||
@@ -129,12 +134,7 @@ func (s *Server) handleSaveRevisionAPI(
|
||||
}
|
||||
}
|
||||
|
||||
var revision *store.Revision
|
||||
if req.BaseRevision != nil {
|
||||
revision, err = s.store.SaveRevisionContentIfHead(req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename, *req.BaseRevision)
|
||||
} else {
|
||||
revision, err = s.store.SaveRevisionContent(req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename)
|
||||
}
|
||||
revision, err := s.store.SaveRevisionContentInDomain(r.Context(), domainUUID, actorUUID, req.ObjectUUID, []byte(req.Body), req.Message, req.Language, req.ContentType, req.Filename, req.BaseRevision)
|
||||
if errors.Is(err, store.ErrRevisionConflict) {
|
||||
api.WriteError(w, http.StatusConflict, "revision_conflict", "document changed; reload the language head before saving")
|
||||
return
|
||||
|
||||
@@ -285,8 +285,7 @@ func (s *Server) verifyMagicLink(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
config, configErr := s.store.AuthProviderConfig(domainUUID, "smtp")
|
||||
var valid bool
|
||||
stateErr := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? WHERE p.uuid=? AND p.state IN ('active','readonly') AND m.state='active' AND p.email=?)`, domainUUID, principalUUID, payload.Email).Scan(&valid)
|
||||
valid, stateErr := s.store.ValidateMagicLinkIdentity(r.Context(), domainUUID, principalUUID, payload.Email)
|
||||
if configErr != nil || !config.Enabled || stateErr != nil || !valid || payload.Email == "" {
|
||||
http.Error(w, "This sign-in link is invalid or expired", http.StatusBadRequest)
|
||||
return
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -36,14 +36,7 @@ func (s *Server) calendars() (*calendar.Repository, error) {
|
||||
s.calendarErr = errors.New("calendar storage unavailable")
|
||||
return
|
||||
}
|
||||
s.calendarRepo, s.calendarErr = calendar.New(filepath.Join(s.store.DocumentRoot, "calendars"), s.store.DB)
|
||||
if s.calendarErr == nil {
|
||||
s.calendarRepo.PublicAllowed = func(domain string) bool {
|
||||
var allowed bool
|
||||
err := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM locality_publication_policies p JOIN domains d ON d.uuid=p.domain_uuid AND d.enabled=1 JOIN config c ON c.domain_uuid=p.domain_uuid AND c.key='authorization_mode' AND c.value='enforcing' WHERE p.domain_uuid=? AND p.node_kind='domain' AND p.node_uuid=p.domain_uuid AND p.policy='public')`, domain).Scan(&allowed)
|
||||
return err == nil && allowed
|
||||
}
|
||||
}
|
||||
s.calendarRepo, s.calendarErr = s.store.NewCalendarRepository(context.Background())
|
||||
})
|
||||
return s.calendarRepo, s.calendarErr
|
||||
}
|
||||
@@ -316,9 +309,8 @@ func (s *Server) handleCalendarAPI(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
id = p.UUID
|
||||
}
|
||||
var count int
|
||||
e := s.store.DB.QueryRowContext(r.Context(), `SELECT COUNT(*) FROM domain_memberships WHERE domain_uuid=? AND principal_uuid=? AND state='active'`, domain, id).Scan(&count)
|
||||
if e != nil || count != 1 {
|
||||
active, e := s.store.ActiveDomainMember(r.Context(), domain, id)
|
||||
if e != nil || !active {
|
||||
http.Error(w, "unknown collaborator", 400)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -161,14 +161,7 @@ func fetchCalendarSource(ctx context.Context, client *http.Client, c calendar.Ca
|
||||
return events, response.Header.Get("ETag"), response.Header.Get("Last-Modified"), false, e
|
||||
}
|
||||
func (s *Server) ensureCalendarSweep() error {
|
||||
var exists bool
|
||||
if e := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM event_definitions WHERE uuid=?)`, calendarSweepType).Scan(&exists); e != nil {
|
||||
return e
|
||||
}
|
||||
if exists {
|
||||
return nil
|
||||
}
|
||||
return s.store.SaveEventDefinition(&store.EventDefinition{UUID: calendarSweepType, Name: "Calendar source synchronization", EventType: calendarSweepType, EventKey: calendarSweepType, Payload: "{}", ScheduleKind: "interval", IntervalSeconds: 60, Enabled: true, Priority: -10, MaxAttempts: 1})
|
||||
return s.store.EnsureBuiltinEventDefinition(store.EventDefinition{UUID: calendarSweepType, Name: "Calendar source synchronization", EventType: calendarSweepType, EventKey: calendarSweepType, Payload: "{}", ScheduleKind: "interval", IntervalSeconds: 60, Enabled: true, Priority: -10, MaxAttempts: 1})
|
||||
}
|
||||
func (s *Server) enqueueCalendarSource(domain, id string, force bool) (bool, error) {
|
||||
b, _ := json.Marshal(calendarSyncPayload{Domain: domain, Calendar: id, Force: force})
|
||||
@@ -220,8 +213,7 @@ func (s *Server) runCalendarSource(job *store.EventJob) error {
|
||||
if !c.Source.Enabled || (!p.Force && c.Source.Next > time.Now().UnixMilli()) {
|
||||
return nil
|
||||
}
|
||||
var active bool
|
||||
e = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM domain_memberships m JOIN principals p ON p.uuid=m.principal_uuid JOIN domains d ON d.uuid=m.domain_uuid WHERE m.domain_uuid=? AND m.principal_uuid=? AND m.state='active' AND p.state='active' AND d.enabled=1)`, p.Domain, c.Owner).Scan(&active)
|
||||
active, e := s.store.ActiveDomainMember(context.Background(), p.Domain, c.Owner)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
type cityAutoPlanItem struct {
|
||||
@@ -46,15 +48,7 @@ func (s *Server) availableCityAutoName(domain, kind, parent, base, boundary stri
|
||||
}
|
||||
name = stem + "-" + hex.EncodeToString(sum[:8])
|
||||
}
|
||||
var exists bool
|
||||
var err error
|
||||
if kind == "spot" {
|
||||
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM spots WHERE domain_uuid=? AND name=? AND state!='archived')`, domain, name).Scan(&exists)
|
||||
} else if kind == "quartier" {
|
||||
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM zones WHERE domain_uuid=? AND kind='quartier' AND name=? AND state!='archived')`, domain, name).Scan(&exists)
|
||||
} else {
|
||||
err = s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM zones WHERE domain_uuid=? AND parent_uuid=? AND name=? AND state!='archived')`, domain, parent, name).Scan(&exists)
|
||||
}
|
||||
exists, err := s.store.CityNameExists(domain, kind, parent, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -66,19 +60,29 @@ func (s *Server) availableCityAutoName(domain, kind, parent, base, boundary stri
|
||||
}
|
||||
|
||||
func (s *Server) buildCityAutoPlan(domainUUID string) ([]cityAutoPlanItem, error) {
|
||||
rows, err := s.store.DB.Query(`SELECT p.path,p.object_uuid FROM paths p WHERE p.domain_uuid=? AND p.path LIKE '/%' AND p.path!='/u' AND p.path NOT LIKE '/u/%'
|
||||
AND NOT EXISTS(SELECT 1 FROM city_page_overrides ov JOIN spots dest ON dest.uuid=ov.spot_uuid AND dest.domain_uuid=ov.domain_uuid AND dest.state!='archived' WHERE ov.domain_uuid=p.domain_uuid AND ov.page_uuid=p.object_uuid)
|
||||
ORDER BY p.path`, domainUUID)
|
||||
candidates, err := s.store.CityAutoCandidates(domainUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []cityAutoPlanItem
|
||||
for rows.Next() {
|
||||
var semanticPath, pageUUID string
|
||||
if err := rows.Scan(&semanticPath, &pageUUID); err != nil {
|
||||
return nil, err
|
||||
// Many Pages share one boundary. Resolve each boundary once, without
|
||||
// retaining a database cursor while making nested repository calls.
|
||||
type lookupResult struct {
|
||||
node store.CityNodeDetail
|
||||
err error
|
||||
}
|
||||
cache := map[string]lookupResult{}
|
||||
lookup := func(kind, parent, boundary string) (store.CityNodeDetail, error) {
|
||||
key := kind + "\x00" + parent + "\x00" + boundary
|
||||
if r, ok := cache[key]; ok {
|
||||
return r.node, r.err
|
||||
}
|
||||
node, err := s.store.CityNodeOverlappingPath(domainUUID, kind, parent, boundary)
|
||||
cache[key] = lookupResult{node, err}
|
||||
return node, err
|
||||
}
|
||||
for _, candidate := range candidates {
|
||||
semanticPath, pageUUID := candidate.Path, candidate.ObjectUUID
|
||||
parts := strings.Split(strings.Trim(semanticPath, "/"), "/")
|
||||
if len(parts) < 3 || parts[0] == "system" {
|
||||
continue
|
||||
@@ -93,19 +97,25 @@ func (s *Server) buildCityAutoPlan(domainUUID string) ([]cityAutoPlanItem, error
|
||||
item.SpotPath = item.StreetPath + "/_undefined"
|
||||
}
|
||||
item.Action = "create"
|
||||
if q, e := s.store.CityNodeOverlappingPath(domainUUID, "quartier", "", item.QuartierPath); e == nil {
|
||||
if q, e := lookup("quartier", "", item.QuartierPath); e == nil {
|
||||
item.Quartier = q.Name
|
||||
item.Action = "merge"
|
||||
if st, e := s.store.CityNodeOverlappingPath(domainUUID, "street", q.UUID, item.StreetPath); e == nil {
|
||||
if st, e := lookup("street", q.UUID, item.StreetPath); e == nil {
|
||||
item.Street = st.Name
|
||||
if spot, e := s.store.CityNodeOverlappingPath(domainUUID, "spot", st.UUID, item.SpotPath); e == nil {
|
||||
if spot, e := lookup("spot", st.UUID, item.SpotPath); e == nil {
|
||||
item.Spot = spot.Name
|
||||
} else if !errors.Is(e, sql.ErrNoRows) {
|
||||
return nil, e
|
||||
}
|
||||
} else if !errors.Is(e, sql.ErrNoRows) {
|
||||
return nil, e
|
||||
}
|
||||
} else if !errors.Is(e, sql.ErrNoRows) {
|
||||
return nil, e
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out, rows.Err()
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (s *Server) applyCityAutoPlan(domainUUID, actor string) (int, error) {
|
||||
|
||||
@@ -40,7 +40,7 @@ func (s *Server) handleCityGovernance(w http.ResponseWriter, r *http.Request) {
|
||||
kind := map[string]string{"spots": "spot", "domains": "domain"}[p[0]]
|
||||
var err error
|
||||
if p[0] == "zones" {
|
||||
err = s.store.DB.QueryRowContext(r.Context(), `SELECT kind FROM zones WHERE uuid=? AND domain_uuid=?`, p[1], domain).Scan(&kind)
|
||||
kind, err = s.store.CityZoneKind(r.Context(), domain, p[1])
|
||||
}
|
||||
var result any
|
||||
if err == nil && automatic {
|
||||
|
||||
@@ -131,14 +131,17 @@ func (s *Server) handleConfig(
|
||||
campusAdminRequired := strings.HasPrefix(section, "campus-") || strings.HasPrefix(section, "domain-") || section == "instance-status"
|
||||
if campusAdminRequired {
|
||||
targetCampusUUID := currentDomain.CampusUUID
|
||||
var targetErr error
|
||||
if section == "domain-create" {
|
||||
targetCampusUUID = r.FormValue("domain_campus_uuid")
|
||||
} else if section == "domain-update" || section == "domain-delete" || section == "domain-entry" || section == "domain-projection" {
|
||||
_ = s.store.DB.QueryRow(`SELECT campus_uuid FROM domains WHERE id=?`, r.FormValue("domain_id")).Scan(&targetCampusUUID)
|
||||
id, _ := strconv.ParseInt(r.FormValue("domain_id"), 10, 64)
|
||||
targetCampusUUID, targetErr = s.store.ConfigTargetCampus(r.Context(), true, id)
|
||||
} else if section != "campus-create" && r.FormValue("campus_id") != "" {
|
||||
_ = s.store.DB.QueryRow(`SELECT uuid FROM campuses WHERE id=?`, r.FormValue("campus_id")).Scan(&targetCampusUUID)
|
||||
id, _ := strconv.ParseInt(r.FormValue("campus_id"), 10, 64)
|
||||
targetCampusUUID, targetErr = s.store.ConfigTargetCampus(r.Context(), false, id)
|
||||
}
|
||||
if !s.store.IsCampusAdmin(targetCampusUUID, principal.UUID) {
|
||||
if targetErr != nil || !s.store.IsCampusAdmin(targetCampusUUID, principal.UUID) {
|
||||
http.Error(w, "campus admin required", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
@@ -824,6 +827,10 @@ func (s *Server) handleConfig(
|
||||
}
|
||||
|
||||
case "zombie-purge":
|
||||
if !s.store.SupportsZombieGC() {
|
||||
http.Error(w, store.ErrBackendFeatureUnavailable.Error(), http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.store.PurgeZombieObjectsInDomain(
|
||||
r.Context(), domainUUID,
|
||||
@@ -918,7 +925,7 @@ func (s *Server) handleConfig(
|
||||
s.getZombieGraceHours(s.domainUUIDForRequest(r))
|
||||
|
||||
var zombies []store.ZombieObject
|
||||
if r.URL.Query().Get("panel") == "objects" {
|
||||
if r.URL.Query().Get("panel") == "objects" && s.store.SupportsZombieGC() {
|
||||
zombies, err =
|
||||
s.store.ListZombieObjectsInDomain(
|
||||
r.Context(), s.domainUUIDForRequest(r),
|
||||
|
||||
@@ -88,12 +88,13 @@ func (s *Server) domainForRequestWithError(
|
||||
return s.currentDomain(), nil
|
||||
}
|
||||
if cached, ok := r.Context().Value(requestDomainKey{}).(*requestDomainLookup); ok && cached.host == r.Host {
|
||||
cached.once.Do(func() { cached.domain, cached.err = s.store.GetDomainByHost(r.Host) })
|
||||
cached.once.Do(func() { cached.domain, cached.err = s.store.GetDomainByHostContext(r.Context(), r.Host) })
|
||||
return cached.domain, cached.err
|
||||
}
|
||||
|
||||
sp, err :=
|
||||
s.store.GetDomainByHost(
|
||||
s.store.GetDomainByHostContext(
|
||||
r.Context(),
|
||||
r.Host,
|
||||
)
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ func (q schedulerQueue) Started(ctx context.Context, job *dregarnuhr.Job) error
|
||||
return q.store.AddEventLogContext(ctx, job.UUID, job.EventUUID, "info", "execution started")
|
||||
}
|
||||
func (q schedulerQueue) Finish(ctx context.Context, job *dregarnuhr.Job, err error, now time.Time) error {
|
||||
return q.store.FinishEventAt(ctx, job.UUID, err, now)
|
||||
return q.store.FinishClaimedEventAt(ctx, job, err, now)
|
||||
}
|
||||
|
||||
func (s *Server) eventRunner() *dregarnuhr.Runner {
|
||||
@@ -112,6 +112,9 @@ func (s *Server) schedulerLoop() dregarnuhr.Loop {
|
||||
{Name: "initial-preview-sweep", After: 5 * time.Second, Run: func(context.Context, time.Time) error { s.enqueueAllPreviewEvents(); return nil }},
|
||||
{Name: "preview-sweep", Every: 15 * time.Minute, Run: func(context.Context, time.Time) error { s.enqueueAllPreviewEvents(); return nil }},
|
||||
{Name: "history-cleanup", Every: time.Hour, Run: func(context.Context, time.Time) error {
|
||||
if !s.store.SupportsEventHistoryMaintenance() {
|
||||
return nil
|
||||
}
|
||||
settings, err := s.store.GetQueueSettings()
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -54,39 +54,56 @@ func (s *Server) initializePreviewScheduler() {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) startEventScheduler() {
|
||||
func (s *Server) startEventScheduler() error {
|
||||
if s.store == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
release, err := s.store.AcquireEventWorker(s.backgroundCtx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ready := false
|
||||
defer func() {
|
||||
if !ready {
|
||||
_ = release()
|
||||
}
|
||||
}()
|
||||
if err := s.store.EnsureFeedEvent(); err != nil {
|
||||
log.Printf("feed scheduler initialization failed")
|
||||
return fmt.Errorf("feed scheduler initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureBuiltinResourceEvent(); err != nil {
|
||||
log.Printf("resource monitor initialization failed: %v", err)
|
||||
return fmt.Errorf("resource monitor initialization: %w", err)
|
||||
}
|
||||
if err := s.ensureCalendarSweep(); err != nil {
|
||||
log.Printf("calendar scheduler initialization failed: %v", err)
|
||||
return fmt.Errorf("calendar scheduler initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskActivityBridgeEvent(); err != nil {
|
||||
log.Printf("task journal bridge initialization failed: %v", err)
|
||||
return fmt.Errorf("task journal bridge initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskReminderEvent(); err != nil {
|
||||
log.Printf("task reminder initialization failed: %v", err)
|
||||
return fmt.Errorf("task reminder initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskFocusEvent(); err != nil {
|
||||
log.Printf("focus timer initialization failed: %v", err)
|
||||
return fmt.Errorf("focus timer initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureTaskDigestEvent(); err != nil {
|
||||
log.Printf("task digest initialization failed: %v", err)
|
||||
return fmt.Errorf("task digest initialization: %w", err)
|
||||
}
|
||||
if err := s.store.EnsureNotificationPushEvent(); err != nil {
|
||||
log.Printf("notification push initialization failed: %v", err)
|
||||
if s.store.SupportsNotificationPush() {
|
||||
if err := s.store.EnsureNotificationPushEvent(); err != nil {
|
||||
return fmt.Errorf("notification push initialization: %w", err)
|
||||
}
|
||||
}
|
||||
s.workerRelease = release
|
||||
ready = true
|
||||
s.backgroundWG.Add(1)
|
||||
go func() {
|
||||
if err := s.schedulerLoop().Run(context.Background()); err != nil {
|
||||
defer s.backgroundWG.Done()
|
||||
if err := s.schedulerLoop().Run(s.backgroundCtx); err != nil && !errors.Is(err, context.Canceled) {
|
||||
log.Printf("event scheduler loop stopped: %v", err)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) wakeEventScheduler() {
|
||||
|
||||
@@ -29,6 +29,10 @@ func zombieAPIOptions(r *http.Request) (int64, int, error) {
|
||||
}
|
||||
|
||||
func (s *Server) handleZombieListAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.store.SupportsZombieGC() {
|
||||
http.Error(w, store.ErrBackendFeatureUnavailable.Error(), http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", http.MethodGet)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
@@ -50,6 +54,10 @@ func (s *Server) handleZombieListAPI(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (s *Server) handleZombiePurgeAPI(w http.ResponseWriter, r *http.Request) {
|
||||
if !s.store.SupportsZombieGC() {
|
||||
http.Error(w, store.ErrBackendFeatureUnavailable.Error(), http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
if r.Method != http.MethodPost {
|
||||
w.Header().Set("Allow", http.MethodPost)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
|
||||
@@ -41,13 +41,18 @@ func (s *Server) zombieMaintenanceGrace() (int64, error) {
|
||||
}
|
||||
|
||||
func (s *Server) startGCWorker() {
|
||||
if !s.store.SupportsZombieGC() {
|
||||
return
|
||||
}
|
||||
grace, err := s.zombieMaintenanceGrace()
|
||||
if err != nil {
|
||||
log.Printf("zombie GC maintenance disabled: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
s.backgroundWG.Add(1)
|
||||
go func() {
|
||||
defer s.backgroundWG.Done()
|
||||
|
||||
ticker :=
|
||||
time.NewTicker(
|
||||
@@ -57,7 +62,11 @@ func (s *Server) startGCWorker() {
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
<-ticker.C
|
||||
select {
|
||||
case <-s.backgroundCtx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
}
|
||||
|
||||
deleted, err :=
|
||||
s.store.PurgeZombieObjects(
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"compress/gzip"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -597,10 +598,6 @@ func (s *Server) rollbackImport(domainUUID, sessionUUID string) error {
|
||||
if session.Status != "completed" && session.Status != "failed" {
|
||||
return errors.New("only a completed or failed import can be rolled back")
|
||||
}
|
||||
entries, err := s.store.ImportJournal(sessionUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
actor := session.ActorUUID
|
||||
if actor == "" {
|
||||
actor = "system:import"
|
||||
@@ -610,32 +607,20 @@ func (s *Server) rollbackImport(domainUUID, sessionUUID string) error {
|
||||
return err
|
||||
}
|
||||
for _, edge := range edges {
|
||||
_ = s.store.RemoveSpotPage(domainUUID, actor, edge.SpotUUID, edge.PageUUID, "primary")
|
||||
if err := s.store.RemoveSpotPage(domainUUID, actor, edge.SpotUUID, edge.PageUUID, "primary"); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
nodes, err := s.store.ImportCityJournal(sessionUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, node := range nodes {
|
||||
_ = s.store.DeleteCityNode(domainUUID, actor, node.Kind, node.UUID)
|
||||
}
|
||||
tx, err := s.store.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
for _, x := range entries {
|
||||
if _, err = tx.Exec(`DELETE FROM paths WHERE domain_uuid=? AND path=? AND object_uuid=?`, domainUUID, x.Path, x.ObjectUUID); err != nil {
|
||||
if err := s.store.DeleteCityNode(domainUUID, actor, node.Kind, node.UUID); err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(`UPDATE import_sessions SET status='rolled_back',updated_at_ms=? WHERE uuid=?`, time.Now().UnixMilli(), sessionUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = tx.Commit(); err == nil {
|
||||
s.store.InvalidatePathCaches()
|
||||
}
|
||||
return err
|
||||
return s.store.RollbackImportPaths(domainUUID, sessionUUID)
|
||||
}
|
||||
|
||||
func readImportArchive(filename, name string, wanted map[string]bool) (map[string][]byte, error) {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Close waits for the scheduler loop, maintenance and all running handlers
|
||||
// before releasing database worker ownership. Deadline expiry leaves ownership
|
||||
// held until the in-flight work actually exits; callers must not close the DB.
|
||||
func (s *Server) Close(ctx context.Context) error {
|
||||
s.closeOnce.Do(func() {
|
||||
s.backgroundCancel()
|
||||
go func() {
|
||||
_ = s.eventRunner().Stop(context.Background())
|
||||
s.backgroundWG.Wait()
|
||||
if s.workerRelease != nil {
|
||||
s.closeErr = s.workerRelease()
|
||||
}
|
||||
close(s.closeDone)
|
||||
}()
|
||||
})
|
||||
select {
|
||||
case <-s.closeDone:
|
||||
return s.closeErr
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// RunContext drains HTTP before releasing worker ownership. If a request or
|
||||
// worker cannot stop in time, return an error without pretending it has exited.
|
||||
func (s *Server) RunContext(ctx context.Context, addr string) error {
|
||||
server := &http.Server{Addr: addr, Handler: s.handler(), ReadHeaderTimeout: 10 * time.Second}
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- server.ListenAndServe() }()
|
||||
var serveErr error
|
||||
select {
|
||||
case serveErr = <-done:
|
||||
case <-ctx.Done():
|
||||
shutdown, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
serveErr = server.Shutdown(shutdown)
|
||||
cancel()
|
||||
if serveErr != nil {
|
||||
_ = server.Close()
|
||||
}
|
||||
}
|
||||
closeCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
closeErr := s.Close(closeCtx)
|
||||
if errors.Is(serveErr, http.ErrServerClosed) {
|
||||
serveErr = nil
|
||||
}
|
||||
return errors.Join(serveErr, closeErr)
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/dregarnuhr"
|
||||
"pageswiki/internal/store"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func lifecycleFixture(t *testing.T) *store.Store {
|
||||
t.Helper()
|
||||
t.Chdir("../..")
|
||||
database, err := db.Open(t.TempDir() + "/pages.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
if err = db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := store.NewWithDocumentRoot(database, t.TempDir()+"/documents")
|
||||
if err = s.EnsureConfigTable(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func TestServerCloseRetainsOwnershipUntilHandlersExit(t *testing.T) {
|
||||
st := lifecycleFixture(t)
|
||||
st.BackgroundJobsDisabled = true
|
||||
s, err := NewChecked(st)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Admission is driven manually: avoid unrelated timed triggers in this test.
|
||||
st.BackgroundJobsDisabled = false
|
||||
released := make(chan struct{})
|
||||
s.workerRelease = func() error { close(released); return nil }
|
||||
started := make(chan struct{})
|
||||
cancelled := make(chan struct{})
|
||||
exit := make(chan struct{})
|
||||
if err = s.registeredEventHandlers().Register("synthetic.stop", func(ctx context.Context, _ *dregarnuhr.Job) error {
|
||||
close(started)
|
||||
<-ctx.Done()
|
||||
close(cancelled)
|
||||
<-exit
|
||||
return ctx.Err()
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = st.EnqueueEvent("synthetic.stop", "one", "", time.Now()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s.eventRunner().Dispatch(s.backgroundCtx)
|
||||
select {
|
||||
case <-started:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("handler not started")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
if err = s.Close(ctx); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-cancelled:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("handler not cancelled")
|
||||
}
|
||||
select {
|
||||
case <-released:
|
||||
t.Fatal("ownership released while handler lives")
|
||||
default:
|
||||
}
|
||||
close(exit)
|
||||
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err = s.Close(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
select {
|
||||
case <-released:
|
||||
default:
|
||||
t.Fatal("ownership retained after stop")
|
||||
}
|
||||
}
|
||||
|
||||
func TestServerRefusesSchedulerInitializationFailure(t *testing.T) {
|
||||
st := lifecycleFixture(t)
|
||||
if _, err := st.DB.Exec(`DROP TABLE event_definitions`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s, err := NewChecked(st)
|
||||
if err == nil || s != nil {
|
||||
t.Fatal("partial server accepted", err)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package server
|
||||
|
||||
import "pageswiki/internal/mestionora"
|
||||
import "pageswiki/internal/store"
|
||||
|
||||
// Construction is read-only: schema/index maintenance remains an explicit
|
||||
// application startup operation, never a search request side effect.
|
||||
func (s *Server) searchEngine() *mestionora.SQLite {
|
||||
return mestionora.NewSQLite(s.store.DB)
|
||||
func (s *Server) searchEngine() *store.Store {
|
||||
return s.store
|
||||
}
|
||||
@@ -25,6 +25,9 @@ func validPushSubscription(sub webpush.Subscription) bool {
|
||||
}
|
||||
|
||||
func (s *Server) notificationPushKeys(ctx context.Context) (private, public string, err error) {
|
||||
if !s.store.SupportsNotificationPush() {
|
||||
return "", "", store.ErrBackendFeatureUnavailable
|
||||
}
|
||||
err = s.store.DB.QueryRowContext(ctx, `SELECT private_key,public_key FROM notification_push_keys WHERE id=1`).Scan(&private, &public)
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
return
|
||||
@@ -42,6 +45,10 @@ func (s *Server) notificationPushKeys(ctx context.Context) (private, public stri
|
||||
}
|
||||
|
||||
func (s *Server) handleNotificationPush(w http.ResponseWriter, r *http.Request, domain, owner string) {
|
||||
if !s.store.SupportsNotificationPush() {
|
||||
writeEventJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "push disabled for this database backend"})
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
switch r.Method {
|
||||
|
||||
@@ -34,6 +34,12 @@ func notificationDeliveryClient() *http.Client {
|
||||
}
|
||||
|
||||
func (s *Server) deliverNotificationPushContext(ctx context.Context, job *store.EventJob) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if !s.store.SupportsNotificationPush() {
|
||||
return store.ErrBackendFeatureUnavailable
|
||||
}
|
||||
dispatcher := ordoschnellic.Dispatcher{
|
||||
Queue: notificationDeliveryQueue{s.store},
|
||||
Sender: ordoschnellic.WebPush{Keys: s.notificationPushKeys, Client: notificationDeliveryClient()},
|
||||
|
||||
@@ -19,22 +19,11 @@ func (s *Server) periodicalShareTargets(r *http.Request, kind, path string) []pe
|
||||
defer cancel()
|
||||
r = r.WithContext(ctx)
|
||||
source := s.domainUUIDForRequest(r)
|
||||
rows, err := s.store.DB.QueryContext(r.Context(), `SELECT domain_uuid FROM periodical_projections WHERE source_uuid=? AND kind=? ORDER BY domain_uuid LIMIT 65`, source, kind)
|
||||
ids, err := s.store.PeriodicalProjectionTargets(r.Context(), source, kind)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return nil
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil || len(ids) > 64 {
|
||||
if len(ids) > 64 {
|
||||
return nil
|
||||
}
|
||||
out := []periodicalShareTarget{}
|
||||
|
||||
@@ -26,9 +26,7 @@ func (s *Server) appearanceForRequest(r *http.Request) personalAppearance {
|
||||
if principal == nil {
|
||||
return p
|
||||
}
|
||||
var raw string
|
||||
var version int64
|
||||
if s.store.DB.QueryRowContext(r.Context(), `SELECT version,data FROM personal_appearance WHERE domain_uuid=? AND owner_uuid=?`, domain, principal.UUID).Scan(&version, &raw) == nil {
|
||||
if version, raw, err := s.store.PersonalAppearance(r.Context(), domain, principal.UUID); err == nil {
|
||||
var saved personalAppearance
|
||||
if json.Unmarshal([]byte(raw), &saved) == nil {
|
||||
saved.Version = version
|
||||
@@ -93,20 +91,7 @@ func (s *Server) savePersonalAppearance(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
p.Version = version + 1
|
||||
raw, _ := json.Marshal(p)
|
||||
result, err := s.store.DB.ExecContext(r.Context(), `INSERT INTO personal_appearance(domain_uuid,owner_uuid,version,data) SELECT ?,?,?,? WHERE ?=0 ON CONFLICT(domain_uuid,owner_uuid) DO NOTHING`, domain, actor, p.Version, string(raw), version)
|
||||
if err == nil {
|
||||
var n int64
|
||||
n, err = result.RowsAffected()
|
||||
if err == nil && n == 0 {
|
||||
result, err = s.store.DB.ExecContext(r.Context(), `UPDATE personal_appearance SET version=?,data=? WHERE domain_uuid=? AND owner_uuid=? AND version=?`, p.Version, string(raw), domain, actor, version)
|
||||
if err == nil {
|
||||
n, err = result.RowsAffected()
|
||||
if n == 0 {
|
||||
err = sql.ErrNoRows
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
err = s.store.SavePersonalAppearance(r.Context(), domain, actor, version, string(raw))
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
http.Error(w, "Settings changed elsewhere; reload before saving.", 409)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/db"
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
const nativeTestDomain = "35ff2304-a401-49ba-b21a-6129d4be2af2"
|
||||
|
||||
func nativeRuntimeFixture(t *testing.T) *store.Store {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("PAGESWIKI_TEST_POSTGRES_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("set PAGESWIKI_TEST_POSTGRES_DSN to a disposable PostgreSQL URL")
|
||||
}
|
||||
u, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
t.Fatal("invalid test DSN")
|
||||
}
|
||||
ctx := context.Background()
|
||||
admin, err := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: dsn, AllowInsecure: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
schema := "pageswiki_http_test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
if _, err = admin.Exec(`CREATE SCHEMA "` + schema + `"`); err != nil {
|
||||
admin.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
defer admin.Close()
|
||||
if _, err := admin.Exec(`DROP SCHEMA "` + schema + `" CASCADE`); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
})
|
||||
q := u.Query()
|
||||
q.Set("search_path", schema)
|
||||
u.RawQuery = q.Encode()
|
||||
database, err := db.OpenPostgreSQL(ctx, db.PostgreSQLOptions{DSN: u.String(), AllowInsecure: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { database.Close() })
|
||||
root := filepath.Join(t.TempDir(), "documents")
|
||||
if err = os.Mkdir(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PAGESWIKI_AUTH_KEY", "synthetic runtime key")
|
||||
// Initial bootstrap creates schema but refuses to invent namespace IDs.
|
||||
if _, err = store.NewPostgreSQLWithDocumentRoot(ctx, database, root); err == nil {
|
||||
t.Fatal("unprovisioned namespace accepted")
|
||||
}
|
||||
if _, err = database.Exec(`INSERT INTO campuses(uuid,name,title,created_at) VALUES('campus','default','Test Campus',1);
|
||||
INSERT INTO domains(uuid,name,title,created_at,campus_uuid) VALUES('35ff2304-a401-49ba-b21a-6129d4be2af2','default','Test Domain',1,'campus')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s, err := store.NewPostgreSQLWithDocumentRoot(ctx, database, root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Chdir("../..")
|
||||
return s
|
||||
}
|
||||
|
||||
func TestPostgreSQLRuntimeHTTPAndRestart(t *testing.T) {
|
||||
st := nativeRuntimeFixture(t)
|
||||
ctx := context.Background()
|
||||
owner, err := st.BootstrapOwner(nativeTestDomain, "owner", "synthetic runtime password", "Owner", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := st.CreateAuthSession(nativeTestDomain, owner.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = st.SetConfigInDomain(nativeTestDomain, "authorization_mode", "enforcing"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obj, err := st.CreatePageAtPathInDomain(nativeTestDomain, "/native-startup")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = st.SaveRevisionContentInDomain(ctx, nativeTestDomain, owner.UUID, obj, []byte("<h1>Native startup page</h1><p>Canonical document</p>"), "initial", "en", "text/html", "", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for iteration := 0; iteration < 2; iteration++ {
|
||||
if iteration > 0 {
|
||||
st, err = store.NewPostgreSQLWithDocumentRoot(ctx, st.DB, st.DocumentRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
s, err := NewChecked(st)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
closeServer := func() {
|
||||
closeCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
if err := s.Close(closeCtx); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
t.Cleanup(closeServer)
|
||||
listener := httptest.NewServer(s.handler())
|
||||
request, _ := http.NewRequest(http.MethodGet, listener.URL+"/native-startup", nil)
|
||||
request.Host = "localhost"
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
response, err := listener.Client().Do(request)
|
||||
if err != nil {
|
||||
listener.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := io.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
listener.Close()
|
||||
if err != nil || response.StatusCode != 200 || !bytes.Contains(body, []byte("Canonical document")) {
|
||||
t.Fatalf("listener roundtrip: %d %v", response.StatusCode, err)
|
||||
}
|
||||
job := &store.EventJob{UUID: "synthetic-runtime-resource", Type: store.ResourceEventType}
|
||||
for range 2 {
|
||||
if err := s.runEvent(job); err != nil {
|
||||
t.Fatal("resource worker", err)
|
||||
}
|
||||
}
|
||||
var samples int
|
||||
if err := st.DB.QueryRow(`SELECT count(*) FROM resource_samples WHERE job_uuid='synthetic-runtime-resource'`).Scan(&samples); err != nil || samples != 1 {
|
||||
t.Fatal("resource deduplication", samples, err)
|
||||
}
|
||||
for _, target := range []string{"/health", "/", "/native-startup", "/e/-/native-startup", "/v/search?q=native-startup", "/_/api/v1/search/suggest?q=native", "/-/sys/config?panel=auth", "/v/tree", "/v/browser", "/v/plan/tasks", "/v/plan/cal/2026/11", "/v/plan/contacts", "/v/plan/dashboard", "/v/feeds"} {
|
||||
r := httptest.NewRequest(http.MethodGet, "http://localhost"+target, nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("iteration %d %s: HTTP %d %.400s", iteration, target, w.Code, w.Body.String())
|
||||
}
|
||||
if strings.HasPrefix(target, "/v/search") && w.Header().Get("X-PagesWiki-Search-Profile") != "metadata-only" {
|
||||
t.Error("SQLite search selected")
|
||||
}
|
||||
if target == "/native-startup" && !strings.Contains(w.Body.String(), "Canonical document") {
|
||||
t.Error("page body missing")
|
||||
}
|
||||
}
|
||||
base, err := st.GetRevisionForExactLanguage(obj, "en")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
payload, _ := json.Marshal(map[string]any{"object_uuid": obj, "body": "<h1>Native startup page</h1><p>Canonical document updated</p>", "lang": "en", "content_type": "text/html", "base_revision_uuid": base.UUID, "editor_principal_uuid": owner.UUID})
|
||||
save := func(auth string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(http.MethodPost, "http://localhost/_/api/v1/revisions/save", bytes.NewReader(payload))
|
||||
r.Header.Set("Authorization", "Bearer "+auth)
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
if w := save(token); w.Code != 200 {
|
||||
t.Fatalf("save: %d %s", w.Code, w.Body)
|
||||
}
|
||||
if w := save(token); w.Code != 409 {
|
||||
t.Fatalf("stale save: %d %s", w.Code, w.Body)
|
||||
}
|
||||
if w := save("expired"); w.Code != 401 {
|
||||
t.Fatalf("expired save: %d %s", w.Code, w.Body)
|
||||
}
|
||||
for _, target := range []string{calendarDAV, taskDAVCollection, contactDAV} {
|
||||
r := httptest.NewRequest("PROPFIND", "http://localhost"+target, strings.NewReader(`<d:propfind xmlns:d="DAV:"><d:allprop/></d:propfind>`))
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
r.Header.Set("Depth", "1")
|
||||
r.Header.Set("Content-Type", "application/xml")
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code != 207 {
|
||||
t.Errorf("DAV %s: %d %.300s", target, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
for _, panel := range []string{"objects", "assets", "auth-providers", "auth-sessions", "groups", "campuses", "domains", "scheduler", "logs", "queue", "organizer"} {
|
||||
r := httptest.NewRequest("GET", "http://localhost/-/sys/config?panel="+panel, nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code != 200 {
|
||||
t.Errorf("panel %s: %d %.300s", panel, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
// Another Server must not take ownership while this worker is alive.
|
||||
if peer, e := NewChecked(st); e == nil {
|
||||
peer.Close(ctx)
|
||||
t.Error("duplicate worker accepted")
|
||||
}
|
||||
if iteration == 1 {
|
||||
if err := st.RevokeAuthSession(token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if w := save(token); w.Code != 401 {
|
||||
t.Errorf("revoked session accepted: %d", w.Code)
|
||||
}
|
||||
r := httptest.NewRequest("GET", "http://localhost/native-startup", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
s.handler().ServeHTTP(w, r)
|
||||
if w.Code == 200 || strings.Contains(w.Body.String(), "Canonical document") {
|
||||
t.Error("revoked session exposed private page")
|
||||
}
|
||||
}
|
||||
closeServer()
|
||||
}
|
||||
}
|
||||
@@ -38,16 +38,11 @@ func (s *Server) handleRestoreRevisionAPI(w http.ResponseWriter, r *http.Request
|
||||
http.Error(w, "forbidden", 403)
|
||||
return
|
||||
}
|
||||
revision, err := s.store.GetRevision(input.Revision)
|
||||
if err != nil {
|
||||
http.Error(w, "revision unavailable", 500)
|
||||
return
|
||||
}
|
||||
message := strings.TrimSpace(input.Message)
|
||||
if message == "" {
|
||||
message = "Restore revision " + revision.UUID
|
||||
message = "Restore revision " + metadata.UUID
|
||||
}
|
||||
saved, err := s.store.SaveRevisionContentIfHead(revision.ObjectUUID, []byte(revision.Body), message, revision.Language, revision.ContentType, revision.Filename, input.Expected)
|
||||
saved, err := s.store.RestoreRevisionInDomain(r.Context(), s.domainUUIDForRequest(r), "", input.Revision, input.Expected, message)
|
||||
if errors.Is(err, store.ErrRevisionConflict) {
|
||||
http.Error(w, "revision changed; reload history", 412)
|
||||
return
|
||||
@@ -57,5 +52,5 @@ func (s *Server) handleRestoreRevisionAPI(w http.ResponseWriter, r *http.Request
|
||||
return
|
||||
}
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
writeEventJSON(w, 200, map[string]string{"revision_uuid": saved.UUID, "object_uuid": revision.ObjectUUID})
|
||||
writeEventJSON(w, 200, map[string]string{"revision_uuid": saved.UUID, "object_uuid": saved.ObjectUUID})
|
||||
}
|
||||
@@ -121,6 +121,7 @@ func searchExcerpt(raw, contentType, query string) string {
|
||||
}
|
||||
|
||||
func (s *Server) handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-PagesWiki-Search-Profile", s.store.SearchProfile())
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
w.Header().Set("Allow", "GET, HEAD")
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
|
||||
@@ -86,6 +86,7 @@ func citySuggestion(result store.CitySearchResult, query string) searchSuggestio
|
||||
}
|
||||
|
||||
func (s *Server) handleSearchSuggestAPI(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-PagesWiki-Search-Profile", s.store.SearchProfile())
|
||||
if r.Method != http.MethodGet {
|
||||
w.Header().Set("Allow", http.MethodGet)
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -44,13 +45,32 @@ type Server struct {
|
||||
schedulerRunner *dregarnuhr.Runner
|
||||
eventHandlersOnce sync.Once
|
||||
eventHandlers *dregarnuhr.Registry
|
||||
backgroundCtx context.Context
|
||||
backgroundCancel context.CancelFunc
|
||||
backgroundWG sync.WaitGroup
|
||||
workerRelease func() error
|
||||
closeOnce sync.Once
|
||||
closeDone chan struct{}
|
||||
closeErr error
|
||||
}
|
||||
|
||||
func New(
|
||||
st *store.Store,
|
||||
) *Server {
|
||||
s, err := NewChecked(st)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// NewChecked refuses to serve when mandatory scheduler initialization fails.
|
||||
// New remains a compatibility wrapper for existing constructors and tests.
|
||||
func NewChecked(st *store.Store) (*Server, error) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
s := &Server{
|
||||
backgroundCtx: ctx, backgroundCancel: cancel, closeDone: make(chan struct{}),
|
||||
mux: http.NewServeMux(),
|
||||
store: st,
|
||||
previewSlots: make(chan struct{}, 1),
|
||||
@@ -111,11 +131,14 @@ func New(
|
||||
s.registerRoutes()
|
||||
|
||||
if !st.ReadOnly && !st.BackgroundJobsDisabled {
|
||||
if err := s.startEventScheduler(); err != nil {
|
||||
cancel()
|
||||
return nil, err
|
||||
}
|
||||
s.startGCWorker()
|
||||
s.startEventScheduler()
|
||||
}
|
||||
|
||||
return s
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (s *Server) registerStaticRoutes() {
|
||||
@@ -378,8 +401,5 @@ func (s *Server) Run(addr string) error {
|
||||
addr,
|
||||
)
|
||||
|
||||
return http.ListenAndServe(
|
||||
addr,
|
||||
s.handler(),
|
||||
)
|
||||
return s.RunContext(context.Background(), addr)
|
||||
}
|
||||
@@ -193,18 +193,10 @@ func (s *Server) taskPagePath(r *http.Request, id string) string {
|
||||
if id == "" {
|
||||
return ""
|
||||
}
|
||||
rows, err := s.store.DB.QueryContext(r.Context(), `SELECT path FROM paths WHERE domain_uuid=? AND object_uuid=? ORDER BY length(path),path LIMIT 100`, s.domainUUIDForRequest(r), id)
|
||||
paths, err := s.store.ObjectAliasPaths(r.Context(), s.domainUUIDForRequest(r), id)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
paths := []string{}
|
||||
for rows.Next() {
|
||||
var p string
|
||||
if rows.Scan(&p) == nil {
|
||||
paths = append(paths, p)
|
||||
}
|
||||
}
|
||||
rows.Close()
|
||||
allows := s.pathReadFilter(r)
|
||||
for _, p := range paths {
|
||||
if allows(p) {
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestActivityConsumersBackend(t *testing.T) {
|
||||
for _, backend := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(backend), func(t *testing.T) {
|
||||
s := testNamespaceStore(t, backend)
|
||||
ctx := context.Background()
|
||||
for range 2 {
|
||||
if err := s.EnsureActivityOutboxSchema(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// Source and its envelope share one transaction, including rollback.
|
||||
appendEvents := func(commit bool) {
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err = s.lockTaskWriter(ctx, tx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, domain := range []string{"synthetic-a", "synthetic-b"} {
|
||||
for i, typ := range []string{"task.changed", "noise", "page.changed", "task.changed"} {
|
||||
e := ActivityOutboxEvent{DomainUUID: domain, EventType: typ, IdempotencyKey: fmt.Sprint(i)}
|
||||
out, created, err := s.AppendActivityOutboxTx(tx, e)
|
||||
if err != nil || !created || out.Sequence != int64(i+1) {
|
||||
t.Fatal(out, created, err)
|
||||
}
|
||||
again, created, err := s.AppendActivityOutboxTx(tx, e)
|
||||
if err != nil || created || again.UUID != out.UUID {
|
||||
t.Fatal(again, created, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
if commit {
|
||||
if err = tx.Commit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
appendEvents(false)
|
||||
sub := ActivitySubscription{ConsumerID: "consumer-a", EventTypes: []string{"task.changed", "page.changed"}}
|
||||
if domains, err := s.PendingActivityConsumerDomains(ctx, sub, 100); err != nil || len(domains) != 0 {
|
||||
t.Fatal(domains, err)
|
||||
}
|
||||
appendEvents(true)
|
||||
if domains, err := s.PendingActivityConsumerDomains(ctx, sub, 100); err != nil || len(domains) != 2 {
|
||||
t.Fatal(domains, err)
|
||||
}
|
||||
items, err := s.ActivityOutboxForConsumer(ctx, sub, "synthetic-a", 2)
|
||||
if err != nil || len(items) != 2 || items[0].Sequence != 1 || items[1].Sequence != 3 {
|
||||
t.Fatal(items, err)
|
||||
}
|
||||
if err = s.CompleteActivityOutboxThrough(sub.ConsumerID, "synthetic-a", 3); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, err := s.ActivityOutboxCheckpoint(sub.ConsumerID, "synthetic-a")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.CompleteActivityOutboxThrough(sub.ConsumerID, "synthetic-a", 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
after, err := s.ActivityOutboxCheckpoint(sub.ConsumerID, "synthetic-a")
|
||||
if err != nil || before != after {
|
||||
t.Fatal(before, after, err)
|
||||
}
|
||||
items, err = s.ActivityOutboxForConsumer(ctx, sub, "synthetic-a", 10)
|
||||
if err != nil || len(items) != 1 || items[0].Sequence != 4 {
|
||||
t.Fatal(items, err)
|
||||
}
|
||||
// Another subscriber still sees the complete history.
|
||||
sub.ConsumerID = "consumer-b"
|
||||
items, err = s.ActivityOutboxForConsumer(ctx, sub, "synthetic-a", 10)
|
||||
if err != nil || len(items) != 3 {
|
||||
t.Fatal(items, err)
|
||||
}
|
||||
items, err = s.ActivityOutboxAfter(ctx, "synthetic-b", 2, 10)
|
||||
if err != nil || len(items) != 2 {
|
||||
t.Fatal(items, err)
|
||||
}
|
||||
items, err = s.PendingActivityOutbox(ctx, "synthetic-a", 10)
|
||||
if err != nil || len(items) != 4 {
|
||||
t.Fatal(items, err)
|
||||
}
|
||||
// Consumer action and checkpoint rollback together.
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.AdvanceActivityOutboxCheckpointTx(tx, "consumer-b", "synthetic-a", 4); err != nil {
|
||||
tx.Rollback()
|
||||
t.Fatal(err)
|
||||
}
|
||||
tx.Rollback()
|
||||
checkpoint, err := s.ActivityOutboxCheckpoint("consumer-b", "synthetic-a")
|
||||
if err != nil || checkpoint.LastSequence != 0 {
|
||||
t.Fatal(checkpoint, err)
|
||||
}
|
||||
// Simultaneous acknowledgments must not lose the larger watermark.
|
||||
var wg sync.WaitGroup
|
||||
for _, seq := range []int64{4, 2, 3, 1} {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
if err := s.CompleteActivityOutboxThrough("consumer-b", "synthetic-a", seq); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
checkpoint, err = s.ActivityOutboxCheckpoint("consumer-b", "synthetic-a")
|
||||
if err != nil || checkpoint.LastSequence != 4 {
|
||||
t.Fatal(checkpoint, err)
|
||||
}
|
||||
if domains, err := s.PendingActivityConsumerDomains(ctx, sub, 100); err != nil || len(domains) != 1 || domains[0] != "synthetic-b" {
|
||||
t.Fatal(domains, err)
|
||||
}
|
||||
s.ReadOnly = true
|
||||
if err = s.CompleteActivityOutboxThrough("consumer-b", "synthetic-b", 4); !errors.Is(err, ErrReadOnly) {
|
||||
t.Fatal("readonly", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
// The immutable journal and typed heads belong to core-metadata. Native
|
||||
// writers advance heads in the source transaction; no SQLite trigger/backfill
|
||||
// is run here. Consumer cursors are an independent schema component.
|
||||
func (s *Store) ensurePostgreSQLActivityConsumers(ctx context.Context) error {
|
||||
return db.ApplyPostgreSQLMigrations(ctx, s.DB, "store-activity-consumers", []db.PostgreSQLMigration{{Version: 1, Name: "independent activity consumer checkpoints", Statements: []string{
|
||||
`CREATE TABLE activity_outbox_checkpoints(consumer_id TEXT COLLATE "C" NOT NULL,domain_uuid TEXT COLLATE "C" NOT NULL,last_sequence BIGINT NOT NULL DEFAULT 0 CHECK(last_sequence>=0),updated_at_ms BIGINT NOT NULL,PRIMARY KEY(consumer_id,domain_uuid))`,
|
||||
`CREATE INDEX idx_activity_heads_type ON activity_outbox_heads(event_type,domain_uuid,last_sequence)`,
|
||||
`CREATE INDEX idx_activity_outbox_subscription ON activity_outbox(domain_uuid,event_type,sequence)`,
|
||||
`CREATE INDEX idx_activity_outbox_pending ON activity_outbox(status,next_attempt_ms,domain_uuid,sequence)`,
|
||||
`CREATE INDEX idx_activity_outbox_target ON activity_outbox(domain_uuid,target_uuid,sequence)`,
|
||||
}, Assertions: []string{
|
||||
`SELECT consumer_id,domain_uuid,last_sequence,updated_at_ms FROM activity_outbox_checkpoints WHERE 1=0`,
|
||||
`SELECT ` + activityOutboxColumns + ` FROM activity_outbox WHERE 1=0`,
|
||||
`SELECT domain_uuid,event_type,last_sequence FROM activity_outbox_heads WHERE 1=0`,
|
||||
}}})
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"math"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -53,6 +54,14 @@ type ActivityOutboxCheckpoint struct {
|
||||
var ErrActivityOutboxInput = errors.New("invalid activity outbox event")
|
||||
|
||||
func (s *Store) EnsureActivityOutboxSchema() error {
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
return s.ensurePostgreSQLActivityConsumers(ctx)
|
||||
}
|
||||
statements := []string{
|
||||
`CREATE TABLE IF NOT EXISTS activity_outbox (
|
||||
uuid TEXT PRIMARY KEY,
|
||||
@@ -97,6 +106,9 @@ func (s *Store) EnsureActivityOutboxSchema() error {
|
||||
// source mutation. Repeated idempotency keys return the original envelope and
|
||||
// never create a second Domain sequence number.
|
||||
func (s *Store) AppendActivityOutboxTx(tx *sql.Tx, event ActivityOutboxEvent) (ActivityOutboxRecord, bool, error) {
|
||||
if s.ReadOnly {
|
||||
return ActivityOutboxRecord{}, false, ErrReadOnly
|
||||
}
|
||||
event.DomainUUID = strings.TrimSpace(event.DomainUUID)
|
||||
event.EventType = strings.TrimSpace(event.EventType)
|
||||
event.IdempotencyKey = strings.TrimSpace(event.IdempotencyKey)
|
||||
@@ -106,6 +118,9 @@ func (s *Store) AppendActivityOutboxTx(tx *sql.Tx, event ActivityOutboxEvent) (A
|
||||
if event.UUID == "" {
|
||||
event.UUID = uuid.NewString()
|
||||
}
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
return s.appendNativeActivityOutboxTx(tx, event)
|
||||
}
|
||||
var existing ActivityOutboxRecord
|
||||
err := scanActivityOutbox(tx.QueryRow(`SELECT `+activityOutboxColumns+`
|
||||
FROM activity_outbox WHERE domain_uuid=? AND idempotency_key=?`, event.DomainUUID, event.IdempotencyKey), &existing)
|
||||
@@ -137,6 +152,11 @@ func (s *Store) AppendActivityOutboxTx(tx *sql.Tx, event ActivityOutboxEvent) (A
|
||||
// intentionally construct only a subset of the schema) usable while every
|
||||
// normal Store created through New has the outbox table.
|
||||
func (s *Store) appendActivityOutboxIfAvailable(tx *sql.Tx, event ActivityOutboxEvent) error {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
// Native schemas are mandatory: absence is an error, not a lost event.
|
||||
_, _, err := s.AppendActivityOutboxTx(tx, event)
|
||||
return err
|
||||
}
|
||||
var present int
|
||||
if err := tx.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='activity_outbox'`).Scan(&present); err != nil {
|
||||
return err
|
||||
@@ -148,15 +168,55 @@ func (s *Store) appendActivityOutboxIfAvailable(tx *sql.Tx, event ActivityOutbox
|
||||
return err
|
||||
}
|
||||
|
||||
// Native application writers hold the namespace advisory lock before any
|
||||
// mutation. This shares sequence allocation with metadata.NewApplication.
|
||||
func (s *Store) appendNativeActivityOutboxTx(tx *sql.Tx, e ActivityOutboxEvent) (ActivityOutboxRecord, bool, error) {
|
||||
var result ActivityOutboxRecord
|
||||
err := scanActivityOutbox(tx.QueryRow(`SELECT `+activityOutboxColumns+` FROM activity_outbox WHERE domain_uuid=$1 AND idempotency_key=$2`, e.DomainUUID, e.IdempotencyKey), &result)
|
||||
if err == nil {
|
||||
return result, false, nil
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
return result, false, err
|
||||
}
|
||||
var last int64
|
||||
if err = tx.QueryRow(`SELECT COALESCE(MAX(last_sequence),0) FROM activity_outbox_heads WHERE domain_uuid=$1`, e.DomainUUID).Scan(&last); err != nil {
|
||||
return result, false, err
|
||||
}
|
||||
if last == math.MaxInt64 {
|
||||
return result, false, ErrActivityOutboxInput
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
_, err = tx.Exec(`INSERT INTO activity_outbox(uuid,domain_uuid,sequence,event_type,actor_uuid,target_kind,target_uuid,payload,idempotency_key,next_attempt_ms,created_at_ms) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)`, e.UUID, e.DomainUUID, last+1, e.EventType, e.ActorUUID, e.TargetKind, e.TargetUUID, e.Payload, e.IdempotencyKey, now, now)
|
||||
if err != nil {
|
||||
return result, false, err
|
||||
}
|
||||
_, err = tx.Exec(`INSERT INTO activity_outbox_heads(domain_uuid,event_type,last_sequence) VALUES($1,$2,$3) ON CONFLICT(domain_uuid,event_type) DO UPDATE SET last_sequence=excluded.last_sequence`, e.DomainUUID, e.EventType, last+1)
|
||||
if err != nil {
|
||||
return result, false, err
|
||||
}
|
||||
err = scanActivityOutbox(tx.QueryRow(`SELECT `+activityOutboxColumns+` FROM activity_outbox WHERE uuid=$1`, e.UUID), &result)
|
||||
return result, err == nil, err
|
||||
}
|
||||
|
||||
func (s *Store) appendRevisionActivityOutboxIfAvailable(tx *sql.Tx, revision *Revision) error {
|
||||
// Legacy unit fixtures may omit the entire outbox. Check that explicitly;
|
||||
// once it exists, an unexpected source lookup error must abort the save,
|
||||
// not silently commit a revision with no event.
|
||||
var present int
|
||||
if err := tx.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='activity_outbox'`).Scan(&present); err != nil {
|
||||
return err
|
||||
}
|
||||
if present == 0 {
|
||||
return nil
|
||||
}
|
||||
var domainUUID string
|
||||
err := tx.QueryRow(`SELECT domain_uuid FROM paths WHERE object_uuid=? ORDER BY is_primary DESC, path LIMIT 1`, revision.ObjectUUID).Scan(&domainUUID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
// Asset-only or legacy fixtures may not have a path for the object.
|
||||
return nil
|
||||
return err
|
||||
}
|
||||
payload, err := json.Marshal(map[string]any{
|
||||
"revision_uuid": revision.UUID, "object_uuid": revision.ObjectUUID,
|
||||
@@ -166,10 +226,11 @@ func (s *Store) appendRevisionActivityOutboxIfAvailable(tx *sql.Tx, revision *Re
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.appendActivityOutboxIfAvailable(tx, ActivityOutboxEvent{
|
||||
_, _, err = s.AppendActivityOutboxTx(tx, ActivityOutboxEvent{
|
||||
DomainUUID: domainUUID, EventType: "revision.created", TargetKind: "revision", TargetUUID: revision.UUID,
|
||||
Payload: string(payload), IdempotencyKey: "revision.created:" + revision.UUID,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
const activityOutboxColumns = `uuid,domain_uuid,sequence,event_type,actor_uuid,target_kind,target_uuid,payload,idempotency_key,status,attempts,next_attempt_ms,last_error,created_at_ms,claimed_at_ms`
|
||||
@@ -185,13 +246,16 @@ func scanActivityOutbox(scanner interface{ Scan(...any) error }, out *ActivityOu
|
||||
// authorization policy without making reads of the source object implicit.
|
||||
// Deprecated: this is a legacy-status inspection, not consumer pending work.
|
||||
// New workers must use ActivityOutboxForConsumer and their own checkpoint.
|
||||
func (s *Store) PendingActivityOutbox(ctx context.Context, domainUUID string, limit int) ([]ActivityOutboxRecord, error) {
|
||||
func (s *Store) PendingActivityOutbox(ctx context.Context, domainUUID string, limit int) (_ []ActivityOutboxRecord, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("pending activity", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
if limit < 1 || limit > 500 {
|
||||
limit = 100
|
||||
}
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT `+activityOutboxColumns+` FROM activity_outbox
|
||||
WHERE status='pending' AND next_attempt_ms<=? AND (?='' OR domain_uuid=?)
|
||||
ORDER BY domain_uuid,sequence LIMIT ?`, time.Now().UnixMilli(), domainUUID, domainUUID, limit)
|
||||
WHERE status='pending' AND next_attempt_ms<=`+s.sqlParam(1)+` AND (`+s.sqlParam(2)+`='' OR domain_uuid=`+s.sqlParam(3)+`)
|
||||
ORDER BY domain_uuid,sequence LIMIT `+s.sqlParam(4), time.Now().UnixMilli(), domainUUID, domainUUID, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -210,7 +274,10 @@ func (s *Store) PendingActivityOutbox(ctx context.Context, domainUUID string, li
|
||||
// ActivityOutboxAfter reads the immutable Domain sequence independently for
|
||||
// each consumer. It intentionally includes every status: a consumer's
|
||||
// checkpoint, rather than a shared job status, is the delivery contract.
|
||||
func (s *Store) ActivityOutboxAfter(ctx context.Context, domainUUID string, afterSequence int64, limit int) ([]ActivityOutboxRecord, error) {
|
||||
func (s *Store) ActivityOutboxAfter(ctx context.Context, domainUUID string, afterSequence int64, limit int) (_ []ActivityOutboxRecord, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("activity history", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
if strings.TrimSpace(domainUUID) == "" || afterSequence < 0 {
|
||||
return nil, ErrActivityOutboxInput
|
||||
}
|
||||
@@ -218,7 +285,7 @@ func (s *Store) ActivityOutboxAfter(ctx context.Context, domainUUID string, afte
|
||||
limit = 100
|
||||
}
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT `+activityOutboxColumns+` FROM activity_outbox
|
||||
WHERE domain_uuid=? AND sequence>? ORDER BY sequence LIMIT ?`, domainUUID, afterSequence, limit)
|
||||
WHERE domain_uuid=`+s.sqlParam(1)+` AND sequence>`+s.sqlParam(2)+` ORDER BY sequence LIMIT `+s.sqlParam(3), domainUUID, afterSequence, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -234,13 +301,16 @@ func (s *Store) ActivityOutboxAfter(ctx context.Context, domainUUID string, afte
|
||||
return items, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) ActivityOutboxCheckpoint(consumerID, domainUUID string) (ActivityOutboxCheckpoint, error) {
|
||||
func (s *Store) ActivityOutboxCheckpoint(consumerID, domainUUID string) (_ ActivityOutboxCheckpoint, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("activity checkpoint", resultErr) }()
|
||||
consumerID, domainUUID = strings.TrimSpace(consumerID), strings.TrimSpace(domainUUID)
|
||||
if consumerID == "" || domainUUID == "" {
|
||||
return ActivityOutboxCheckpoint{}, ErrActivityOutboxInput
|
||||
}
|
||||
var checkpoint ActivityOutboxCheckpoint
|
||||
err := s.DB.QueryRow(`SELECT consumer_id,domain_uuid,last_sequence,updated_at_ms FROM activity_outbox_checkpoints WHERE consumer_id=? AND domain_uuid=?`, consumerID, domainUUID).
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT consumer_id,domain_uuid,last_sequence,updated_at_ms FROM activity_outbox_checkpoints WHERE consumer_id=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2), consumerID, domainUUID).
|
||||
Scan(&checkpoint.ConsumerID, &checkpoint.DomainUUID, &checkpoint.LastSequence, &checkpoint.UpdatedAtMS)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return ActivityOutboxCheckpoint{ConsumerID: consumerID, DomainUUID: domainUUID}, nil
|
||||
@@ -251,25 +321,35 @@ func (s *Store) ActivityOutboxCheckpoint(consumerID, domainUUID string) (Activit
|
||||
// AdvanceActivityOutboxCheckpointTx is monotonic and safe to call after an
|
||||
// idempotently completed consumer action. A stale retry can never move the
|
||||
// checkpoint backwards.
|
||||
func (s *Store) AdvanceActivityOutboxCheckpointTx(tx *sql.Tx, consumerID, domainUUID string, sequence int64) error {
|
||||
func (s *Store) AdvanceActivityOutboxCheckpointTx(tx *sql.Tx, consumerID, domainUUID string, sequence int64) (resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("activity checkpoint", resultErr) }()
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
consumerID, domainUUID = strings.TrimSpace(consumerID), strings.TrimSpace(domainUUID)
|
||||
if consumerID == "" || domainUUID == "" || sequence < 0 {
|
||||
return ErrActivityOutboxInput
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
_, err := tx.Exec(`INSERT INTO activity_outbox_checkpoints(consumer_id,domain_uuid,last_sequence,updated_at_ms)
|
||||
VALUES(?,?,?,?) ON CONFLICT(consumer_id,domain_uuid) DO UPDATE SET
|
||||
last_sequence=CASE WHEN excluded.last_sequence>last_sequence THEN excluded.last_sequence ELSE last_sequence END,
|
||||
updated_at_ms=CASE WHEN excluded.last_sequence>last_sequence THEN excluded.updated_at_ms ELSE updated_at_ms END`, consumerID, domainUUID, sequence, now)
|
||||
VALUES(`+s.sqlValues(4)+`) ON CONFLICT(consumer_id,domain_uuid) DO UPDATE SET
|
||||
last_sequence=CASE WHEN excluded.last_sequence>activity_outbox_checkpoints.last_sequence THEN excluded.last_sequence ELSE activity_outbox_checkpoints.last_sequence END,
|
||||
updated_at_ms=CASE WHEN excluded.last_sequence>activity_outbox_checkpoints.last_sequence THEN excluded.updated_at_ms ELSE activity_outbox_checkpoints.updated_at_ms END`, consumerID, domainUUID, sequence, now)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) AdvanceActivityOutboxCheckpoint(consumerID, domainUUID string, sequence int64) error {
|
||||
tx, err := s.DB.Begin()
|
||||
func (s *Store) AdvanceActivityOutboxCheckpoint(consumerID, domainUUID string, sequence int64) (err error) {
|
||||
defer func() { err = s.sqlError("activity checkpoint", err) }()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err = s.lockTaskWriter(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.AdvanceActivityOutboxCheckpointTx(tx, consumerID, domainUUID, sequence); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -278,7 +358,10 @@ func (s *Store) AdvanceActivityOutboxCheckpoint(consumerID, domainUUID string, s
|
||||
|
||||
// Deprecated: use PendingActivityConsumerDomains; a shared status cannot
|
||||
// represent delivery to independent subscribers.
|
||||
func (s *Store) PendingActivityOutboxDomains(ctx context.Context) ([]string, error) {
|
||||
func (s *Store) PendingActivityOutboxDomains(ctx context.Context) (_ []string, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("pending activity domains", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT DISTINCT domain_uuid FROM activity_outbox WHERE status='pending' ORDER BY domain_uuid LIMIT 100`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -9,6 +9,43 @@ import (
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
func TestRevisionOutboxLookupFailureRollsBackSource(t *testing.T) {
|
||||
database, err := db.Open(t.TempDir() + "/outbox.db")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer database.Close()
|
||||
if err := db.Migrate(database); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New(database)
|
||||
id, err := s.CreatePageAtPath("/guide")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.SaveRevision(id, "<p>original</p>", "first"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var before int
|
||||
if err := database.QueryRow(`SELECT count(*) FROM activity_outbox`).Scan(&before); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Keep object/revision storage valid but break only the event source lookup.
|
||||
if _, err := database.Exec(`ALTER TABLE paths RENAME COLUMN domain_uuid TO misplaced_domain_uuid`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.SaveRevision(id, "<p>must roll back</p>", "second"); err == nil {
|
||||
t.Fatal("source lookup error was swallowed")
|
||||
}
|
||||
var revisions, events int
|
||||
if err := database.QueryRow(`SELECT count(*) FROM revisions WHERE object_uuid=?`, id).Scan(&revisions); err != nil || revisions != 1 {
|
||||
t.Fatal("revision committed without outbox", err, revisions)
|
||||
}
|
||||
if err := database.QueryRow(`SELECT count(*) FROM activity_outbox`).Scan(&events); err != nil || events != before {
|
||||
t.Fatal("partial outbox commit", err, events)
|
||||
}
|
||||
}
|
||||
|
||||
func TestActivityOutboxIdempotencyAndDomainSequence(t *testing.T) {
|
||||
db, err := sql.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
|
||||
@@ -3,6 +3,7 @@ package store
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Subscriptions are trusted worker configuration, never request parameters.
|
||||
@@ -67,7 +68,10 @@ func (s *Store) ensureActivitySubscriptions() error {
|
||||
|
||||
// PendingActivityConsumerDomains does not scan event history or rely on the
|
||||
// legacy shared status. Acknowledgment by another consumer cannot hide work.
|
||||
func (s *Store) PendingActivityConsumerDomains(ctx context.Context, sub ActivitySubscription, limit int) ([]string, error) {
|
||||
func (s *Store) PendingActivityConsumerDomains(ctx context.Context, sub ActivitySubscription, limit int) (_ []string, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("activity subscriber domains", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
marks, args, err := sub.filter()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -77,7 +81,14 @@ func (s *Store) PendingActivityConsumerDomains(ctx context.Context, sub Activity
|
||||
}
|
||||
args = append([]any{sub.ConsumerID}, args...)
|
||||
args = append(args, limit)
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT h.domain_uuid FROM activity_outbox_heads h LEFT JOIN activity_outbox_checkpoints c ON c.domain_uuid=h.domain_uuid AND c.consumer_id=? WHERE h.event_type IN (`+marks+`) AND h.last_sequence>COALESCE(c.last_sequence,0) GROUP BY h.domain_uuid ORDER BY COALESCE(c.updated_at_ms,0),h.domain_uuid LIMIT ?`, args...)
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
params := make([]string, len(sub.EventTypes))
|
||||
for i := range params {
|
||||
params[i] = s.sqlParam(i + 2)
|
||||
}
|
||||
marks = strings.Join(params, ",")
|
||||
}
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT h.domain_uuid FROM activity_outbox_heads h LEFT JOIN activity_outbox_checkpoints c ON c.domain_uuid=h.domain_uuid AND c.consumer_id=`+s.sqlParam(1)+` WHERE h.event_type IN (`+marks+`) AND h.last_sequence>COALESCE(c.last_sequence,0) GROUP BY h.domain_uuid ORDER BY MIN(COALESCE(c.updated_at_ms,0)),h.domain_uuid LIMIT `+s.sqlParam(len(args)), args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -95,7 +106,10 @@ func (s *Store) PendingActivityConsumerDomains(ctx context.Context, sub Activity
|
||||
|
||||
// The SQL limit is applied per type first, so unrelated floods and SQLite's
|
||||
// final multi-type ordering cannot materialize an unbounded pending backlog.
|
||||
func (s *Store) ActivityOutboxForConsumer(ctx context.Context, sub ActivitySubscription, domain string, limit int) ([]ActivityOutboxRecord, error) {
|
||||
func (s *Store) ActivityOutboxForConsumer(ctx context.Context, sub ActivitySubscription, domain string, limit int) (_ []ActivityOutboxRecord, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("activity subscriber history", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
_, _, err := sub.filter()
|
||||
if err != nil || domain == "" {
|
||||
return nil, ErrActivityOutboxInput
|
||||
@@ -106,11 +120,12 @@ func (s *Store) ActivityOutboxForConsumer(ctx context.Context, sub ActivitySubsc
|
||||
queries := []string{}
|
||||
args := []any{}
|
||||
for _, typ := range sub.EventTypes {
|
||||
queries = append(queries, `SELECT * FROM (SELECT `+activityOutboxColumns+` FROM activity_outbox WHERE domain_uuid=? AND event_type=? AND sequence>COALESCE((SELECT last_sequence FROM activity_outbox_checkpoints WHERE consumer_id=? AND domain_uuid=?),0) ORDER BY sequence LIMIT ?)`)
|
||||
i := len(args)
|
||||
queries = append(queries, `SELECT * FROM (SELECT `+activityOutboxColumns+` FROM activity_outbox WHERE domain_uuid=`+s.sqlParam(i+1)+` AND event_type=`+s.sqlParam(i+2)+` AND sequence>COALESCE((SELECT last_sequence FROM activity_outbox_checkpoints WHERE consumer_id=`+s.sqlParam(i+3)+` AND domain_uuid=`+s.sqlParam(i+4)+`),0) ORDER BY sequence LIMIT `+s.sqlParam(i+5)+`) AS typed_events`)
|
||||
args = append(args, domain, typ, sub.ConsumerID, domain, limit)
|
||||
}
|
||||
args = append(args, limit)
|
||||
rows, err := s.DB.QueryContext(ctx, strings.Join(queries, " UNION ")+` ORDER BY sequence LIMIT ?`, args...)
|
||||
rows, err := s.DB.QueryContext(ctx, strings.Join(queries, " UNION ")+` ORDER BY sequence LIMIT `+s.sqlParam(len(args)), args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -50,6 +50,9 @@ func (s *Store) IssueAdapterTicketForPrincipal(principalUUID, domainUUID, plugin
|
||||
}
|
||||
|
||||
func (s *Store) issueAdapterTicket(principalUUID, domainUUID, pluginID, objectUUID, revisionUUID, rootPath string) (string, error) {
|
||||
if !s.SupportsHostedEditors() {
|
||||
return "", ErrBackendFeatureUnavailable
|
||||
}
|
||||
token, hash, err := adapterSecret("pwa_t_")
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -70,6 +73,9 @@ func secretHash(token string) string {
|
||||
}
|
||||
|
||||
func (s *Store) ExchangeAdapterTicket(ticket string) (string, *AdapterSession, error) {
|
||||
if !s.SupportsHostedEditors() {
|
||||
return "", nil, ErrBackendFeatureUnavailable
|
||||
}
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
@@ -111,6 +117,9 @@ func (s *Store) ExchangeAdapterTicket(ticket string) (string, *AdapterSession, e
|
||||
}
|
||||
|
||||
func (s *Store) GetAdapterSession(token string) (*AdapterSession, error) {
|
||||
if !s.SupportsHostedEditors() {
|
||||
return nil, ErrBackendFeatureUnavailable
|
||||
}
|
||||
var session AdapterSession
|
||||
err := s.DB.QueryRow(`SELECT principal_uuid,domain_uuid,plugin_id,object_uuid,base_revision_uuid,root_path,
|
||||
session_hash,session_expires_at_ms FROM adapter_sessions WHERE session_hash=?`,
|
||||
@@ -123,10 +132,16 @@ func (s *Store) GetAdapterSession(token string) (*AdapterSession, error) {
|
||||
}
|
||||
|
||||
func (s *Store) SaveHostedRevision(session *AdapterSession, payload []byte, message string) (*Revision, error) {
|
||||
if !s.SupportsHostedEditors() {
|
||||
return nil, ErrBackendFeatureUnavailable
|
||||
}
|
||||
return s.SaveHostedRevisionAt(session, session.ObjectUUID, session.BaseRevisionUUID, payload, message)
|
||||
}
|
||||
|
||||
func (s *Store) SaveHostedRevisionAt(session *AdapterSession, objectUUID, baseRevisionUUID string, payload []byte, message string) (*Revision, error) {
|
||||
if !s.SupportsHostedEditors() {
|
||||
return nil, ErrBackendFeatureUnavailable
|
||||
}
|
||||
s.revisionWriteMu.Lock()
|
||||
defer s.revisionWriteMu.Unlock()
|
||||
base, err := s.GetRevision(baseRevisionUUID)
|
||||
|
||||
@@ -0,0 +1,825 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/calendar"
|
||||
"pageswiki/internal/metadata"
|
||||
)
|
||||
|
||||
type ImportedTable struct {
|
||||
Name string `json:"name"`
|
||||
Rows int64 `json:"rows"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
type applicationImportRefusal struct{ reason string }
|
||||
|
||||
func (e *applicationImportRefusal) Error() string { return e.reason }
|
||||
func importRefuse(reason string) error { return &applicationImportRefusal{reason} }
|
||||
func importRefusef(format string, args ...any) error {
|
||||
return importRefuse(fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
type ApplicationImportReport struct {
|
||||
Tables []ImportedTable `json:"tables"`
|
||||
Omitted []string `json:"omitted"`
|
||||
Documents int64 `json:"documents"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
Sequences map[string]int64 `json:"sequences"`
|
||||
Transforms []string `json:"transforms"`
|
||||
}
|
||||
|
||||
// ImportSQLiteApplication imports into an EMPTY, already prepared PostgreSQL
|
||||
// schema. The caller holds an exclusive data-directory lock and stops all
|
||||
// writers. Files stay at documentRoot; no source rows or canonical files are
|
||||
// changed. This is not a merge or an online migration.
|
||||
func ImportSQLiteApplication(ctx context.Context, source, target *sql.DB, documentRoot string) (report ApplicationImportReport, err error) {
|
||||
key, err := existingAuthKey(documentRoot)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
root, err := filepath.Abs(documentRoot)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
if info, e := os.Stat(root); e != nil || !info.IsDir() {
|
||||
return report, ErrDocumentStoreRequired
|
||||
}
|
||||
s := &Store{DB: target, sqlBackend: storePostgreSQL, DocumentRoot: root}
|
||||
defer func() {
|
||||
if err != nil {
|
||||
var refused *applicationImportRefusal
|
||||
if errors.As(err, &refused) {
|
||||
return
|
||||
}
|
||||
err = s.sqlError("application import", err)
|
||||
}
|
||||
}()
|
||||
from, err := source.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
defer from.Rollback()
|
||||
tables, err := inspectApplicationImportSource(ctx, from)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
to, err := target.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelSerializable})
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
defer to.Rollback()
|
||||
if err = inspectApplicationImportTarget(ctx, to); err != nil {
|
||||
return report, err
|
||||
}
|
||||
if _, err = to.ExecContext(ctx, `SELECT pg_advisory_xact_lock(1885431653,1853058931)`); err != nil {
|
||||
return report, err
|
||||
}
|
||||
var completed bool
|
||||
if err = to.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM application_import_receipts)`).Scan(&completed); err != nil {
|
||||
return report, err
|
||||
}
|
||||
if completed {
|
||||
return report, importRefuse("application import already completed; overwrite is forbidden")
|
||||
}
|
||||
names := make([]string, 0, len(applicationImportColumns))
|
||||
for name := range applicationImportColumns {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
// Validate all destination columns and take table locks before emptiness
|
||||
// checks. Only exact schema-provided seed rows may be removed.
|
||||
for _, name := range names {
|
||||
if _, err = to.ExecContext(ctx, `LOCK TABLE `+importIdent(name)+` IN ACCESS EXCLUSIVE MODE`); err != nil {
|
||||
return report, err
|
||||
}
|
||||
var n int64
|
||||
if err = to.QueryRowContext(ctx, `SELECT count(*) FROM `+importIdent(name)).Scan(&n); err != nil {
|
||||
return report, err
|
||||
}
|
||||
if n > 0 {
|
||||
predicate, ok := importSeedPredicates[name]
|
||||
if !ok {
|
||||
return report, importRefusef("import destination is not empty: %s", name)
|
||||
}
|
||||
var valid bool
|
||||
if err = to.QueryRowContext(ctx, `SELECT count(*)=1 AND bool_and(`+predicate+`) FROM `+importIdent(name)).Scan(&valid); err != nil {
|
||||
return report, err
|
||||
}
|
||||
if !valid {
|
||||
return report, importRefusef("import destination seed has changed: %s", name)
|
||||
}
|
||||
}
|
||||
// User triggers derive state or revoke publication on normal writes.
|
||||
// Suspend them only inside this transaction while restoring exact state.
|
||||
// Foreign keys and CHECK/UNIQUE constraints stay enabled throughout.
|
||||
if _, err = to.ExecContext(ctx, `ALTER TABLE `+importIdent(name)+` DISABLE TRIGGER USER`); err != nil {
|
||||
return report, err
|
||||
}
|
||||
}
|
||||
for name := range importSeedPredicates {
|
||||
if _, err = to.ExecContext(ctx, `DELETE FROM `+importIdent(name)); err != nil {
|
||||
return report, err
|
||||
}
|
||||
}
|
||||
order, err := importTableOrder(ctx, to, names)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
for _, name := range order {
|
||||
columns := applicationImportColumns[name]
|
||||
if !tables[name] {
|
||||
if !strings.HasPrefix(name, "calendar_") {
|
||||
return report, importRefusef("missing source table: %s", name)
|
||||
}
|
||||
continue
|
||||
}
|
||||
fingerprint, err := copyImportTable(ctx, from, to, name, strings.Split(columns, ","))
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
report.Tables = append(report.Tables, fingerprint)
|
||||
}
|
||||
// Source-only structures are explicitly classified, not inferred by prefix.
|
||||
for name := range tables {
|
||||
if _, ok := applicationImportOmissions[name]; ok {
|
||||
report.Omitted = append(report.Omitted, name)
|
||||
}
|
||||
}
|
||||
sort.Strings(report.Omitted)
|
||||
report.Transforms = []string{"task DAV index keys: SQLite NUL-separated to native v1 base64url (resources and deletion tombstones)"}
|
||||
// Verify every retained Document reference before committing metadata.
|
||||
docs, err := s.documentStorage()
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
refs, err := to.QueryContext(ctx, `SELECT document_hash FROM revisions UNION SELECT head_document_hash FROM assets UNION SELECT document_hash FROM revision_document_refs UNION SELECT document_hash FROM asset_head_history`)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
for refs.Next() {
|
||||
var hash string
|
||||
if err = refs.Scan(&hash); err != nil {
|
||||
refs.Close()
|
||||
return report, err
|
||||
}
|
||||
if err = docs.Verify(ctx, hash); err != nil {
|
||||
refs.Close()
|
||||
return report, importRefuse("canonical Document missing or hash mismatch; import rolled back")
|
||||
}
|
||||
report.Documents++
|
||||
}
|
||||
err = refs.Err()
|
||||
refs.Close()
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
if err = validateImportedBindings(ctx, to); err != nil {
|
||||
return report, err
|
||||
}
|
||||
for _, name := range names {
|
||||
if _, err = to.ExecContext(ctx, `ALTER TABLE `+importIdent(name)+` ENABLE TRIGGER USER`); err != nil {
|
||||
return report, err
|
||||
}
|
||||
}
|
||||
// Sequence reset uses transactional ALTER, not non-transactional setval.
|
||||
report.Sequences, err = sourceImportSequences(ctx, from)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
if err = resetImportIdentities(ctx, to, report.Sequences); err != nil {
|
||||
return report, err
|
||||
}
|
||||
sort.Slice(report.Tables, func(i, j int) bool { return report.Tables[i].Name < report.Tables[j].Name })
|
||||
b, _ := json.Marshal(report)
|
||||
sum := sha256.Sum256(b)
|
||||
report.Fingerprint = hex.EncodeToString(sum[:])
|
||||
b, _ = json.Marshal(report)
|
||||
ks := sha256.Sum256(key)
|
||||
if _, err = to.ExecContext(ctx, `INSERT INTO application_import_receipts VALUES(1,$1,$2,$3,$4,$5)`, report.Fingerprint, root, hex.EncodeToString(ks[:]), string(b), time.Now().UnixMilli()); err != nil {
|
||||
return report, err
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return report, err
|
||||
}
|
||||
err = to.Commit()
|
||||
return report, err
|
||||
}
|
||||
|
||||
var importSeedPredicates = map[string]string{
|
||||
"organizer_contact_epoch": `id=1 AND value ~ '^[a-f0-9]{64}$'`,
|
||||
"organizer_activity_bridge": `id=1 AND last_sequence=0`,
|
||||
"navigation_generation": `id=1 AND version=1`,
|
||||
"event_queue_settings": `id=1 AND concurrency=1 AND max_depth=10000 AND paused=0 AND retry_base_seconds=30 AND retention_hours=168`,
|
||||
"event_log_policies": `event_type='*' AND retention_hours=168 AND aggregate_after_hours=24 AND bucket_seconds=60 AND max_rows=5000`,
|
||||
}
|
||||
|
||||
func importIdent(s string) string { return `"` + strings.ReplaceAll(s, `"`, `""`) + `"` }
|
||||
|
||||
func inspectApplicationImportSource(ctx context.Context, tx *sql.Tx) (map[string]bool, error) {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT name FROM sqlite_master WHERE type='table' AND substr(name,1,7)!='sqlite_' ORDER BY name`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := map[string]bool{}
|
||||
for rows.Next() {
|
||||
var n string
|
||||
if err = rows.Scan(&n); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
names[n] = true
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for name := range names {
|
||||
expected, ok := applicationImportColumns[name]
|
||||
if !ok {
|
||||
expected, ok = applicationImportOmissions[name]
|
||||
}
|
||||
if !ok {
|
||||
return nil, importRefusef("unclassified source table: %s", name)
|
||||
}
|
||||
cols, err := tx.QueryContext(ctx, `SELECT name FROM pragma_table_info(?) ORDER BY name`, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var got []string
|
||||
for cols.Next() {
|
||||
var n string
|
||||
if err = cols.Scan(&n); err != nil {
|
||||
cols.Close()
|
||||
return nil, err
|
||||
}
|
||||
got = append(got, n)
|
||||
}
|
||||
err = cols.Err()
|
||||
cols.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if strings.Join(got, ",") != expected {
|
||||
return nil, importRefusef("source columns differ: %s", name)
|
||||
}
|
||||
}
|
||||
for name := range applicationImportColumns {
|
||||
if !names[name] && !strings.HasPrefix(name, "calendar_") {
|
||||
return nil, importRefusef("missing source table: %s", name)
|
||||
}
|
||||
}
|
||||
objects, err := tx.QueryContext(ctx, `SELECT type,name,sql FROM sqlite_master WHERE type IN ('trigger','view') ORDER BY name`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for objects.Next() {
|
||||
var kind, name, ddl string
|
||||
if err = objects.Scan(&kind, &name, &ddl); err != nil {
|
||||
objects.Close()
|
||||
return nil, err
|
||||
}
|
||||
sum := sha256.Sum256([]byte(ddl))
|
||||
if applicationImportSourceObjects[name] != kind+":"+hex.EncodeToString(sum[:]) {
|
||||
objects.Close()
|
||||
return nil, importRefusef("unclassified or modified source %s: %s", kind, name)
|
||||
}
|
||||
}
|
||||
err = objects.Err()
|
||||
objects.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return names, nil
|
||||
}
|
||||
|
||||
func inspectApplicationImportTarget(ctx context.Context, tx *sql.Tx) error {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT table_name,column_name FROM information_schema.columns WHERE table_schema=current_schema() ORDER BY table_name,column_name`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
got := map[string][]string{}
|
||||
for rows.Next() {
|
||||
var t, c string
|
||||
if err = rows.Scan(&t, &c); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
got[t] = append(got[t], c)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for name, cols := range got {
|
||||
if name == "schema_migration_steps" || name == "application_import_receipts" {
|
||||
continue
|
||||
}
|
||||
expected, ok := applicationImportColumns[name]
|
||||
if !ok {
|
||||
return importRefusef("unclassified destination table: %s", name)
|
||||
}
|
||||
if name == "event_jobs" {
|
||||
expected += ",claim_token"
|
||||
p := strings.Split(expected, ",")
|
||||
sort.Strings(p)
|
||||
expected = strings.Join(p, ",")
|
||||
}
|
||||
if strings.Join(cols, ",") != expected {
|
||||
return importRefusef("destination columns differ: %s", name)
|
||||
}
|
||||
}
|
||||
for name := range applicationImportColumns {
|
||||
if _, ok := got[name]; !ok {
|
||||
return importRefusef("missing destination table: %s", name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func importTableOrder(ctx context.Context, tx *sql.Tx, names []string) ([]string, error) {
|
||||
deps := map[string][]string{}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT a.relname,b.relname FROM pg_constraint f JOIN pg_class a ON a.oid=f.conrelid JOIN pg_class b ON b.oid=f.confrelid JOIN pg_namespace n ON n.oid=a.relnamespace WHERE f.contype='f' AND n.nspname=current_schema()`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for rows.Next() {
|
||||
var a, b string
|
||||
if err = rows.Scan(&a, &b); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
if a != b {
|
||||
deps[a] = append(deps[a], b)
|
||||
}
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var order []string
|
||||
seen := map[string]bool{}
|
||||
for len(order) < len(names) {
|
||||
changed := false
|
||||
for _, name := range names {
|
||||
if seen[name] {
|
||||
continue
|
||||
}
|
||||
ready := true
|
||||
for _, dep := range deps[name] {
|
||||
ready = ready && seen[dep]
|
||||
}
|
||||
if ready {
|
||||
order = append(order, name)
|
||||
seen[name] = true
|
||||
changed = true
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
return nil, importRefuse("unsupported import foreign key dependency")
|
||||
}
|
||||
}
|
||||
return order, nil
|
||||
}
|
||||
|
||||
func importSelect(name string, columns []string, pg bool) string {
|
||||
quoted := make([]string, len(columns))
|
||||
for i, c := range columns {
|
||||
quoted[i] = importIdent(c)
|
||||
}
|
||||
// All columns participate, including duplicate legacy tags. SQL collation
|
||||
// is kept bytewise to produce the same deterministic row stream.
|
||||
order := make([]string, len(columns))
|
||||
for i, q := range quoted {
|
||||
if pg {
|
||||
order[i] = q + ` NULLS FIRST`
|
||||
} else {
|
||||
order[i] = q + ` COLLATE BINARY NULLS FIRST`
|
||||
}
|
||||
}
|
||||
if name == "organizer_task_deletions" {
|
||||
order = []string{`"domain_uuid" COLLATE BINARY`, `"item_uuid" COLLATE BINARY`}
|
||||
if pg {
|
||||
order = []string{`"domain_uuid" COLLATE "C"`, `"item_uuid" COLLATE "C"`}
|
||||
}
|
||||
}
|
||||
return `SELECT ` + strings.Join(quoted, ",") + ` FROM ` + importIdent(name) + ` ORDER BY ` + strings.Join(order, ",")
|
||||
}
|
||||
|
||||
func copyImportTable(ctx context.Context, from, to *sql.Tx, name string, columns []string) (ImportedTable, error) {
|
||||
result := ImportedTable{Name: name}
|
||||
hash := sha256.New()
|
||||
rows, err := from.QueryContext(ctx, importSelect(name, columns, false))
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer rows.Close()
|
||||
quoted := make([]string, len(columns))
|
||||
for i, c := range columns {
|
||||
quoted[i] = importIdent(c)
|
||||
}
|
||||
var batch [][]any
|
||||
bytes := 0
|
||||
flush := func() error {
|
||||
if len(batch) == 0 {
|
||||
return nil
|
||||
}
|
||||
args := make([]any, 0, len(batch)*len(columns))
|
||||
values := make([]string, 0, len(batch))
|
||||
for _, row := range batch {
|
||||
p := make([]string, len(columns))
|
||||
for i, v := range row {
|
||||
args = append(args, v)
|
||||
p[i] = fmt.Sprintf("$%d", len(args))
|
||||
}
|
||||
values = append(values, "("+strings.Join(p, ",")+")")
|
||||
}
|
||||
_, err := to.ExecContext(ctx, `INSERT INTO `+importIdent(name)+` (`+strings.Join(quoted, ",")+`) OVERRIDING SYSTEM VALUE VALUES `+strings.Join(values, ","), args...)
|
||||
batch = nil
|
||||
bytes = 0
|
||||
return err
|
||||
}
|
||||
for rows.Next() {
|
||||
v := make([]any, len(columns))
|
||||
dest := make([]any, len(v))
|
||||
for i := range v {
|
||||
dest[i] = &v[i]
|
||||
}
|
||||
if err = rows.Scan(dest...); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if err = normalizeImportRow(name, columns, v); err != nil {
|
||||
return result, err
|
||||
}
|
||||
b, err := importRowBytes(v)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if len(b) > 64<<20 {
|
||||
return result, importRefuse("import row exceeds 64 MiB safety limit")
|
||||
}
|
||||
hash.Write(b)
|
||||
hash.Write([]byte{'\n'})
|
||||
result.Rows++
|
||||
batch = append(batch, v)
|
||||
bytes += len(b)
|
||||
if len(batch) >= 100 || bytes >= 2<<20 {
|
||||
if err = flush(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
}
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
rows.Close()
|
||||
if err = flush(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.SHA256 = hex.EncodeToString(hash.Sum(nil))
|
||||
// Re-read retained values, not only row counts. Bytea/NULL/integer/text
|
||||
// retain their JSON type tags, so conversion or loss fails the transaction.
|
||||
selectSQL := importSelect(name, columns, true)
|
||||
// Force bytewise ordering for TEXT regardless of the database default.
|
||||
meta, err := to.QueryContext(ctx, `SELECT column_name FROM information_schema.columns WHERE table_schema=current_schema() AND table_name=$1 AND data_type IN ('text','character varying','character')`, name)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
textCols := map[string]bool{}
|
||||
for meta.Next() {
|
||||
var c string
|
||||
if err = meta.Scan(&c); err != nil {
|
||||
meta.Close()
|
||||
return result, err
|
||||
}
|
||||
textCols[c] = true
|
||||
}
|
||||
err = meta.Err()
|
||||
meta.Close()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
ordering := make([]string, len(columns))
|
||||
for i, c := range columns {
|
||||
ordering[i] = importIdent(c)
|
||||
if textCols[c] {
|
||||
ordering[i] += ` COLLATE "C"`
|
||||
}
|
||||
ordering[i] += " NULLS FIRST"
|
||||
}
|
||||
if name == "organizer_task_deletions" {
|
||||
ordering = []string{`"domain_uuid" COLLATE "C"`, `"item_uuid" COLLATE "C"`}
|
||||
}
|
||||
selectSQL = selectSQL[:strings.Index(selectSQL, " ORDER BY ")] + " ORDER BY " + strings.Join(ordering, ",")
|
||||
check, err := to.QueryContext(ctx, selectSQL)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
defer check.Close()
|
||||
h := sha256.New()
|
||||
var count int64
|
||||
for check.Next() {
|
||||
v := make([]any, len(columns))
|
||||
dest := make([]any, len(v))
|
||||
for i := range v {
|
||||
dest[i] = &v[i]
|
||||
}
|
||||
if err = check.Scan(dest...); err != nil {
|
||||
return result, err
|
||||
}
|
||||
b, err := importRowBytes(v)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
h.Write(b)
|
||||
h.Write([]byte{'\n'})
|
||||
count++
|
||||
}
|
||||
if err = check.Err(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if count != result.Rows || hex.EncodeToString(h.Sum(nil)) != result.SHA256 {
|
||||
return result, importRefusef("import content fingerprint differs: %s", name)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func normalizeImportRow(table string, columns []string, values []any) error {
|
||||
for i, column := range columns {
|
||||
// SQLite may scan a non-NULL zero-length BLOB as []byte(nil).
|
||||
// pgx binds that slice as SQL NULL unless made explicitly non-nil.
|
||||
if b, ok := values[i].([]byte); ok && b == nil {
|
||||
values[i] = []byte{}
|
||||
}
|
||||
if !(table == "organizer_dav_resources" && (column == "name" || column == "uid")) &&
|
||||
!(table == "organizer_task_deletions" && (column == "dav_name" || column == "dav_uid")) {
|
||||
continue
|
||||
}
|
||||
value, ok := values[i].(string)
|
||||
if !ok {
|
||||
return importRefuse("invalid task DAV index scalar")
|
||||
}
|
||||
project := ""
|
||||
if strings.HasPrefix(value, "\x00") {
|
||||
parts := strings.Split(value, "\x00")
|
||||
if len(parts) != 3 || parts[1] == "" || parts[2] == "" {
|
||||
return importRefuse("invalid legacy task DAV index key")
|
||||
}
|
||||
project, value = parts[1], parts[2]
|
||||
} else if strings.ContainsRune(value, 0) {
|
||||
return importRefuse("invalid legacy task DAV index key")
|
||||
}
|
||||
values[i] = nativeTaskDAVIndexKey(project, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func resetImportIdentities(ctx context.Context, tx *sql.Tx, sourceSequences map[string]int64) error {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT table_name,column_name FROM information_schema.columns WHERE table_schema=current_schema() AND is_identity='YES'`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var ids [][2]string
|
||||
for rows.Next() {
|
||||
var t, c string
|
||||
if err = rows.Scan(&t, &c); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
ids = append(ids, [2]string{t, c})
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, id := range ids {
|
||||
if _, ok := applicationImportColumns[id[0]]; !ok {
|
||||
return importRefuse("unclassified identity")
|
||||
}
|
||||
var next int64
|
||||
if err = tx.QueryRowContext(ctx, `SELECT COALESCE(max(`+importIdent(id[1])+`),0)+1 FROM `+importIdent(id[0])).Scan(&next); err != nil {
|
||||
return err
|
||||
}
|
||||
if next < 1 {
|
||||
return importRefuse("identity overflow")
|
||||
}
|
||||
if previous := sourceSequences[id[0]]; previous >= next {
|
||||
if previous == int64(^uint64(0)>>1) {
|
||||
return importRefuse("identity exhausted")
|
||||
}
|
||||
next = previous + 1
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `ALTER TABLE `+importIdent(id[0])+` ALTER COLUMN `+importIdent(id[1])+` RESTART WITH `+fmt.Sprint(next)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func sourceImportSequences(ctx context.Context, tx *sql.Tx) (map[string]int64, error) {
|
||||
out := map[string]int64{}
|
||||
var exists bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name='sqlite_sequence')`).Scan(&exists); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !exists {
|
||||
return out, nil
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT name,seq FROM sqlite_sequence`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var name string
|
||||
var seq int64
|
||||
if err = rows.Scan(&name, &seq); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, ok := applicationImportColumns[name]; ok {
|
||||
out[name] = seq
|
||||
} else if _, ok := applicationImportOmissions[name]; !ok {
|
||||
return nil, importRefusef("unclassified sequence: %s", name)
|
||||
}
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func validateImportedBindings(ctx context.Context, tx *sql.Tx) error {
|
||||
var bad bool
|
||||
err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domains d LEFT JOIN campuses c ON c.uuid=d.campus_uuid WHERE c.uuid IS NULL) OR EXISTS(SELECT 1 FROM paths p LEFT JOIN domains d ON d.uuid=p.domain_uuid WHERE d.uuid IS NULL) OR EXISTS(SELECT 1 FROM domain_memberships m LEFT JOIN domains d ON d.uuid=m.domain_uuid LEFT JOIN principals p ON p.uuid=m.principal_uuid WHERE d.uuid IS NULL OR p.uuid IS NULL)`).Scan(&bad)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if bad {
|
||||
return importRefuse("imported namespace or membership integrity failed")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// PreparePostgreSQLImport creates only schema in a dedicated empty database.
|
||||
// It must not be used to adopt a populated or unversioned PostgreSQL schema.
|
||||
func PreparePostgreSQLImport(ctx context.Context, target *sql.DB, root string) error {
|
||||
if err := refusePopulatedImportTarget(ctx, target); err != nil {
|
||||
return err
|
||||
}
|
||||
s := &Store{DB: target, sqlBackend: storePostgreSQL, DocumentRoot: root}
|
||||
if err := s.ensurePostgreSQLNamespaces(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
core, err := metadata.New(target, metadata.PostgreSQL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = core.InitializeFresh(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = s.prepareNativeApplicationSchemas(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = calendar.NewPostgreSQL(ctx, filepath.Join(root, "calendars"), target)
|
||||
return err
|
||||
}
|
||||
|
||||
// Preparation must not run migrations against an existing application. Failed
|
||||
// attempts may leave versioned schema and exact default seeds, but no user rows.
|
||||
func refusePopulatedImportTarget(ctx context.Context, target *sql.DB) error {
|
||||
rows, err := target.QueryContext(ctx, `SELECT table_name FROM information_schema.tables WHERE table_schema=current_schema() AND table_type='BASE TABLE'`)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var names []string
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err = rows.Scan(&name); err != nil {
|
||||
rows.Close()
|
||||
return err
|
||||
}
|
||||
names = append(names, name)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, name := range names {
|
||||
if name == "schema_migration_steps" {
|
||||
continue
|
||||
}
|
||||
if _, ok := applicationImportColumns[name]; !ok && name != "application_import_receipts" {
|
||||
return importRefuse("import destination contains an unmanaged table")
|
||||
}
|
||||
var n int64
|
||||
if err = target.QueryRowContext(ctx, `SELECT count(*) FROM `+importIdent(name)).Scan(&n); err != nil {
|
||||
return err
|
||||
}
|
||||
if n == 0 {
|
||||
continue
|
||||
}
|
||||
predicate, ok := importSeedPredicates[name]
|
||||
if !ok {
|
||||
return importRefusef("import destination is not empty: %s", name)
|
||||
}
|
||||
var valid bool
|
||||
if err = target.QueryRowContext(ctx, `SELECT count(*)=1 AND bool_and(`+predicate+`) FROM `+importIdent(name)).Scan(&valid); err != nil {
|
||||
return err
|
||||
}
|
||||
if !valid {
|
||||
return importRefusef("import destination seed has changed: %s", name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// JSON alone cannot distinguish a byte slice from its base64 text, nor an
|
||||
// integer from an integral float. Explicit tags make conversion detectable.
|
||||
func importRowBytes(values []any) ([]byte, error) {
|
||||
type cell struct {
|
||||
Type string
|
||||
Value any
|
||||
}
|
||||
row := make([]cell, len(values))
|
||||
for i, value := range values {
|
||||
var kind string
|
||||
switch value.(type) {
|
||||
case nil:
|
||||
kind = "null"
|
||||
case string:
|
||||
kind = "text"
|
||||
case []byte:
|
||||
kind = "bytes"
|
||||
if value.([]byte) == nil {
|
||||
value = []byte{}
|
||||
}
|
||||
case int64:
|
||||
kind = "integer"
|
||||
case float64:
|
||||
kind = "float"
|
||||
case bool:
|
||||
kind = "boolean"
|
||||
default:
|
||||
return nil, importRefuse("unsupported import scalar type")
|
||||
}
|
||||
row[i] = cell{kind, value}
|
||||
}
|
||||
return json.Marshal(row)
|
||||
}
|
||||
|
||||
// InspectSQLiteApplicationImport never writes or hydrates user content.
|
||||
func InspectSQLiteApplicationImport(ctx context.Context, source *sql.DB) ([]string, error) {
|
||||
tx, err := source.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
tables, err := inspectApplicationImportSource(ctx, tx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
names := make([]string, 0, len(tables))
|
||||
for name := range tables {
|
||||
names = append(names, name)
|
||||
}
|
||||
sort.Strings(names)
|
||||
return names, nil
|
||||
}
|
||||
|
||||
// VerifyPostgreSQLImport gates the production selector. A successful core-only
|
||||
// rehearsal is insufficient. The original key and the same canonical root are
|
||||
// required; relocation/restore must preserve the configured mount path.
|
||||
func VerifyPostgreSQLImport(ctx context.Context, target *sql.DB, root string) error {
|
||||
key, err := existingAuthKey(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
root, err = filepath.Abs(root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var path, keyHash, fingerprint string
|
||||
var completed int64
|
||||
if err = target.QueryRowContext(ctx, `SELECT document_root,key_fingerprint,source_fingerprint,completed_at_ms FROM application_import_receipts WHERE id=1`).Scan(&path, &keyHash, &fingerprint, &completed); err != nil {
|
||||
return importRefuse("PostgreSQL startup requires a completed full application import")
|
||||
}
|
||||
sum := sha256.Sum256(key)
|
||||
if root != path || keyHash != hex.EncodeToString(sum[:]) || len(fingerprint) != 64 || completed <= 0 {
|
||||
return importRefuse("PostgreSQL import root/key receipt mismatch")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package store
|
||||
|
||||
// Frozen source column inventory for the current SQLite application schema.
|
||||
// Unknown tables/columns block copying. Changes require an explicit review.
|
||||
var applicationImportColumns = map[string]string{
|
||||
"activity_outbox": "actor_uuid,attempts,claimed_at_ms,created_at_ms,domain_uuid,event_type,idempotency_key,last_error,next_attempt_ms,payload,sequence,status,target_kind,target_uuid,uuid",
|
||||
"activity_outbox_checkpoints": "consumer_id,domain_uuid,last_sequence,updated_at_ms",
|
||||
"activity_outbox_heads": "domain_uuid,event_type,last_sequence",
|
||||
"activity_outbox_migrations": "name",
|
||||
"adapter_sessions": "base_revision_uuid,created_at_ms,domain_uuid,exchanged_at_ms,object_uuid,plugin_id,principal_uuid,root_path,session_expires_at_ms,session_hash,ticket_expires_at_ms,ticket_hash",
|
||||
"asset_head_history": "action,actor_uuid,address,content_type,created_at_ms,document_hash,domain_uuid,filename,id,size_bytes",
|
||||
"assets": "address,content_type,created_at_ms,domain_uuid,filename,head_document_hash,object_uuid,size_bytes,updated_at_ms",
|
||||
"auth_audit_log": "created_at_ms,detail_json,domain_uuid,event,id,outcome,principal_uuid,request_id,target_id,target_type",
|
||||
"auth_capability_grants": "capability,created_at_ms,role_uuid,target_id,target_type,uuid",
|
||||
"auth_challenges": "attempt_count,consumed_at_ms,created_at_ms,domain_uuid,expires_at_ms,kind,payload_json,principal_uuid,token_hash,uuid",
|
||||
"auth_cli_requests": "attempts,browser_hash,challenge,code_hash,created_ms,device_hash,domain_uuid,expires_ms,id,mfa_level,next_poll_ms,oidc_attempts,origin,principal_uuid,remember,state",
|
||||
"auth_credentials": "created_at_ms,enabled,identifier,kind,parameters_json,principal_uuid,secret_hash,updated_at_ms,uuid",
|
||||
"auth_group_mappings": "domain_uuid,enabled,external_group,group_uuid,mode,provider_type,provider_uuid,uuid",
|
||||
"auth_group_role_bindings": "created_at_ms,group_uuid,role_uuid",
|
||||
"auth_providers": "config_json,created_at_ms,domain_uuid,enabled,kind,secret_encrypted,updated_at_ms,uuid",
|
||||
"auth_role_bindings": "created_at_ms,membership_uuid,role_uuid",
|
||||
"auth_roles": "created_at_ms,description,domain_uuid,name,uuid",
|
||||
"auth_sessions": "auth_time_ms,domain_uuid,expires_at_ms,last_seen_at_ms,mfa_level,principal_uuid,revoked_at_ms,token_hash,uuid",
|
||||
"broadcasts": "body,created_at_ms,domain_uuid,ends_at_ms,severity,starts_at_ms,title,updated_at_ms,uuid",
|
||||
"browser_navigation_links": "created_at_ms,domain_uuid,is_directory,label,path,position,principal_uuid,updated_at_ms,uuid",
|
||||
"calendar_event_cache": "calendar,data,domain,end,etag,month,start,uid",
|
||||
"calendar_index_state": "calendar,domain,version",
|
||||
"calendar_month_cache": "calendar,domain,hash,month",
|
||||
"campus_admin_grants": "created_at_ms,group_uuid,member_role,membership_uuid,source",
|
||||
"campus_admin_membership_origins": "domain_uuid,membership_uuid,principal_uuid",
|
||||
"campus_admin_migrations": "key",
|
||||
"campus_tokens": "campus_uuid,created_at,id,label,last_used_at,revoked,token_hash",
|
||||
"campuses": "authority_mode,controller_transport,created_at,default_lang,enabled,fqdn,id,last_contact_at,last_contact_error,name,title,uuid",
|
||||
"city_governance_epochs": "domain_uuid,version",
|
||||
"city_governance_events": "actor_uuid,after_json,before_json,created_at_ms,domain_uuid,epoch,id,node_kind,node_uuid",
|
||||
"city_owner_bindings": "actor_uuid,created_at_ms,domain_uuid,membership_uuid,node_kind,node_uuid",
|
||||
"city_page_overrides": "domain_uuid,page_uuid,spot_uuid",
|
||||
"config": "domain_uuid,key,updated_at,value",
|
||||
"document_backfills": "completed,last_uuid,processed,store_id,updated_at_ms",
|
||||
"domain_entry_settings": "domain_uuid,settings_json",
|
||||
"domain_memberships": "created_at_ms,domain_uuid,principal_uuid,state,uuid",
|
||||
"domain_topology_versions": "domain_uuid,updated_at_ms,version",
|
||||
"domains": "campus_uuid,created_at,domain,enabled,id,instance_fingerprint,instance_fqdn,name,placement,title,uuid",
|
||||
"event_definitions": "created_at_ms,enabled,event_key,event_type,interval_seconds,max_attempts,name,next_run_at_ms,payload,priority,schedule_kind,updated_at_ms,uuid",
|
||||
"event_jobs": "attempts,cancel_requested,created_at_ms,error,event_key,event_type,event_uuid,finished_at_ms,max_attempts,payload,priority,run_at_ms,started_at_ms,status,updated_at_ms,uuid",
|
||||
"event_log_buckets": "bucket_start_ms,count,event_type,first_at_ms,last_at_ms,level,sample_message",
|
||||
"event_log_policies": "aggregate_after_hours,bucket_seconds,event_type,max_rows,retention_hours",
|
||||
"event_logs": "created_at_ms,event_uuid,id,job_uuid,level,message",
|
||||
"event_queue_settings": "concurrency,id,max_depth,paused,retention_hours,retry_base_seconds",
|
||||
"federation_instance_capabilities": "instance_uuid,manifest_version",
|
||||
"federation_instances": "campus_uuid,created_at,fingerprint,fqdn,id,last_seen_at,status,uuid",
|
||||
"federation_manifest_state": "applied_at,applied_revision,campus_uuid,controller_fingerprint,controller_public_key,manifest_json",
|
||||
"federation_manifest_versions": "campus_uuid,manifest_hash,revision,updated_at",
|
||||
"feed_actions": "claim,created_ms,desired,domain_uuid,error,field,item_id,owner_uuid,request_id,source_uuid,source_version,status,until_ms",
|
||||
"feed_import_receipts": "domain_uuid,input_hash,owner_uuid,request_id,result",
|
||||
"feed_items": "body,body_html,feed,folder,id,is_read,published_ms,remote_categories,remote_feed,source_uuid,starred,title,url",
|
||||
"feed_note_receipts": "domain_uuid,input_hash,owner_uuid,page_uuid,request_id",
|
||||
"feed_sources": "credential,domain_uuid,enabled,error,etag,folder,interval_seconds,last_ms,lease,lease_until,modified,next_ms,owner_uuid,provider,remote_catalog,remote_folders,title,url,uuid,version,write_enabled",
|
||||
"feed_states": "id,is_read,source_uuid,starred,version",
|
||||
"group_members": "created_at_ms,group_uuid,member_role,membership_uuid,source",
|
||||
"group_permissions": "created_at_ms,group_uuid,permission,scope_path,uuid",
|
||||
"groups": "builtin_key,created_at_ms,description,display_name,domain_uuid,inherit_members,name,state,updated_at_ms,uuid,visibility",
|
||||
"import_city_edges": "created_at_ms,page_uuid,session_uuid,spot_uuid",
|
||||
"import_city_journal": "created_at_ms,kind,node_uuid,session_uuid",
|
||||
"import_items": "confidence,content_type,selected,sequence,session_uuid,size,source_path,suggested_quartier,suggested_spot,suggested_street,target_path,title",
|
||||
"import_journal": "completed,created_at_ms,object_uuid,path,sequence,session_uuid",
|
||||
"import_sessions": "actor_uuid,archive_name,archive_path,created_at_ms,domain_uuid,error,markdown_files,status,target_prefix,total_files,updated_at_ms,uuid",
|
||||
"locality_access_entries": "domain_uuid,node_kind,node_uuid,permission,subject_kind,subject_name,subject_uuid",
|
||||
"locality_access_policies": "actor_uuid,authority_rank,domain_uuid,grants_access,node_kind,node_uuid,scope_path",
|
||||
"locality_auto_publication": "actor_uuid,domain_uuid,mode,node_kind,node_uuid,scope_path,version",
|
||||
"locality_publication_entries": "document_hash,domain_uuid,lang,node_kind,node_uuid,object_uuid,path,revision_uuid",
|
||||
"locality_publication_manifests": "actor_uuid,created_at_ms,domain_uuid,epoch,node_kind,node_uuid,topology_version",
|
||||
"locality_publication_policies": "actor_uuid,domain_uuid,node_kind,node_uuid,policy,updated_at_ms",
|
||||
"locality_publication_versions": "domain_uuid,node_kind,node_uuid,version",
|
||||
"navigation_generation": "id,version",
|
||||
"notification_inbox": "created_at_ms,domain_uuid,event_uuid,href,kind,message,read_at_ms,recipient_uuid,request_uuid,status,uuid",
|
||||
"notification_push_keys": "id,private_key,public_key",
|
||||
"notification_push_queue": "attempts,created_at_ms,lease_until_ms,next_at_ms,notification_uuid,subscription_id",
|
||||
"notification_push_subscriptions": "domain_uuid,expires_at_ms,foreground_until_ms,id,owner_uuid,session_hash,subscription",
|
||||
"object_origins": "created_at,domain_uuid,object_uuid",
|
||||
"objects": "created_at,kind,uuid",
|
||||
"organizer_activity_bridge": "id,last_sequence",
|
||||
"organizer_capture_receipts": "domain_uuid,item_uuid,owner_uuid,request_hash,request_uuid",
|
||||
"organizer_changes": "actor_uuid,created_at_ms,domain_uuid,item_uuid,sequence,version",
|
||||
"organizer_contact_changes": "domain_uuid,owner_uuid,sequence,uuid",
|
||||
"organizer_contact_credentials": "credential_id",
|
||||
"organizer_contact_epoch": "id,value",
|
||||
"organizer_contact_names": "domain_uuid,name,owner_uuid,uuid",
|
||||
"organizer_contact_revisions": "created_ms,deleted,domain_uuid,hash,owner_uuid,uuid,version",
|
||||
"organizer_contacts": "data,deleted,domain_uuid,hash,name,owner_uuid,search,uid,uuid,version",
|
||||
"organizer_dashboard_layouts": "data,domain_uuid,owner_uuid,version",
|
||||
"organizer_dav_collections": "domain_uuid,owner_uuid,path,project_uuid",
|
||||
"organizer_dav_compat": "credential_id",
|
||||
"organizer_dav_credentials": "created_ms,domain_uuid,expires_ms,id,label,owner_uuid,secret_hash,write_access",
|
||||
"organizer_dav_default_lists": "domain_uuid,owner_uuid,title,updated_at_ms",
|
||||
"organizer_dav_legacy_hrefs": "domain_uuid,item_uuid,name,owner_uuid",
|
||||
"organizer_dav_resources": "domain_uuid,item_uuid,name,owner_uuid,uid",
|
||||
"organizer_dav_scopes": "credential_id,scope",
|
||||
"organizer_digest_attempts": "created_ms,day,domain_uuid,kind,owner_uuid,preference_version,reason,state,timezone,updated_ms,uuid",
|
||||
"organizer_digest_deliveries": "attempt_uuid,day,domain_uuid,owner_uuid,state,updated_ms",
|
||||
"organizer_digest_requests": "created_ms,day,domain_uuid,duplicate_ack,expected_attempt,owner_uuid,uuid",
|
||||
"organizer_focus": "created_at_ms,deadline_ms,domain_uuid,duration_ms,owner_uuid,phase,remaining_ms,state,task_uuid,updated_at_ms,uuid,version",
|
||||
"organizer_focus_changes": "action,created_at_ms,data,session_uuid,version",
|
||||
"organizer_focus_spans": "closed,domain_uuid,end_ms,owner_uuid,phase,session_uuid,start_ms,task_uuid,version",
|
||||
"organizer_items": "archived,bucket,created_at_ms,data,document_hash,domain_uuid,due_on,end_on,kind,owner_uuid,parent_uuid,planned_on,project_uuid,review_on,start_on,status,title,updated_at_ms,uuid,version",
|
||||
"organizer_reminders": "civil_date,domain_uuid,fire_at_ms,item_uuid,kind,notification_key,owner_uuid,source_version,state,timezone",
|
||||
"organizer_revisions": "actor_uuid,created_at_ms,document_hash,domain_uuid,item_uuid,version",
|
||||
"organizer_task_deletions": "dav_name,dav_uid,deleted_at_ms,domain_uuid,item_uuid,owner_uuid,version",
|
||||
"organizer_task_metrics": "completed_ms,complexity,domain_uuid,estimate,owner_uuid,task_uuid",
|
||||
"organizer_task_preferences": "data,domain_uuid,next_digest_ms,owner_uuid,version",
|
||||
"organizer_time_blocks": "calendar_uuid,created_at_ms,domain_uuid,end,event_uid,owner_uuid,start,state,task_uuid,title,uuid",
|
||||
"organizer_work_revisions": "action,changed_ms,data,domain_uuid,owner_uuid,reason,version,work_uuid",
|
||||
"organizer_work_sessions": "cancelled_ms,domain_uuid,end_ms,owner_uuid,start_ms,task_uuid,uuid,version",
|
||||
"paths": "created_at,domain_uuid,is_primary,object_uuid,path",
|
||||
"periodical_entries": "category,domain_uuid,kind,local_date,owner_uuid,page_uuid,timezone,title,version",
|
||||
"periodical_events": "actor_uuid,created_at_ms,domain_uuid,id,kind,operation,page_uuid,version",
|
||||
"periodical_posts": "actor_uuid,digest,domain_uuid,kind,page_uuid,request_id",
|
||||
"periodical_prefixes": "domain_uuid,kind,prefix",
|
||||
"periodical_projections": "domain_uuid,kind,prefix,source_uuid",
|
||||
"periodical_settings": "domain_uuid,enabled,kind",
|
||||
"personal_appearance": "data,domain_uuid,owner_uuid,version",
|
||||
"principal_identities": "attributes_json,created_at_ms,issuer,principal_uuid,provider_type,provider_uuid,subject,uuid",
|
||||
"principals": "avatar,avatar_content_type,created_at_ms,display_name,email,state,updated_at_ms,uuid",
|
||||
"profile_contacts": "domain_uuid,phone_display,phone_number,principal_uuid,updated_at_ms,version,visibility",
|
||||
"projection_metadata": "document_hash,error,kind,payload,retry_at_ms,updated_at_ms,version",
|
||||
"publication_decisions": "created_at_ms,decision,generation,justification,request_uuid,reviewer_uuid,uuid",
|
||||
"publication_requests": "created_at_ms,domain_uuid,expires_at_ms,generation,idempotency_key,policy_version,requester_uuid,scope_kind,scope_uuid,state,subject_hash,subject_json,updated_at_ms,uuid",
|
||||
"resource_samples": "id,job_uuid,payload,timestamp_ms",
|
||||
"revision_document_refs": "document_hash,revision_uuid",
|
||||
"revisions": "body,content_type,created_at,created_at_ms,document_hash,filename,lang,message,object_uuid,uuid",
|
||||
"search_schema_state": "name",
|
||||
"spot_pages": "created_at_ms,edge_type,featured,label,page_uuid,sort_key,source,spot_uuid",
|
||||
"spot_relations": "created_at_ms,relation_type,source_spot_uuid,target_spot_uuid,weight",
|
||||
"spots": "charter_json,created_at_ms,domain_uuid,landing_json,name,path_prefix,schema_json,state,summary,title,updated_at_ms,uuid,version",
|
||||
"street_inbox_pages": "created_at_ms,page_uuid,street_uuid",
|
||||
"street_spots": "created_at_ms,section,sort_key,spot_uuid,street_uuid",
|
||||
"tags": "created_at,object_uuid,tag",
|
||||
"topology_events": "actor_principal_uuid,after_json,before_json,created_at_ms,domain_uuid,id,operation,request_id,target_type,target_uuid,topology_version",
|
||||
"user_space_path_policies": "actor_uuid,domain_uuid,path,principal_uuid,updated_at_ms,visibility",
|
||||
"user_space_policies": "actor_uuid,avatar_visibility,descendant_visibility,domain_uuid,principal_uuid,profile_visibility,root_visibility,updated_at_ms,version",
|
||||
"user_space_policy_events": "actor_uuid,after_json,before_json,created_at_ms,domain_uuid,id,principal_uuid",
|
||||
"zombie_gc_maintenance": "grace_seconds,singleton",
|
||||
"zones": "charter_json,created_at_ms,description,domain_uuid,kind,name,parent_uuid,path_prefix,policy_json,sort_key,state,title,updated_at_ms,uuid,version",
|
||||
}
|
||||
|
||||
// Only backend migration ledgers and rebuildable SQLite FTS projections are omitted.
|
||||
var applicationImportOmissions = map[string]string{
|
||||
"latest_revision_index": "content_type,created_at_ms,document_hash,filename,object_uuid,revision_uuid",
|
||||
"revision_search": "body,filename,object_uuid,revision_uuid",
|
||||
"revision_search_config": "k,v",
|
||||
"revision_search_content": "c0,c1,c2,c3,id",
|
||||
"revision_search_data": "block,id",
|
||||
"revision_search_docsize": "id,sz",
|
||||
"revision_search_idx": "pgno,segid,term",
|
||||
"revision_search_state": "revision_uuid",
|
||||
"schema_migration_steps": "applied_at_ms,checksum,component,name,version",
|
||||
"schema_migrations": "applied_at,name",
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package store
|
||||
|
||||
// Exact app-owned SQLite trigger/view definitions. Custom changes require review.
|
||||
var applicationImportSourceObjects = map[string]string{
|
||||
"activity_outbox_advance_head": "trigger:3058a7506ff2d387fc7eb0a9440f75595c97bb1ce652b35f8b6a9dd382c91a66",
|
||||
"feed_actions_source_change": "trigger:84ccd4016b85c465aa79d0757bf1219d36719cf4fea7aceb3745b214aa1e982d",
|
||||
"feed_actions_source_delete": "trigger:72e0375d92e19683ef72e1af961eb0682f2354c82efb6be5511780852634d91f",
|
||||
"feed_source_delete": "trigger:624615a48b0116e16efe8f59a4e087589f17327a420e3fbca6d90f156b0d2d35",
|
||||
"navigation_auth_capability_grants_DELETE": "trigger:9e3a4389d57054cb80c84bfb474a1cd8bbc6f51ea9ee34660692c36ca8e69a25",
|
||||
"navigation_auth_capability_grants_INSERT": "trigger:a069846cfaed283798e2aaad60e23dedb21a7bc2593e18d200619c4f8018a185",
|
||||
"navigation_auth_capability_grants_UPDATE": "trigger:bb864ba4dfa1bb7f3b1733921d375fcba95493c2e4e0e95b7d32c60072361275",
|
||||
"navigation_auth_role_bindings_DELETE": "trigger:0c7890e438562e3fadfccf864e5d63a3a4963d9a5f9d05010f673dc9ff17e777",
|
||||
"navigation_auth_role_bindings_INSERT": "trigger:a55f66a37f04085d766ddb89ef5c3ae91692e2c937c38d81f870a58d913d727b",
|
||||
"navigation_auth_role_bindings_UPDATE": "trigger:100095820afdc2ea2cb31825a2484a93f292c3fa86eae1762dd3e21db5168073",
|
||||
"navigation_auth_roles_DELETE": "trigger:a879c0bb47cd2280e965373b648af00d80dd4da47e8da6286daadd596d86fcbd",
|
||||
"navigation_auth_roles_INSERT": "trigger:d102dd62d08af05e7fcf56ebbfe07812bbd74c1fc3731bd5513c2f924193b3bd",
|
||||
"navigation_auth_roles_UPDATE": "trigger:a0984b20376c376fac9a5785da863dc34ca49498138c166261c782843540d071",
|
||||
"navigation_campuses_DELETE": "trigger:8fb85a6937f692dd3c03875af27dac60da7cce09f3a6501b0c6d185ad40726c7",
|
||||
"navigation_campuses_INSERT": "trigger:0657d8a8294e4d782b9e1287112a2f391805d3ba93d69539b9d634a6483cdace",
|
||||
"navigation_campuses_UPDATE": "trigger:88fc863aae99ab5e78916a194bfd95c71a27191e1bfcf4ec93cd2d843551b6a4",
|
||||
"navigation_city_governance_epochs_DELETE": "trigger:e0500d0b84809cf695fa41ee2781ebc285e8a9d273d3235fff4f75ea9a34288e",
|
||||
"navigation_city_governance_epochs_INSERT": "trigger:fcab809d5a9038dcf64c1880ce937c3ab1a7fa92367fb97bec86f4c154c2ac2c",
|
||||
"navigation_city_governance_epochs_UPDATE": "trigger:19449b43b8db827d1b3118ccbebb9b56e4c0017bd2b49e98725196202edbbd2b",
|
||||
"navigation_city_owner_bindings_DELETE": "trigger:1d6776e9c3b8d289604d05b1ed3adebb0322cfd7299266ddc74d26cea0f0f0f9",
|
||||
"navigation_city_owner_bindings_INSERT": "trigger:66fd2522d2a89cb1a6c58f412c28952f2e7ec357c493074a3806ad45bf028b26",
|
||||
"navigation_city_owner_bindings_UPDATE": "trigger:0755ad5e7a325edda7e050f392a8628ec68a31b34fa89aab44293e35c6e6f4e8",
|
||||
"navigation_city_page_overrides_DELETE": "trigger:5464f4bf8b9839c447deec70b71a8ee345033a46ab55d71a2852ac2adcebc38a",
|
||||
"navigation_city_page_overrides_INSERT": "trigger:c154d7be988ca67b8eefd4715b2972d53e731b516a847db302db5edb6d03676b",
|
||||
"navigation_city_page_overrides_UPDATE": "trigger:a62ea9270f83204ede125e48b5be7a9e0bae3be4097a793c7515a0123d67df08",
|
||||
"navigation_domain_memberships_DELETE": "trigger:ad0f4d3f1f6f90cc740ee55d1038a9770d6795725a9a5cf6ba5078eb2dea4d1c",
|
||||
"navigation_domain_memberships_INSERT": "trigger:2b25f34b22c69dddfa972561c62ece61777555e8fa1a9c83fb5eb561d4680aed",
|
||||
"navigation_domain_memberships_UPDATE": "trigger:1cf891af8e22c12c7ad67d1bfe5694319a97923c13b2c1603128216d4954e8f2",
|
||||
"navigation_domain_topology_versions_DELETE": "trigger:c8a39f592225412784d6ea851425c9e172d48bea7213e49d034a42a18836c7aa",
|
||||
"navigation_domain_topology_versions_INSERT": "trigger:e13c715defc21167dfa60762c925c8b9ab7abde161d821d2a292be63a3356946",
|
||||
"navigation_domain_topology_versions_UPDATE": "trigger:2142ff3636a1e61e590d9740cb3b61c247f955e451354d710225ac6ebbf3ebfc",
|
||||
"navigation_domains_DELETE": "trigger:8ee87712265ae840a4d94874ebaf3ab79deb69706a9fcb17afe3aae5b3cab699",
|
||||
"navigation_domains_INSERT": "trigger:fb3cefdcea591416a6d46a7c1bfed99d51e3aa383792a717a69203871f8e25b7",
|
||||
"navigation_domains_UPDATE": "trigger:f5104dff7f370a7f531886af9f4a884b2ddc30509689c26975749b7c6bc46716",
|
||||
"navigation_group_members_DELETE": "trigger:5e351d5e06f46f545f5e19f12a997156038a446af54d495cbff397f505cdaf3c",
|
||||
"navigation_group_members_INSERT": "trigger:8b25c1b04980291cce53c85fd3c8c4a0c2b13499ce15af12699a58cc81bf3769",
|
||||
"navigation_group_members_UPDATE": "trigger:348b2cce776333d54d54d24c941c408742f80285593e8cf5132502941c50bfdb",
|
||||
"navigation_group_permissions_DELETE": "trigger:32aab8c788c491a6aabe288c6eb27df323f5bcfa92e33bfd5e94fa3d0a799624",
|
||||
"navigation_group_permissions_INSERT": "trigger:7f2638908b391bf9dabfabd6ef0d9218510995df21b9a610ffc8fbf76b09f760",
|
||||
"navigation_group_permissions_UPDATE": "trigger:4669026703d8aade79bbee5c9f56ca8123c0fa1c0cc393ea0d731f9b3548d636",
|
||||
"navigation_groups_DELETE": "trigger:29af285057e26f2a1b1db7dc76fcb87a9a407ead289fbf908ef69c44d5d1e947",
|
||||
"navigation_groups_INSERT": "trigger:ecbc10a3d8d1b099ec671af2bda61dc8d5a0ec06e00d18d2855c35fedf6e5542",
|
||||
"navigation_groups_UPDATE": "trigger:ec274be8dfadb4297d9cbbbeff7cf9ff984fe2fb335b479f5d746aea902c6dfd",
|
||||
"navigation_locality_access_entries_DELETE": "trigger:8f261aa7a88b75d3861d5fdb486ed973dd08dd882b62e3af45904e569b7767d9",
|
||||
"navigation_locality_access_entries_INSERT": "trigger:8d963f3cd940d51c4614e168917cbf165c9a8f519865db2d5d272f12607af25a",
|
||||
"navigation_locality_access_entries_UPDATE": "trigger:7780ac847469992cd6d222b3b5faeaa24031ae152252ea45a4251f5ac8104eb1",
|
||||
"navigation_locality_access_policies_DELETE": "trigger:79ffcae086bd29bce3569f5d4bae2eccf0e7c436efcf90e38a02ea28a0b53a42",
|
||||
"navigation_locality_access_policies_INSERT": "trigger:e6eb2ce999b658dfff3788c7657884bc1f6b2545cdbf1bb164ddc0072b39d046",
|
||||
"navigation_locality_access_policies_UPDATE": "trigger:55edd795c4c54bb1596806a012f162e82c1526580a48892a54a0a53b19733335",
|
||||
"navigation_locality_auto_publication_DELETE": "trigger:25ef213c6a2f72fdcd92ff78caf1c6ba1a7c6d449754af55fdf975e95f005af0",
|
||||
"navigation_locality_auto_publication_INSERT": "trigger:b27b6312dac392c9ed98fe5916702ae1e03c2ef985c498d5a96107e6431cc2e2",
|
||||
"navigation_locality_auto_publication_UPDATE": "trigger:6f8fcf1e33e4ee1eb75b3e1ed694076a76d34f8b3c3c1517e8e0246c0c2443fa",
|
||||
"navigation_locality_publication_entries_DELETE": "trigger:340aa39b018f65eeb8caf844375b90759cc7cae12ed0ae5f1943020910cb9c5f",
|
||||
"navigation_locality_publication_entries_INSERT": "trigger:6084380e04cf1f98600a1d86893f2e3474153541590d5106d8943c1632b134c2",
|
||||
"navigation_locality_publication_entries_UPDATE": "trigger:3b332ffd9d4905fcd1aede89547decb06427886e9f954527067fd4cf4f2662d3",
|
||||
"navigation_locality_publication_manifests_DELETE": "trigger:a2e0e39cfd27c419f3d597c55218ebab6ab51849570879eb45ac842d62222c38",
|
||||
"navigation_locality_publication_manifests_INSERT": "trigger:fc7191e6f5c1c63116cabd1095767d22078e3e3bf61ac9cf32913db5a89889e1",
|
||||
"navigation_locality_publication_manifests_UPDATE": "trigger:fbcea4d67a696062e6f9e1aea99d07a5ab6d6980e39adff715e67fd53fbd0630",
|
||||
"navigation_locality_publication_policies_DELETE": "trigger:1243967d9cc72083c04790d2d7f7e348889fe0375279fc52286c2efc449b1745",
|
||||
"navigation_locality_publication_policies_INSERT": "trigger:4d00a5b374aecc48075396e4a51ea51c1ddb99ee54cbbe362847b883dfb96856",
|
||||
"navigation_locality_publication_policies_UPDATE": "trigger:ed03cf19aa341f95d2f43772554ea7a3e9df8ce48ed57468dcdb75fecba3070f",
|
||||
"navigation_paths_DELETE": "trigger:4c359ac88380127cbdfeb004b0ec5336927ac3c7063827181985c8d84b4800ad",
|
||||
"navigation_paths_INSERT": "trigger:80c5bb14e8f7d68c48270f3685461a43f1b42bbaa6b26c17cc4c0640cec17f97",
|
||||
"navigation_paths_UPDATE": "trigger:70a812a7cd9a9e64fd4b51ff7339d65cec7e115de7840b7eb1bb2c6f548f42ee",
|
||||
"navigation_principals_DELETE": "trigger:6658328e4049be3a7d1dbc223d19b369bd5884007ebcf192886f49025fd2766a",
|
||||
"navigation_principals_INSERT": "trigger:1d1fe9ff6cc466b2238787c1436e427dc15cf807a1485e62fb8ec8a045668662",
|
||||
"navigation_principals_UPDATE": "trigger:72faf544e6a984508fb0f7e2fb200d2c73bd5c55874d2bbedfc05314f18c5f75",
|
||||
"navigation_revisions_DELETE": "trigger:03a821a70b405afcdd62ef21a84545d82bda28775a4f0c0e3eb2ae7f36933f66",
|
||||
"navigation_revisions_INSERT": "trigger:3ba614f28fb3794a09ccdd3c248bbdc026225ae68a027afc0be1cf0ad73c9cda",
|
||||
"navigation_revisions_UPDATE": "trigger:41f6a3d5d80aba84a9a65287e79e2323f51a2cd474021d7fe71d48168e6946e2",
|
||||
"navigation_spot_pages_DELETE": "trigger:d601a473eafcfaf43221983b6990da3a313877a6554d5ed8e69a3f33b13ae62b",
|
||||
"navigation_spot_pages_INSERT": "trigger:ec4e790c698efd8f8aa5739a21fb1c53f80fec79759e7dfcda807d374aa88507",
|
||||
"navigation_spot_pages_UPDATE": "trigger:e6e8068aa97ddb395edf779970c4c516724194048ce195ffebca104dadf344ff",
|
||||
"navigation_spot_relations_DELETE": "trigger:96871413826e8e9667f8ece6fc89c21a955099d945cea560173689369f7c7da3",
|
||||
"navigation_spot_relations_INSERT": "trigger:1ad96b83bb291c0ac2c329f2a86497858418c172c977e9c1bbb3740ceffd9b15",
|
||||
"navigation_spot_relations_UPDATE": "trigger:90f9dec821d3a671b88a9d837305f1d6157a6ea34588f1947fc73c18d37fcd63",
|
||||
"navigation_spots_DELETE": "trigger:932721d8d3e803f93635fd19b0a041cd006bb77e1a4901ed7f1c096b9510e426",
|
||||
"navigation_spots_INSERT": "trigger:3d295775e12bc78d561ec1b39e3d637a60dab853dbbdeb3deceefb9e5937f134",
|
||||
"navigation_spots_UPDATE": "trigger:3af090d07d3008930664583893af50038cc7ef37aedf4b79368d1282365f7e9a",
|
||||
"navigation_street_inbox_pages_DELETE": "trigger:bd8ee4f3ce651b71403024fb50b32e88c0cd1565b869f35d4e20a53abc31ec19",
|
||||
"navigation_street_inbox_pages_INSERT": "trigger:8d5d6ed6b8450e2ee80ee8eba52c7ba42e664bb5dcf64925424640e604be4582",
|
||||
"navigation_street_inbox_pages_UPDATE": "trigger:453d77bfa398726051d3688b6228ab1c0613003f9cab372e2262453b894aa60e",
|
||||
"navigation_street_spots_DELETE": "trigger:1ba9ade99be0bdfad31c5ca47772e4b640a6f3395a6ea469e4439bb71daec28b",
|
||||
"navigation_street_spots_INSERT": "trigger:852e6203572d50b13fee39872060acf8e64498ae51d6f757e900b66e7b9b781a",
|
||||
"navigation_street_spots_UPDATE": "trigger:dff679b3f8c4a5eba6a0641b56faa3229b8c55eb3126fac7f04b72c432bb9b3a",
|
||||
"navigation_user_space_path_policies_DELETE": "trigger:c3aa3758d9cac900298538b0aebbded9db58674319d3685b755127a38d82a5a5",
|
||||
"navigation_user_space_path_policies_INSERT": "trigger:b31187de87a56d3b2f22f1ee0b635da00aa6eac64c81aa80286bef63495da8fc",
|
||||
"navigation_user_space_path_policies_UPDATE": "trigger:3e2a7575623cbe3f8d2dfb1837b5492c7a3e61e0b8a339a6a21021d9d5820138",
|
||||
"navigation_user_space_policies_DELETE": "trigger:0163223816f3eeb396bd2322d5d6e90e2c6d222a5b4fc434676c80978c38de45",
|
||||
"navigation_user_space_policies_INSERT": "trigger:8001600fa9d8e6c487a77291251e5a3ffd2257375d38dc9262951891e6395ce7",
|
||||
"navigation_user_space_policies_UPDATE": "trigger:14578732717e3708cb07c426de84f913ac99a6a6df2451437594bc0ce9f24a8d",
|
||||
"navigation_zones_DELETE": "trigger:8078540c0d455786c925ee79158ee870b749a1aacfa2d798267e31e2e2928eab",
|
||||
"navigation_zones_INSERT": "trigger:96e9867bac8f83340c53008ebe7b6da60ee94c285efd511b3becceba2de1687b",
|
||||
"navigation_zones_UPDATE": "trigger:87ded915d38c4f4b5102b5b655dea5b3ba7b314fc02f740baf648a41e57f5857",
|
||||
"notification_push_enqueue": "trigger:898b87d9c96ba4cd0157f95a76fa9f2cd692ae0a5178c7496177ad39ece60923",
|
||||
"organizer_contact_credential_delete": "trigger:a106930d17e9af95527f7f1ae12975c4e98ff9e84478030c97ad8153a310b6de",
|
||||
"organizer_contact_insert": "trigger:227fce8ed11726fbf5dfd98be662da16de2cfba4f13ea70c775a4508cac397f5",
|
||||
"organizer_contact_update": "trigger:2ff643fe4aad770e65c021f143ff3d8940edac6bfb77322322c0a995057a0071",
|
||||
"organizer_dav_compat_delete": "trigger:644447a76a1db36d598b86f4cd4b3ae3e23655f2709428e79909d53175965762",
|
||||
"organizer_dav_scopes_delete": "trigger:1241f67ec0b744e331c7056bac44bf82fa4afc3bf6fd7d6fd5e018f4f3d1a203",
|
||||
"revision_search_delete": "trigger:c5ae0b0c31fe048c46de47ea001737e8aec3b38ea4c67cdd7e12073e20e9df58",
|
||||
"revision_search_insert": "trigger:e866fe23f1fe2732e3229c63a33bf48d28448c122fcfa03840de71354d1f5ba1",
|
||||
"user_space_path_policy_delete": "trigger:83e40e2aaf9575c2e63cb4c20da1ba1c142c88a2a10c26694ee01b8455ea16da",
|
||||
"user_space_path_policy_move": "trigger:919d2ff986131f5fc09b08115f741a429c9becf5477c4e36037bd8d9fef7ed62",
|
||||
"user_space_path_policy_rename": "trigger:a581329d9436cf12f9cf08a74e8cd3a1aa96694fd4d9cc326dc81ad78c611fe8",
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
// Legacy operational state is retained even where its native worker is gated.
|
||||
func (s *Store) ensureNativeImportRetention(ctx context.Context) error {
|
||||
return db.ApplyPostgreSQLMigrations(ctx, s.DB, "store-import-retention", []db.PostgreSQLMigration{{Version: 1, Name: "preserve tags and legacy maintenance state", Statements: []string{
|
||||
`CREATE TABLE tags(object_uuid TEXT,tag TEXT,created_at BIGINT DEFAULT (extract(epoch from now())::bigint))`,
|
||||
`CREATE INDEX tags_object_uuid ON tags(object_uuid)`,
|
||||
`CREATE TABLE document_backfills(store_id TEXT PRIMARY KEY,last_uuid TEXT NOT NULL DEFAULT '',processed BIGINT NOT NULL DEFAULT 0,completed INTEGER NOT NULL DEFAULT 0,updated_at_ms BIGINT NOT NULL)`,
|
||||
`CREATE TABLE zombie_gc_maintenance(singleton INTEGER PRIMARY KEY CHECK(singleton=1),grace_seconds BIGINT NOT NULL CHECK(grace_seconds>=86400))`,
|
||||
`CREATE TABLE activity_outbox_migrations(name TEXT PRIMARY KEY)`,
|
||||
`CREATE TABLE search_schema_state(name TEXT PRIMARY KEY)`,
|
||||
`CREATE TABLE application_import_receipts(id INTEGER PRIMARY KEY CHECK(id=1),source_fingerprint TEXT NOT NULL,document_root TEXT NOT NULL,key_fingerprint TEXT NOT NULL,report_json TEXT NOT NULL,completed_at_ms BIGINT NOT NULL)`,
|
||||
}, Assertions: []string{`SELECT object_uuid,tag,created_at FROM tags WHERE 1=0`, `SELECT store_id,last_uuid,processed,completed,updated_at_ms FROM document_backfills WHERE 1=0`, `SELECT singleton,grace_seconds FROM zombie_gc_maintenance WHERE 1=0`, `SELECT name FROM activity_outbox_migrations WHERE 1=0`, `SELECT name FROM search_schema_state WHERE 1=0`, `SELECT id,source_fingerprint,document_root,key_fingerprint,report_json,completed_at_ms FROM application_import_receipts WHERE 1=0`}}})
|
||||
}
|
||||
@@ -0,0 +1,348 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
func TestApplicationImportRoundTrip(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
source := testNamespaceStore(t, storeSQLite)
|
||||
if _, err := source.NewCalendarRepository(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domain, err := source.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owner, err := source.BootstrapOwner(domain.UUID, "owner", "synthetic import password", "日本語 owner", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = source.SetConfigInDomain(domain.UUID, "authorization_mode", "enforcing"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obj, err := source.CreatePageAtPathInDomain(domain.UUID, "/日本語/roundtrip")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rev, err := source.SaveRevisionContent(obj, []byte("<h1>Imported page</h1>"), "commit message", "en", "text/html", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = source.AddTag(obj, "日本語 tag"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := source.CreateAuthSession(domain.UUID, owner.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = source.DB.Exec(`UPDATE principals SET avatar=? WHERE uuid=?`, []byte{0, 1, 2, 255}, owner.UUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
task, err := source.SaveTaskDAV(ctx, domain.UUID, owner.UUID, TaskRecord{UUID: uuid.NewString(), Kind: "task", Title: "移行タスク", Status: "todo", Bucket: "inbox"}, 0, &TaskDAVData{Name: "import.ics", UID: "import-task", Data: "synthetic VTODO"}, "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
contact, err := source.SaveContact(ctx, domain.UUID, owner.UUID, Contact{UUID: uuid.NewString(), VCard: syntheticContact("import-contact")}, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
project, err := source.SaveTask(ctx, domain.UUID, owner.UUID, TaskRecord{UUID: uuid.NewString(), Kind: "project", Title: "移行 Project", Status: "todo", Bucket: "inbox"}, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
projectTask, err := source.SaveTaskDAV(ctx, domain.UUID, owner.UUID, TaskRecord{UUID: uuid.NewString(), Kind: "task", Title: "Project task", Status: "todo", Bucket: "inbox", ProjectUUID: project.UUID}, 0, &TaskDAVData{Name: "import.ics", UID: "import-task", Data: "synthetic VTODO"}, "", project.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deleted, err := source.SaveTask(ctx, domain.UUID, owner.UUID, TaskRecord{UUID: uuid.NewString(), Kind: "task", Title: "Deleted task", Status: "todo", Bucket: "inbox", ProjectUUID: project.UUID}, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = source.DeleteTask(ctx, domain.UUID, owner.UUID, deleted.UUID, deleted.Version); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
snapshot, err := source.ContactDAVSnapshot(ctx, domain.UUID, owner.UUID, "", 200)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Deleted highest sequence must not be reused after transfer.
|
||||
if _, err = source.DB.Exec(`UPDATE sqlite_sequence SET seq=100000 WHERE name='organizer_contact_changes'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
target := testNamespaceStore(t, storePostgreSQL)
|
||||
// Only synthetic bootstrap rows in this test's private schema are removed.
|
||||
if _, err = target.DB.Exec(`DELETE FROM domains;DELETE FROM campuses;DELETE FROM config`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = PreparePostgreSQLImport(ctx, target.DB, source.DocumentRoot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = VerifyPostgreSQLImport(ctx, target.DB, source.DocumentRoot); err == nil {
|
||||
t.Fatal("unimported target accepted")
|
||||
}
|
||||
// Force a late failure after row copying, without altering canonical files.
|
||||
if _, err = source.DB.Exec(`INSERT INTO tags(object_uuid,tag,created_at) VALUES(?,?,?)`, obj, "rollback", "invalid bigint"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = ImportSQLiteApplication(ctx, source.DB, target.DB, source.DocumentRoot); err == nil {
|
||||
t.Fatal("invalid row accepted")
|
||||
}
|
||||
var count int
|
||||
if err = target.DB.QueryRow(`SELECT count(*) FROM domains`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("partial import", count, err)
|
||||
}
|
||||
if err = target.DB.QueryRow(`SELECT count(*) FROM pg_trigger t JOIN pg_class c ON c.oid=t.tgrelid JOIN pg_namespace n ON n.oid=c.relnamespace WHERE n.nspname=current_schema() AND NOT t.tgisinternal AND t.tgenabled='D'`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("disabled triggers retained", count, err)
|
||||
}
|
||||
if _, err = source.DB.Exec(`DELETE FROM tags WHERE tag='rollback'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = PreparePostgreSQLImport(ctx, target.DB, source.DocumentRoot); err != nil {
|
||||
t.Fatal("retry preparation", err)
|
||||
}
|
||||
report, err := ImportSQLiteApplication(ctx, source.DB, target.DB, source.DocumentRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if report.Documents != 1 || len(report.Tables) < 130 || report.Fingerprint == "" {
|
||||
t.Fatal(report)
|
||||
}
|
||||
if err = VerifyPostgreSQLImport(ctx, target.DB, source.DocumentRoot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
st, err := NewPostgreSQLWithDocumentRoot(ctx, target.DB, source.DocumentRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := st.GetRevision(rev.UUID)
|
||||
if err != nil || got.Body != rev.Body {
|
||||
t.Fatal(got, err)
|
||||
}
|
||||
p, err := st.PrincipalForSession(domain.UUID, token)
|
||||
if err != nil || p.UUID != owner.UUID {
|
||||
t.Fatal(p, err)
|
||||
}
|
||||
var avatar []byte
|
||||
if err = target.DB.QueryRow(`SELECT avatar FROM principals WHERE uuid=$1`, owner.UUID).Scan(&avatar); err != nil || !bytes.Equal(avatar, []byte{0, 1, 2, 255}) {
|
||||
t.Fatal(avatar, err)
|
||||
}
|
||||
tags, err := st.ListTags(obj)
|
||||
if err != nil || len(tags) != 1 || tags[0] != "日本語 tag" {
|
||||
t.Fatal(tags, err)
|
||||
}
|
||||
if got, err := st.TaskDAVByName(ctx, domain.UUID, owner.UUID, "import.ics"); err != nil || got.UUID != task.UUID || got.Title != task.Title {
|
||||
t.Fatal(got, err)
|
||||
}
|
||||
if got, err := st.TaskDAVByName(ctx, domain.UUID, owner.UUID, "import.ics", project.UUID); err != nil || got.UUID != projectTask.UUID {
|
||||
t.Fatal("project DAV identity", got, err)
|
||||
}
|
||||
if err = st.DeleteTask(ctx, domain.UUID, owner.UUID, deleted.UUID, deleted.Version); err != nil {
|
||||
t.Fatal("delete idempotency", err)
|
||||
}
|
||||
if got, err := st.Contact(ctx, domain.UUID, owner.UUID, contact.UUID); err != nil || got.VCard != contact.VCard {
|
||||
t.Fatal(got, err)
|
||||
}
|
||||
if got, err := st.ContactDAVSnapshot(ctx, domain.UUID, owner.UUID, snapshot.Token, 200); err != nil || len(got.Items) != 0 {
|
||||
t.Fatal("sync continuity", got, err)
|
||||
}
|
||||
contact.Name = "After import"
|
||||
if _, err = st.SaveContact(ctx, domain.UUID, owner.UUID, contact, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var sequence int64
|
||||
if err = target.DB.QueryRow(`SELECT max(sequence) FROM organizer_contact_changes`).Scan(&sequence); err != nil || sequence <= 100000 {
|
||||
t.Fatal("sequence reused", sequence, err)
|
||||
}
|
||||
if err = PreparePostgreSQLImport(ctx, target.DB, source.DocumentRoot); err == nil {
|
||||
t.Fatal("populated target preparation accepted")
|
||||
}
|
||||
if os.Getenv("PAGESWIKI_TEST_POSTGRES_CONTAINER") != "" {
|
||||
restored := restoreImportTestSchema(t, target)
|
||||
if err = VerifyPostgreSQLImport(ctx, restored, source.DocumentRoot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err := NewPostgreSQLWithDocumentRoot(ctx, restored, source.DocumentRoot)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, err := r.GetRevision(rev.UUID); err != nil || got.Body != rev.Body {
|
||||
t.Fatal("restored revision", err)
|
||||
}
|
||||
if got, err := r.PrincipalForSession(domain.UUID, token); err != nil || got.UUID != owner.UUID {
|
||||
t.Fatal("restored session", err)
|
||||
}
|
||||
if got, err := r.TaskDAVByName(ctx, domain.UUID, owner.UUID, "import.ics", project.UUID); err != nil || got.UUID != projectTask.UUID {
|
||||
t.Fatal("restored task DAV", err)
|
||||
}
|
||||
if _, err = r.ContactDAVSnapshot(ctx, domain.UUID, owner.UUID, snapshot.Token, 200); err != nil {
|
||||
t.Fatal("restored contact sync", err)
|
||||
}
|
||||
t.Log("schema-scoped pg_dump/pg_restore and canonical-file reuse verified")
|
||||
}
|
||||
if _, err = ImportSQLiteApplication(ctx, source.DB, target.DB, source.DocumentRoot); err == nil {
|
||||
t.Fatal("overwrite accepted")
|
||||
}
|
||||
}
|
||||
|
||||
// Opt-in restore rehearsal: only the random schema created by this test is
|
||||
// dumped and restored, inside the explicitly selected disposable container.
|
||||
func restoreImportTestSchema(t *testing.T, source *Store) *sql.DB {
|
||||
t.Helper()
|
||||
container := os.Getenv("PAGESWIKI_TEST_POSTGRES_CONTAINER")
|
||||
u, err := url.Parse(os.Getenv("PAGESWIKI_TEST_POSTGRES_DSN"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var schema string
|
||||
if err = source.DB.QueryRow(`SELECT current_schema()`).Scan(&schema); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(schema, "pageswiki_namespace_test_") {
|
||||
t.Fatal("restore requires a private test schema")
|
||||
}
|
||||
database := strings.TrimPrefix(u.Path, "/")
|
||||
dump, err := exec.Command("docker", "exec", container, "pg_dump", "-U", u.User.Username(), "-d", database, "-n", schema, "--no-owner", "--no-acl", "-Fc").Output()
|
||||
if err != nil {
|
||||
t.Fatal("synthetic schema pg_dump failed")
|
||||
}
|
||||
old := schema + "_old"
|
||||
if _, err = source.DB.Exec(`ALTER SCHEMA ` + importIdent(schema) + ` RENAME TO ` + importIdent(old)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _, _ = source.DB.Exec(`DROP SCHEMA IF EXISTS ` + importIdent(old) + ` CASCADE`) })
|
||||
cmd := exec.Command("docker", "exec", "-i", container, "pg_restore", "-U", u.User.Username(), "-d", database, "--exit-on-error", "--no-owner", "--no-acl")
|
||||
cmd.Stdin = bytes.NewReader(dump)
|
||||
if err = cmd.Run(); err != nil {
|
||||
t.Fatal("synthetic schema pg_restore failed")
|
||||
}
|
||||
q := u.Query()
|
||||
q.Set("search_path", schema)
|
||||
u.RawQuery = q.Encode()
|
||||
restored, err := db.OpenPostgreSQL(context.Background(), db.PostgreSQLOptions{DSN: u.String(), AllowInsecure: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { restored.Close() })
|
||||
return restored
|
||||
}
|
||||
|
||||
func TestApplicationImportScalarFingerprint(t *testing.T) {
|
||||
seen := map[string]bool{}
|
||||
for _, v := range []any{nil, "", []byte{}, "YQ==", []byte("a"), int64(1), float64(1), true} {
|
||||
b, err := importRowBytes([]any{v})
|
||||
if err != nil || seen[string(b)] {
|
||||
t.Fatal("scalar collision", err)
|
||||
}
|
||||
seen[string(b)] = true
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplicationImportRejectsSourceDrift(t *testing.T) {
|
||||
for _, ddl := range []string{`CREATE TABLE unexpected(secret TEXT)`, `ALTER TABLE tags ADD COLUMN unexpected TEXT`, `CREATE TRIGGER unexpected AFTER INSERT ON tags BEGIN SELECT 1; END`} {
|
||||
t.Run(strings.Fields(ddl)[1], func(t *testing.T) {
|
||||
s := testNamespaceStore(t, storeSQLite)
|
||||
if _, err := InspectSQLiteApplicationImport(context.Background(), s.DB); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.DB.Exec(ddl); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := InspectSQLiteApplicationImport(context.Background(), s.DB); err == nil {
|
||||
t.Fatal("schema drift accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Opt-in schema inventory contains table/column metadata only, never user rows.
|
||||
func TestApplicationImportCatalogInventory(t *testing.T) {
|
||||
if os.Getenv("PAGESWIKI_IMPORT_CATALOG") != "1" {
|
||||
t.Skip("schema inventory")
|
||||
}
|
||||
ctx := context.Background()
|
||||
for _, backend := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
s := testNamespaceStore(t, backend)
|
||||
if backend == storePostgreSQL {
|
||||
root := filepath.Join(t.TempDir(), "documents")
|
||||
if err := os.Mkdir(root, 0700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("PAGESWIKI_AUTH_KEY", "synthetic catalog key")
|
||||
var err error
|
||||
s, err = NewPostgreSQLWithDocumentRoot(ctx, s.DB, root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := s.NewCalendarRepository(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
query := `SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`
|
||||
if backend == storePostgreSQL {
|
||||
query = `SELECT table_name FROM information_schema.tables WHERE table_schema=current_schema() AND table_type='BASE TABLE' ORDER BY table_name`
|
||||
}
|
||||
rows, err := s.DB.Query(query)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var tables []string
|
||||
for rows.Next() {
|
||||
var n string
|
||||
if err = rows.Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tables = append(tables, n)
|
||||
}
|
||||
rows.Close()
|
||||
if backend == storeSQLite {
|
||||
objects, err := s.DB.Query(`SELECT type,name,sql FROM sqlite_master WHERE type IN ('trigger','view') ORDER BY name`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for objects.Next() {
|
||||
var kind, name, ddl string
|
||||
if err = objects.Scan(&kind, &name, &ddl); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Logf("SOURCEOBJECT %s %s %x", kind, name, sha256.Sum256([]byte(ddl)))
|
||||
}
|
||||
objects.Close()
|
||||
}
|
||||
for _, table := range tables {
|
||||
var cols *sql.Rows
|
||||
if backend == storeSQLite {
|
||||
cols, err = s.DB.Query(`SELECT name,type FROM pragma_table_info(?) ORDER BY name`, table)
|
||||
} else {
|
||||
cols, err = s.DB.Query(`SELECT column_name,data_type FROM information_schema.columns WHERE table_schema=current_schema() AND table_name=$1 ORDER BY column_name`, table)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
columns := map[string]string{}
|
||||
for cols.Next() {
|
||||
var name, kind string
|
||||
if err = cols.Scan(&name, &kind); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
columns[name] = kind
|
||||
}
|
||||
cols.Close()
|
||||
b, _ := json.Marshal(columns)
|
||||
t.Logf("CATALOG %d %s %s", backend, table, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,124 +1,27 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/net/html"
|
||||
"pageswiki/internal/documentrefs"
|
||||
)
|
||||
|
||||
var ErrEmbeddedAssetUnavailable = errors.New("embedded asset is unavailable")
|
||||
var ErrEmbeddedAssetUnavailable = documentrefs.ErrUnavailable
|
||||
|
||||
// freezeEmbeddedAssets resolves mutable asset: HEADs only in HTML embedding
|
||||
// attributes. Plain text and code examples containing asset: remain unchanged.
|
||||
func (s *Store) freezeEmbeddedAssets(objectUUID string, payload []byte, contentType string) ([]byte, []string, error) {
|
||||
return freezeEmbeddedAssetsFrom(s.DB, objectUUID, payload, contentType)
|
||||
}
|
||||
|
||||
func freezeEmbeddedAssetsFrom(q interface{ QueryRow(string, ...any) *sql.Row }, objectUUID string, payload []byte, contentType string) ([]byte, []string, error) {
|
||||
if !strings.HasPrefix(strings.ToLower(strings.TrimSpace(contentType)), "text/html") {
|
||||
return payload, nil, nil
|
||||
}
|
||||
z := html.NewTokenizer(bytes.NewReader(payload))
|
||||
var output bytes.Buffer
|
||||
seen := map[string]bool{}
|
||||
resolved := map[string]string{}
|
||||
var hashes []string
|
||||
resolve := func(address string) (string, error) {
|
||||
if hash := resolved[address]; hash != "" {
|
||||
return hash, nil
|
||||
}
|
||||
return documentrefs.Freeze(context.Background(), payload, contentType, func(_ context.Context, address string) (string, error) {
|
||||
var hash string
|
||||
err := q.QueryRow(`SELECT COALESCE(a.head_document_hash,'') FROM assets a JOIN paths p ON p.domain_uuid=a.domain_uuid WHERE p.object_uuid=? AND a.address=? ORDER BY COALESCE(p.is_primary,0) DESC LIMIT 1`, objectUUID, address).Scan(&hash)
|
||||
if err != nil || !validDocumentHash(hash) {
|
||||
if errors.Is(err, sql.ErrNoRows) || err == nil {
|
||||
return "", fmt.Errorf("%w: %s", ErrEmbeddedAssetUnavailable, address)
|
||||
}
|
||||
return "", err
|
||||
if errors.Is(err, sql.ErrNoRows) || err == nil && !validDocumentHash(hash) {
|
||||
return "", fmt.Errorf("%w: %s", ErrEmbeddedAssetUnavailable, address)
|
||||
}
|
||||
resolved[address] = hash
|
||||
if !seen[hash] {
|
||||
seen[hash] = true
|
||||
hashes = append(hashes, hash)
|
||||
}
|
||||
return hash, nil
|
||||
}
|
||||
for {
|
||||
typeOfToken := z.Next()
|
||||
if typeOfToken == html.ErrorToken {
|
||||
if errors.Is(z.Err(), io.EOF) {
|
||||
return output.Bytes(), hashes, nil
|
||||
}
|
||||
return nil, nil, z.Err()
|
||||
}
|
||||
if typeOfToken != html.StartTagToken && typeOfToken != html.SelfClosingTagToken {
|
||||
output.Write(z.Raw())
|
||||
continue
|
||||
}
|
||||
token := z.Token()
|
||||
changed := false
|
||||
assetRef := tokenAttribute(token.Attr, "asset-ref")
|
||||
legacyImmutableRef := tokenAttribute(token.Attr, "immutable-ref")
|
||||
if assetRef == "" && strings.HasPrefix(legacyImmutableRef, "asset:") {
|
||||
assetRef = legacyImmutableRef
|
||||
}
|
||||
if strings.HasPrefix(assetRef, "asset:") {
|
||||
hash, err := resolve(assetRef)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
setTokenAttribute(&token, "asset-ref", assetRef)
|
||||
setTokenAttribute(&token, "immutable-ref", "sha256:"+hash)
|
||||
changed = true
|
||||
}
|
||||
for _, name := range []string{"src", "href", "poster", "data"} {
|
||||
provenanceName := "data-pageswiki-asset-" + name
|
||||
address := tokenAttribute(token.Attr, provenanceName)
|
||||
if address == "" {
|
||||
candidate := tokenAttribute(token.Attr, name)
|
||||
if strings.HasPrefix(candidate, "asset:") {
|
||||
address = candidate
|
||||
}
|
||||
}
|
||||
if !strings.HasPrefix(address, "asset:") {
|
||||
continue
|
||||
}
|
||||
hash, err := resolve(address)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
setTokenAttribute(&token, provenanceName, address)
|
||||
setTokenAttribute(&token, name, "/_/obj/sha256:"+hash)
|
||||
changed = true
|
||||
}
|
||||
if changed {
|
||||
output.WriteString(token.String())
|
||||
} else {
|
||||
output.Write(z.Raw())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func tokenAttribute(attributes []html.Attribute, name string) string {
|
||||
for _, attribute := range attributes {
|
||||
if strings.EqualFold(attribute.Key, name) {
|
||||
return strings.TrimSpace(attribute.Val)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func setTokenAttribute(token *html.Token, name, value string) {
|
||||
for index := range token.Attr {
|
||||
if strings.EqualFold(token.Attr[index].Key, name) {
|
||||
token.Attr[index].Key = name
|
||||
token.Attr[index].Val = value
|
||||
return
|
||||
}
|
||||
}
|
||||
token.Attr = append(token.Attr, html.Attribute{Key: name, Val: value})
|
||||
return hash, err
|
||||
})
|
||||
}
|
||||
+105
-17
@@ -1,12 +1,16 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/metadata"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
@@ -48,6 +52,14 @@ type AssetDependency struct {
|
||||
}
|
||||
|
||||
func (s *Store) EnsureAssetsSchema() error {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
// Native core migrations own these tables; never run SQLite adoption.
|
||||
rows, err := s.DB.Query(`SELECT a.domain_uuid,a.address,a.object_uuid,a.head_document_hash,a.content_type,a.filename,a.size_bytes,a.created_at_ms,a.updated_at_ms,h.id,h.action,h.document_hash,h.actor_uuid FROM assets a JOIN asset_head_history h ON h.domain_uuid=a.domain_uuid AND h.address=a.address WHERE 1=0`)
|
||||
if err != nil {
|
||||
return s.sqlError("asset schema", err)
|
||||
}
|
||||
return rows.Close()
|
||||
}
|
||||
columns, err := assetTableColumns(s.DB, "assets")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -129,8 +141,9 @@ func assetTableColumns(db *sql.DB, table string) (map[string]bool, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) ListAssets(domainUUID string) ([]Asset, error) {
|
||||
rows, err := s.DB.Query(`SELECT a.domain_uuid,a.address,a.object_uuid,COALESCE(a.head_document_hash,''),a.content_type,a.filename,a.size_bytes,a.created_at_ms,a.updated_at_ms,(SELECT COUNT(DISTINCT x.revision_uuid) FROM revision_document_refs x JOIN revisions r ON r.uuid=x.revision_uuid WHERE x.document_hash IN (SELECT document_hash FROM asset_head_history h WHERE h.domain_uuid=a.domain_uuid AND h.address=a.address UNION SELECT a.head_document_hash) AND EXISTS(SELECT 1 FROM paths p WHERE p.domain_uuid=a.domain_uuid AND p.object_uuid=r.object_uuid)) FROM assets a WHERE a.domain_uuid=? ORDER BY a.updated_at_ms DESC,a.address`, domainUUID)
|
||||
func (s *Store) ListAssets(domainUUID string) (_ []Asset, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("list assets", resultErr) }()
|
||||
rows, err := s.DB.Query(`SELECT a.domain_uuid,a.address,a.object_uuid,COALESCE(a.head_document_hash,''),a.content_type,a.filename,a.size_bytes,a.created_at_ms,a.updated_at_ms,(SELECT COUNT(DISTINCT x.revision_uuid) FROM revision_document_refs x JOIN revisions r ON r.uuid=x.revision_uuid WHERE x.document_hash IN (SELECT document_hash FROM asset_head_history h WHERE h.domain_uuid=a.domain_uuid AND h.address=a.address UNION SELECT a.head_document_hash) AND EXISTS(SELECT 1 FROM paths p WHERE p.domain_uuid=a.domain_uuid AND p.object_uuid=r.object_uuid)) FROM assets a WHERE a.domain_uuid=`+s.sqlParam(1)+` ORDER BY a.updated_at_ms DESC,a.address`, domainUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -146,22 +159,24 @@ func (s *Store) ListAssets(domainUUID string) ([]Asset, error) {
|
||||
return assets, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) GetAsset(domainUUID, address string) (*Asset, error) {
|
||||
func (s *Store) GetAsset(domainUUID, address string) (_ *Asset, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("get asset", resultErr) }()
|
||||
var a Asset
|
||||
err := s.DB.QueryRow(`SELECT a.domain_uuid,a.address,a.object_uuid,COALESCE(a.head_document_hash,''),a.content_type,a.filename,a.size_bytes,a.created_at_ms,a.updated_at_ms,(SELECT COUNT(DISTINCT x.revision_uuid) FROM revision_document_refs x JOIN revisions r ON r.uuid=x.revision_uuid WHERE x.document_hash IN (SELECT document_hash FROM asset_head_history h WHERE h.domain_uuid=a.domain_uuid AND h.address=a.address UNION SELECT a.head_document_hash) AND EXISTS(SELECT 1 FROM paths p WHERE p.domain_uuid=a.domain_uuid AND p.object_uuid=r.object_uuid)) FROM assets a WHERE a.domain_uuid=? AND a.address=?`, domainUUID, address).Scan(&a.DomainUUID, &a.Address, &a.ObjectUUID, &a.HeadDocumentHash, &a.ContentType, &a.Filename, &a.SizeBytes, &a.CreatedAtMS, &a.UpdatedAtMS, &a.ReferenceCount)
|
||||
err := s.DB.QueryRow(`SELECT a.domain_uuid,a.address,a.object_uuid,COALESCE(a.head_document_hash,''),a.content_type,a.filename,a.size_bytes,a.created_at_ms,a.updated_at_ms,(SELECT COUNT(DISTINCT x.revision_uuid) FROM revision_document_refs x JOIN revisions r ON r.uuid=x.revision_uuid WHERE x.document_hash IN (SELECT document_hash FROM asset_head_history h WHERE h.domain_uuid=a.domain_uuid AND h.address=a.address UNION SELECT a.head_document_hash) AND EXISTS(SELECT 1 FROM paths p WHERE p.domain_uuid=a.domain_uuid AND p.object_uuid=r.object_uuid)) FROM assets a WHERE a.domain_uuid=`+s.sqlParam(1)+` AND a.address=`+s.sqlParam(2), domainUUID, address).Scan(&a.DomainUUID, &a.Address, &a.ObjectUUID, &a.HeadDocumentHash, &a.ContentType, &a.Filename, &a.SizeBytes, &a.CreatedAtMS, &a.UpdatedAtMS, &a.ReferenceCount)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrAssetNotFound
|
||||
}
|
||||
return &a, err
|
||||
}
|
||||
|
||||
func (s *Store) GetAssetByHeadHash(domainUUID, hash string) (*Asset, error) {
|
||||
func (s *Store) GetAssetByHeadHash(domainUUID, hash string) (_ *Asset, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("get asset hash", resultErr) }()
|
||||
hash = strings.ToLower(strings.TrimSpace(hash))
|
||||
if !validDocumentHash(hash) {
|
||||
return nil, ErrAssetNotFound
|
||||
}
|
||||
var a Asset
|
||||
err := s.DB.QueryRow(`SELECT a.domain_uuid,a.address,a.object_uuid,COALESCE(a.head_document_hash,''),a.content_type,a.filename,a.size_bytes,a.created_at_ms,a.updated_at_ms,(SELECT COUNT(DISTINCT x.revision_uuid) FROM revision_document_refs x JOIN revisions r ON r.uuid=x.revision_uuid WHERE x.document_hash IN (SELECT document_hash FROM asset_head_history h WHERE h.domain_uuid=a.domain_uuid AND h.address=a.address UNION SELECT a.head_document_hash) AND EXISTS(SELECT 1 FROM paths p WHERE p.domain_uuid=a.domain_uuid AND p.object_uuid=r.object_uuid)) FROM assets a WHERE a.domain_uuid=? AND a.head_document_hash=? LIMIT 1`, domainUUID, hash).Scan(&a.DomainUUID, &a.Address, &a.ObjectUUID, &a.HeadDocumentHash, &a.ContentType, &a.Filename, &a.SizeBytes, &a.CreatedAtMS, &a.UpdatedAtMS, &a.ReferenceCount)
|
||||
err := s.DB.QueryRow(`SELECT a.domain_uuid,a.address,a.object_uuid,COALESCE(a.head_document_hash,''),a.content_type,a.filename,a.size_bytes,a.created_at_ms,a.updated_at_ms,(SELECT COUNT(DISTINCT x.revision_uuid) FROM revision_document_refs x JOIN revisions r ON r.uuid=x.revision_uuid WHERE x.document_hash IN (SELECT document_hash FROM asset_head_history h WHERE h.domain_uuid=a.domain_uuid AND h.address=a.address UNION SELECT a.head_document_hash) AND EXISTS(SELECT 1 FROM paths p WHERE p.domain_uuid=a.domain_uuid AND p.object_uuid=r.object_uuid)) FROM assets a WHERE a.domain_uuid=`+s.sqlParam(1)+` AND a.head_document_hash=`+s.sqlParam(2)+` LIMIT 1`, domainUUID, hash).Scan(&a.DomainUUID, &a.Address, &a.ObjectUUID, &a.HeadDocumentHash, &a.ContentType, &a.Filename, &a.SizeBytes, &a.CreatedAtMS, &a.UpdatedAtMS, &a.ReferenceCount)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrAssetNotFound
|
||||
}
|
||||
@@ -178,6 +193,12 @@ func normalizeAssetAddress(address string) (string, error) {
|
||||
}
|
||||
|
||||
func (s *Store) PutAsset(domainUUID, address string, payload []byte, contentType, filename, expectedHash, actorUUID string) (*Asset, bool, error) {
|
||||
if s.ReadOnly {
|
||||
return nil, false, ErrReadOnly
|
||||
}
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
return s.putNativeAsset(domainUUID, address, payload, contentType, filename, expectedHash, actorUUID)
|
||||
}
|
||||
if s.DocumentRoot == "" {
|
||||
return nil, false, errors.New("Asset writes require a configured Document Store")
|
||||
}
|
||||
@@ -244,8 +265,9 @@ func (s *Store) PutAsset(domainUUID, address string, payload []byte, contentType
|
||||
return a, true, err
|
||||
}
|
||||
|
||||
func (s *Store) ListAssetHistory(domainUUID, address string) ([]AssetHistory, error) {
|
||||
rows, err := s.DB.Query(`SELECT id,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms FROM asset_head_history WHERE domain_uuid=? AND address=? ORDER BY id DESC LIMIT 100`, domainUUID, address)
|
||||
func (s *Store) ListAssetHistory(domainUUID, address string) (_ []AssetHistory, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("asset history", resultErr) }()
|
||||
rows, err := s.DB.Query(`SELECT id,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms FROM asset_head_history WHERE domain_uuid=`+s.sqlParam(1)+` AND address=`+s.sqlParam(2)+` ORDER BY id DESC LIMIT 100`, domainUUID, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -261,7 +283,8 @@ func (s *Store) ListAssetHistory(domainUUID, address string) ([]AssetHistory, er
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) ListAssetDependencies(domainUUID, address string) ([]AssetDependency, error) {
|
||||
func (s *Store) ListAssetDependencies(domainUUID, address string) (_ []AssetDependency, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("asset dependencies", resultErr) }()
|
||||
rows, err := s.DB.Query(`
|
||||
SELECT DISTINCT x.document_hash,r.uuid,r.object_uuid,p.path,
|
||||
COALESCE(r.created_at_ms,COALESCE(r.created_at,0)*1000)
|
||||
@@ -270,7 +293,7 @@ func (s *Store) ListAssetDependencies(domainUUID, address string) ([]AssetDepend
|
||||
JOIN revision_document_refs x ON x.document_hash=h.document_hash
|
||||
JOIN revisions r ON r.uuid=x.revision_uuid
|
||||
JOIN paths p ON p.domain_uuid=h.domain_uuid AND p.object_uuid=r.object_uuid
|
||||
WHERE h.domain_uuid=? AND h.address=?
|
||||
WHERE h.domain_uuid=`+s.sqlParam(1)+` AND h.address=`+s.sqlParam(2)+`
|
||||
ORDER BY COALESCE(r.created_at_ms,COALESCE(r.created_at,0)*1000) DESC,p.path
|
||||
LIMIT 500
|
||||
`, domainUUID, address)
|
||||
@@ -290,6 +313,9 @@ func (s *Store) ListAssetDependencies(domainUUID, address string) ([]AssetDepend
|
||||
}
|
||||
|
||||
func (s *Store) RestoreAssetHead(domainUUID, address string, historyID int64, expectedHash, actorUUID string) (*Asset, error) {
|
||||
if s.ReadOnly {
|
||||
return nil, ErrReadOnly
|
||||
}
|
||||
current, err := s.GetAsset(domainUUID, address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -298,25 +324,40 @@ func (s *Store) RestoreAssetHead(domainUUID, address string, historyID int64, ex
|
||||
return nil, ErrAssetConflict
|
||||
}
|
||||
var historical AssetHistory
|
||||
err = s.DB.QueryRow(`SELECT id,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms FROM asset_head_history WHERE domain_uuid=? AND address=? AND id=?`, domainUUID, address, historyID).Scan(&historical.ID, &historical.Action, &historical.DocumentHash, &historical.ContentType, &historical.Filename, &historical.SizeBytes, &historical.ActorUUID, &historical.CreatedAtMS)
|
||||
err = s.DB.QueryRow(`SELECT id,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms FROM asset_head_history WHERE domain_uuid=`+s.sqlParam(1)+` AND address=`+s.sqlParam(2)+` AND id=`+s.sqlParam(3), domainUUID, address, historyID).Scan(&historical.ID, &historical.Action, &historical.DocumentHash, &historical.ContentType, &historical.Filename, &historical.SizeBytes, &historical.ActorUUID, &historical.CreatedAtMS)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrAssetNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
if s.DocumentRoot == "" {
|
||||
return nil, ErrDocumentStoreRequired
|
||||
}
|
||||
docs, err := s.documentStorage()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
if err = docs.Verify(ctx, historical.DocumentHash); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
reader, err := s.OpenDocument(historical.DocumentHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = reader.Close()
|
||||
now := time.Now().UnixMilli()
|
||||
tx, err := s.DB.Begin()
|
||||
tx, cancel, err := s.beginPathMutation(domainUUID)
|
||||
defer cancel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
result, err := tx.Exec(`UPDATE assets SET head_document_hash=?,content_type=?,filename=?,size_bytes=?,updated_at_ms=? WHERE domain_uuid=? AND address=? AND head_document_hash=?`, historical.DocumentHash, historical.ContentType, historical.Filename, historical.SizeBytes, now, domainUUID, address, current.HeadDocumentHash)
|
||||
result, err := tx.Exec(`UPDATE assets SET head_document_hash=`+s.sqlParam(1)+`,content_type=`+s.sqlParam(2)+`,filename=`+s.sqlParam(3)+`,size_bytes=`+s.sqlParam(4)+`,updated_at_ms=`+s.sqlParam(5)+` WHERE domain_uuid=`+s.sqlParam(6)+` AND address=`+s.sqlParam(7)+` AND head_document_hash=`+s.sqlParam(8), historical.DocumentHash, historical.ContentType, historical.Filename, historical.SizeBytes, now, domainUUID, address, current.HeadDocumentHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -324,7 +365,7 @@ func (s *Store) RestoreAssetHead(domainUUID, address string, historyID int64, ex
|
||||
if affected != 1 {
|
||||
return nil, ErrAssetConflict
|
||||
}
|
||||
if _, err = tx.Exec(`INSERT INTO asset_head_history(domain_uuid,address,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms) VALUES(?,?,'restore',?,?,?,?,?,?)`, domainUUID, address, historical.DocumentHash, historical.ContentType, historical.Filename, historical.SizeBytes, actorUUID, now); err != nil {
|
||||
if _, err = tx.Exec(`INSERT INTO asset_head_history(domain_uuid,address,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,'restore',`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`)`, domainUUID, address, historical.DocumentHash, historical.ContentType, historical.Filename, historical.SizeBytes, actorUUID, now); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
@@ -334,6 +375,9 @@ func (s *Store) RestoreAssetHead(domainUUID, address string, historyID int64, ex
|
||||
}
|
||||
|
||||
func (s *Store) DeleteAsset(domainUUID, address, expectedHash, actorUUID string) error {
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
a, err := s.GetAsset(domainUUID, address)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -342,15 +386,16 @@ func (s *Store) DeleteAsset(domainUUID, address, expectedHash, actorUUID string)
|
||||
return ErrAssetConflict
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
tx, err := s.DB.Begin()
|
||||
tx, cancel, err := s.beginPathMutation(domainUUID)
|
||||
defer cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err = tx.Exec(`INSERT INTO asset_head_history(domain_uuid,address,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms) VALUES(?,?,'delete',?,?,?,?,?,?)`, domainUUID, address, a.HeadDocumentHash, a.ContentType, a.Filename, a.SizeBytes, actorUUID, now); err != nil {
|
||||
if _, err = tx.Exec(`INSERT INTO asset_head_history(domain_uuid,address,action,document_hash,content_type,filename,size_bytes,actor_uuid,created_at_ms) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,'delete',`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`)`, domainUUID, address, a.HeadDocumentHash, a.ContentType, a.Filename, a.SizeBytes, actorUUID, now); err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.Exec(`DELETE FROM assets WHERE domain_uuid=? AND address=? AND head_document_hash=?`, domainUUID, address, a.HeadDocumentHash)
|
||||
result, err := tx.Exec(`DELETE FROM assets WHERE domain_uuid=`+s.sqlParam(1)+` AND address=`+s.sqlParam(2)+` AND head_document_hash=`+s.sqlParam(3), domainUUID, address, a.HeadDocumentHash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -359,3 +404,46 @@ func (s *Store) DeleteAsset(domainUUID, address, expectedHash, actorUUID string)
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) putNativeAsset(domain, address string, payload []byte, contentType, filename, expected, actor string) (*Asset, bool, error) {
|
||||
if s.DocumentRoot == "" {
|
||||
return nil, false, ErrDocumentStoreRequired
|
||||
}
|
||||
address, err := normalizeAssetAddress(address)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if strings.TrimSpace(contentType) == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
repo, err := metadata.NewApplication(s.DB, metadata.PostgreSQL)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
docs, err := s.documentStorage()
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
hash := revisionHash(string(payload))
|
||||
previous, getErr := repo.GetAsset(ctx, domain, address)
|
||||
if getErr != nil && !errors.Is(getErr, metadata.ErrNotFound) {
|
||||
return nil, false, getErr
|
||||
}
|
||||
if previous.HeadDocumentHash != strings.Trim(expected, "\"") {
|
||||
return nil, false, ErrAssetConflict
|
||||
}
|
||||
if err = docs.PutIfAbsent(ctx, hash, int64(len(payload)), bytes.NewReader(payload)); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
a, err := repo.PutAsset(ctx, metadata.Asset{DomainUUID: domain, Address: address, HeadDocumentHash: hash, ContentType: contentType, Filename: strings.TrimSpace(filename), SizeBytes: int64(len(payload))}, strings.Trim(expected, "\""), actor)
|
||||
if errors.Is(err, metadata.ErrConflict) {
|
||||
return nil, false, ErrAssetConflict
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
result, err := s.GetAsset(domain, address)
|
||||
return result, a.UpdatedAtMS != previous.UpdatedAtMS, err
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/geduldh"
|
||||
)
|
||||
|
||||
func TestAssetsBackendLifecycle(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
f := newPublicationBackend(t, b)
|
||||
s := f.s
|
||||
s.DocumentRoot = t.TempDir()
|
||||
if err := s.EnsureAssetsSchema(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
a, changed, err := s.PutAsset(f.domain, "asset:image:logo", []byte("one"), "image/png", "logo.png", "", f.actor)
|
||||
if err != nil || !changed {
|
||||
t.Fatal(a, changed, err)
|
||||
}
|
||||
if _, changed, err = s.PutAsset(f.domain, a.Address, []byte("one"), a.ContentType, a.Filename, a.HeadDocumentHash, f.actor); err != nil || changed {
|
||||
t.Fatal("no-op", changed, err)
|
||||
}
|
||||
obj, err := s.CreatePageAtPathInDomain(f.domain, "/asset-page")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.SaveRevisionContent(obj, []byte(`<img src="asset:image:logo">`), "", "en", "text/html", ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
updated, _, err := s.PutAsset(f.domain, a.Address, []byte("two"), a.ContentType, a.Filename, a.HeadDocumentHash, f.actor)
|
||||
if err != nil || updated.ReferenceCount != 1 {
|
||||
t.Fatal(updated, err)
|
||||
}
|
||||
deps, err := s.ListAssetDependencies(f.domain, a.Address)
|
||||
if err != nil || len(deps) != 1 || deps[0].Path != "/asset-page" {
|
||||
t.Fatal(deps, err)
|
||||
}
|
||||
assets, err := s.ListAssets(f.domain)
|
||||
if err != nil || len(assets) != 1 || assets[0].ReferenceCount != 1 {
|
||||
t.Fatal(assets, err)
|
||||
}
|
||||
byHash, err := s.GetAssetByHeadHash(f.domain, updated.HeadDocumentHash)
|
||||
if err != nil || byHash.ObjectUUID != a.ObjectUUID {
|
||||
t.Fatal(byHash, err)
|
||||
}
|
||||
history, err := s.ListAssetHistory(f.domain, a.Address)
|
||||
if err != nil || len(history) != 2 {
|
||||
t.Fatal(history, err)
|
||||
}
|
||||
if _, err = s.RestoreAssetHead(f.domain, a.Address, history[1].ID, "wrong", f.actor); !errors.Is(err, ErrAssetConflict) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
restored, err := s.RestoreAssetHead(f.domain, a.Address, history[1].ID, updated.HeadDocumentHash, f.actor)
|
||||
if err != nil || restored.HeadDocumentHash != a.HeadDocumentHash {
|
||||
t.Fatal(restored, err)
|
||||
}
|
||||
f.exec(`ALTER TABLE asset_head_history RENAME COLUMN action TO unavailable_action`)
|
||||
if err = s.DeleteAsset(f.domain, a.Address, restored.HeadDocumentHash, f.actor); err == nil {
|
||||
t.Fatal("ignored audit failure")
|
||||
}
|
||||
if current, err := s.GetAsset(f.domain, a.Address); err != nil || current.HeadDocumentHash != restored.HeadDocumentHash {
|
||||
t.Fatal(current, err)
|
||||
}
|
||||
f.exec(`ALTER TABLE asset_head_history RENAME COLUMN unavailable_action TO action`)
|
||||
if err = s.DeleteAsset(f.domain, a.Address, restored.HeadDocumentHash, f.actor); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.GetAsset(f.domain, a.Address); !errors.Is(err, ErrAssetNotFound) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
history, err = s.ListAssetHistory(f.domain, a.Address)
|
||||
if err != nil || len(history) != 4 || history[0].Action != "delete" {
|
||||
t.Fatal(history, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLAssetMissingPayloadAndCAS(t *testing.T) {
|
||||
f := newPublicationBackend(t, storePostgreSQL)
|
||||
s := f.s
|
||||
s.DocumentRoot = t.TempDir()
|
||||
a, _, err := s.PutAsset(f.domain, "asset:image:test", []byte("original"), "image/png", "", "", f.actor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
history, err := s.ListAssetHistory(f.domain, a.Address)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
peer := &Store{DB: s.DB, sqlBackend: storePostgreSQL, DocumentRoot: s.DocumentRoot}
|
||||
results := make(chan error, 2)
|
||||
for i, w := range []*Store{s, peer} {
|
||||
go func(i int, w *Store) {
|
||||
_, _, e := w.PutAsset(f.domain, a.Address, []byte(fmt.Sprint(i)), "image/png", "", a.HeadDocumentHash, f.actor)
|
||||
results <- e
|
||||
}(i, w)
|
||||
}
|
||||
wins := 0
|
||||
for i := 0; i < 2; i++ {
|
||||
e := <-results
|
||||
if e == nil {
|
||||
wins++
|
||||
} else if !errors.Is(e, ErrAssetConflict) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if wins != 1 {
|
||||
t.Fatal(wins)
|
||||
}
|
||||
current, err := s.GetAsset(f.domain, a.Address)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key, _ := geduldh.Key(a.HeadDocumentHash)
|
||||
if err = os.Remove(filepath.Join(s.DocumentRoot, key)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.RestoreAssetHead(f.domain, a.Address, history[0].ID, current.HeadDocumentHash, f.actor); err == nil {
|
||||
t.Fatal("missing payload restored")
|
||||
}
|
||||
}
|
||||
+111
-170
@@ -1,6 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
@@ -82,70 +83,18 @@ func VerifyPassword(encoded, password string) bool {
|
||||
func (s *Store) PrincipalCount() (int, error) {
|
||||
var count int
|
||||
err := s.DB.QueryRow(`SELECT COUNT(*) FROM principals`).Scan(&count)
|
||||
return count, err
|
||||
return count, s.sqlError("principal count", err)
|
||||
}
|
||||
|
||||
func (s *Store) BootstrapOwner(domainUUID, username, password, displayName, email string) (*Principal, error) {
|
||||
username = strings.ToLower(strings.TrimSpace(username))
|
||||
if !authUsernamePattern.MatchString(username) {
|
||||
return nil, errors.New("username must be 2-64 lowercase letters, digits, underscores, or hyphens")
|
||||
}
|
||||
encoded, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var count int
|
||||
if err = tx.QueryRow(`SELECT COUNT(*) FROM principals`).Scan(&count); err != nil || count != 0 {
|
||||
if err == nil {
|
||||
err = errors.New("owner already exists")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
principalID, membershipID, roleID := uuid.NewString(), uuid.NewString(), uuid.NewString()
|
||||
if strings.TrimSpace(displayName) == "" {
|
||||
displayName = username
|
||||
}
|
||||
queries := []struct {
|
||||
q string
|
||||
args []any
|
||||
}{
|
||||
{`INSERT INTO principals(uuid,display_name,email,state,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,?)`, []any{principalID, displayName, strings.TrimSpace(email), "active", now, now}},
|
||||
{`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,subject,created_at_ms) VALUES(?,?,?,?,?)`, []any{uuid.NewString(), principalID, "password", username, now}},
|
||||
{`INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,?,?)`, []any{uuid.NewString(), principalID, "password", username, encoded, now, now}},
|
||||
{`INSERT INTO domain_memberships(uuid,domain_uuid,principal_uuid,state,created_at_ms) VALUES(?,?,?,?,?)`, []any{membershipID, domainUUID, principalID, "active", now}},
|
||||
{`INSERT INTO auth_roles(uuid,domain_uuid,name,description,created_at_ms) VALUES(?,?,?,?,?)`, []any{roleID, domainUUID, "owner", "Domain owner", now}},
|
||||
{`INSERT INTO auth_role_bindings(membership_uuid,role_uuid,created_at_ms) VALUES(?,?,?)`, []any{membershipID, roleID, now}},
|
||||
}
|
||||
for _, item := range queries {
|
||||
if _, err = tx.Exec(item.q, item.args...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
for _, capability := range []string{"read", "write", "rename", "delete", "link", "manage", "delegate"} {
|
||||
if _, err = tx.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(?,?,?,?,?,?)`, uuid.NewString(), roleID, capability, "domain", domainUUID, now); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = s.EnsureBuiltinAdminGroups(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Principal{UUID: principalID, DisplayName: displayName, Email: email, State: "active"}, nil
|
||||
return s.BootstrapOwnerContext(context.Background(), domainUUID, username, password, displayName, email)
|
||||
}
|
||||
|
||||
func (s *Store) AuthenticatePassword(identifier, password string) (*Principal, error) {
|
||||
identifier = strings.ToLower(strings.TrimSpace(identifier))
|
||||
var p Principal
|
||||
var encoded string
|
||||
err := s.DB.QueryRow(`SELECT p.uuid,p.display_name,p.email,p.state,c.secret_hash FROM auth_credentials c JOIN principals p ON p.uuid=c.principal_uuid WHERE c.kind='password' AND c.identifier=? AND c.enabled=1`, identifier).Scan(&p.UUID, &p.DisplayName, &p.Email, &p.State, &encoded)
|
||||
err := s.DB.QueryRow(`SELECT p.uuid,p.display_name,p.email,p.state,c.secret_hash FROM auth_credentials c JOIN principals p ON p.uuid=c.principal_uuid WHERE c.kind='password' AND c.identifier=`+s.sqlParam(1)+` AND c.enabled=1`, identifier).Scan(&p.UUID, &p.DisplayName, &p.Email, &p.State, &encoded)
|
||||
if err != nil || (p.State != "active" && p.State != "readonly") || !VerifyPassword(encoded, password) {
|
||||
return nil, ErrAccessDenied
|
||||
}
|
||||
@@ -178,25 +127,33 @@ func (s *Store) createTypedAuthSessionForLifetime(domainUUID, principalUUID stri
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
hash := base64.RawURLEncoding.EncodeToString(sum[:])
|
||||
now := time.Now().UnixMilli()
|
||||
result, err := s.DB.Exec(`INSERT INTO auth_sessions(uuid,token_hash,principal_uuid,domain_uuid,auth_time_ms,expires_at_ms,last_seen_at_ms,mfa_level)
|
||||
SELECT ?,?,p.uuid,m.domain_uuid,?,?,?,? FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid
|
||||
WHERE p.uuid=? AND p.state IN ('active','readonly') AND m.domain_uuid=? AND m.state='active'`,
|
||||
uuid.NewString(), hash, now, now+lifetime.Milliseconds(), now, mfaLevel, principalUUID, domainUUID)
|
||||
// Share the administration lock with suspension and inherited-membership
|
||||
// revocation; an INSERT snapshot alone can race a concurrent suspension.
|
||||
err = s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
result, err := tx.ExecContext(ctx, fmt.Sprintf(`INSERT INTO auth_sessions(uuid,token_hash,principal_uuid,domain_uuid,auth_time_ms,expires_at_ms,last_seen_at_ms,mfa_level)
|
||||
SELECT %s,%s,p.uuid,m.domain_uuid,%s,%s,%s,%s FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid
|
||||
WHERE p.uuid=%s AND p.state IN ('active','readonly') AND m.domain_uuid=%s AND m.state='active'`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3), s.sqlParam(4), s.sqlParam(5), s.sqlParam(6), s.sqlParam(7), s.sqlParam(8)),
|
||||
uuid.NewString(), hash, now, now+lifetime.Milliseconds(), now, mfaLevel, principalUUID, domainUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count, err := result.RowsAffected(); err != nil || count != 1 {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if count, err := result.RowsAffected(); err != nil || count != 1 {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
func (s *Store) PrincipalForSession(domainUUID, token string) (*Principal, error) {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
hash := base64.RawURLEncoding.EncodeToString(sum[:])
|
||||
var p Principal
|
||||
err := s.DB.QueryRow(`SELECT p.uuid,COALESCE((SELECT subject FROM principal_identities i WHERE i.principal_uuid=p.uuid AND i.provider_type='password' LIMIT 1),p.uuid),p.display_name,p.email,p.state,length(p.avatar)>0 FROM auth_sessions s JOIN principals p ON p.uuid=s.principal_uuid
|
||||
err := s.DB.QueryRow(fmt.Sprintf(`SELECT p.uuid,COALESCE((SELECT subject FROM principal_identities i WHERE i.principal_uuid=p.uuid AND i.provider_type='password' ORDER BY i.subject,i.uuid LIMIT 1),p.uuid),p.display_name,p.email,p.state,length(p.avatar)>0 FROM auth_sessions s JOIN principals p ON p.uuid=s.principal_uuid
|
||||
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=s.domain_uuid AND m.state='active'
|
||||
WHERE s.token_hash=? AND s.domain_uuid=? AND s.revoked_at_ms=0 AND s.expires_at_ms>?`, hash, domainUUID, time.Now().UnixMilli()).Scan(&p.UUID, &p.Username, &p.DisplayName, &p.Email, &p.State, &p.HasAvatar)
|
||||
WHERE s.token_hash=%s AND s.domain_uuid=%s AND s.revoked_at_ms=0 AND s.expires_at_ms>%s`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), hash, domainUUID, time.Now().UnixMilli()).Scan(&p.UUID, &p.Username, &p.DisplayName, &p.Email, &p.State, &p.HasAvatar)
|
||||
if err != nil || (p.State != "active" && p.State != "readonly") {
|
||||
return nil, ErrAccessDenied
|
||||
}
|
||||
@@ -206,28 +163,32 @@ func (s *Store) PrincipalForSession(domainUUID, token string) (*Principal, error
|
||||
func (s *Store) PrincipalByUsername(domainUUID, username string) (*Principal, error) {
|
||||
username = strings.ToLower(strings.TrimSpace(username))
|
||||
var p Principal
|
||||
err := s.DB.QueryRow(`SELECT p.uuid,i.subject,p.display_name,p.email,p.state,length(p.avatar)>0 FROM principal_identities i JOIN principals p ON p.uuid=i.principal_uuid JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? WHERE i.provider_type='password' AND i.subject=?`, domainUUID, username).Scan(&p.UUID, &p.Username, &p.DisplayName, &p.Email, &p.State, &p.HasAvatar)
|
||||
return &p, err
|
||||
err := s.DB.QueryRow(fmt.Sprintf(`SELECT p.uuid,i.subject,p.display_name,p.email,p.state,length(p.avatar)>0 FROM principal_identities i JOIN principals p ON p.uuid=i.principal_uuid JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=%s WHERE i.provider_type='password' AND i.subject=%s ORDER BY i.uuid LIMIT 1`, s.sqlParam(1), s.sqlParam(2)), domainUUID, username).Scan(&p.UUID, &p.Username, &p.DisplayName, &p.Email, &p.State, &p.HasAvatar)
|
||||
return &p, s.sqlError("principal username", err)
|
||||
}
|
||||
|
||||
func (s *Store) SetPrincipalAvatar(principalUUID, contentType string, data []byte) error {
|
||||
_, err := s.DB.Exec(`UPDATE principals SET avatar=?,avatar_content_type=?,updated_at_ms=? WHERE uuid=?`, data, contentType, time.Now().UnixMilli(), principalUUID)
|
||||
return err
|
||||
// An empty avatar is stored as a zero-length BLOB/BYTEA, never SQL NULL.
|
||||
if data == nil {
|
||||
data = []byte{}
|
||||
}
|
||||
_, err := s.DB.Exec(fmt.Sprintf(`UPDATE principals SET avatar=%s,avatar_content_type=%s,updated_at_ms=%s WHERE uuid=%s`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3), s.sqlParam(4)), data, contentType, time.Now().UnixMilli(), principalUUID)
|
||||
return s.sqlError("principal avatar update", err)
|
||||
}
|
||||
func (s *Store) PrincipalAvatar(principalUUID string) (string, []byte, error) {
|
||||
var contentType string
|
||||
var data []byte
|
||||
err := s.DB.QueryRow(`SELECT avatar_content_type,avatar FROM principals WHERE uuid=?`, principalUUID).Scan(&contentType, &data)
|
||||
return contentType, data, err
|
||||
err := s.DB.QueryRow(`SELECT avatar_content_type,avatar FROM principals WHERE uuid=`+s.sqlParam(1), principalUUID).Scan(&contentType, &data)
|
||||
return contentType, data, s.sqlError("principal avatar", err)
|
||||
}
|
||||
func (s *Store) RevokeAuthSession(token string) error {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
_, err := s.DB.Exec(`UPDATE auth_sessions SET revoked_at_ms=? WHERE token_hash=?`, time.Now().UnixMilli(), base64.RawURLEncoding.EncodeToString(sum[:]))
|
||||
return err
|
||||
_, err := s.DB.Exec(`UPDATE auth_sessions SET revoked_at_ms=`+s.sqlParam(1)+` WHERE token_hash=`+s.sqlParam(2)+` AND revoked_at_ms=0`, time.Now().UnixMilli(), base64.RawURLEncoding.EncodeToString(sum[:]))
|
||||
return s.sqlError("revoke session", err)
|
||||
}
|
||||
|
||||
func (s *Store) ListDomainPrincipals(domainUUID string) ([]Principal, error) {
|
||||
rows, err := s.DB.Query(`SELECT p.uuid,p.display_name,p.email,p.state FROM domain_memberships m JOIN principals p ON p.uuid=m.principal_uuid WHERE m.domain_uuid=? ORDER BY lower(p.display_name),p.uuid`, domainUUID)
|
||||
rows, err := s.DB.Query(`SELECT p.uuid,p.display_name,p.email,p.state FROM domain_memberships m JOIN principals p ON p.uuid=m.principal_uuid WHERE m.domain_uuid=`+s.sqlParam(1)+` ORDER BY lower(p.display_name),p.uuid`, domainUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -243,7 +204,7 @@ func (s *Store) ListDomainPrincipals(domainUUID string) ([]Principal, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (s *Store) ListAuthSessions(domainUUID string) ([]AuthSessionInfo, error) {
|
||||
rows, err := s.DB.Query(`SELECT s.uuid,s.principal_uuid,p.display_name,s.auth_time_ms,s.expires_at_ms,s.last_seen_at_ms,s.mfa_level FROM auth_sessions s JOIN principals p ON p.uuid=s.principal_uuid WHERE s.domain_uuid=? AND s.revoked_at_ms=0 AND s.expires_at_ms>? ORDER BY s.last_seen_at_ms DESC`, domainUUID, time.Now().UnixMilli())
|
||||
rows, err := s.DB.Query(fmt.Sprintf(`SELECT s.uuid,s.principal_uuid,p.display_name,s.auth_time_ms,s.expires_at_ms,s.last_seen_at_ms,s.mfa_level FROM auth_sessions s JOIN principals p ON p.uuid=s.principal_uuid WHERE s.domain_uuid=%s AND s.revoked_at_ms=0 AND s.expires_at_ms>%s ORDER BY s.last_seen_at_ms DESC,s.uuid`, s.sqlParam(1), s.sqlParam(2)), domainUUID, time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -259,9 +220,9 @@ func (s *Store) ListAuthSessions(domainUUID string) ([]AuthSessionInfo, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (s *Store) RevokeAuthSessionByID(domainUUID, sessionUUID string) error {
|
||||
result, err := s.DB.Exec(`UPDATE auth_sessions SET revoked_at_ms=? WHERE domain_uuid=? AND uuid=? AND revoked_at_ms=0`, time.Now().UnixMilli(), domainUUID, sessionUUID)
|
||||
result, err := s.DB.Exec(fmt.Sprintf(`UPDATE auth_sessions SET revoked_at_ms=%s WHERE domain_uuid=%s AND uuid=%s AND revoked_at_ms=0`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), time.Now().UnixMilli(), domainUUID, sessionUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
return s.sqlError("revoke session by ID", err)
|
||||
}
|
||||
count, _ := result.RowsAffected()
|
||||
if count == 0 {
|
||||
@@ -270,31 +231,32 @@ func (s *Store) RevokeAuthSessionByID(domainUUID, sessionUUID string) error {
|
||||
return nil
|
||||
}
|
||||
func (s *Store) SetPrincipalState(domainUUID, principalUUID, state string) error {
|
||||
return s.SetPrincipalStateContext(context.Background(), domainUUID, principalUUID, state)
|
||||
}
|
||||
|
||||
func (s *Store) SetPrincipalStateContext(ctx context.Context, domainUUID, principalUUID, state string) error {
|
||||
if state != "active" && state != "readonly" && state != "suspended" && state != "quarantined" {
|
||||
return errors.New("invalid principal state")
|
||||
}
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
result, err := tx.Exec(`UPDATE principals SET state=?,updated_at_ms=? WHERE uuid=? AND EXISTS(SELECT 1 FROM domain_memberships WHERE domain_uuid=? AND principal_uuid=?)`, state, time.Now().UnixMilli(), principalUUID, domainUUID, principalUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
count, _ := result.RowsAffected()
|
||||
if count == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
if state != "active" {
|
||||
if _, err = tx.Exec(`UPDATE auth_sessions SET revoked_at_ms=? WHERE principal_uuid=?`, time.Now().UnixMilli(), principalUUID); err != nil {
|
||||
return s.groupWrite(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
||||
result, err := tx.ExecContext(ctx, fmt.Sprintf(`UPDATE principals SET state=%s,updated_at_ms=%s WHERE uuid=%s AND EXISTS(SELECT 1 FROM domain_memberships WHERE domain_uuid=%s AND principal_uuid=%s)`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3), s.sqlParam(4), s.sqlParam(5)), state, time.Now().UnixMilli(), principalUUID, domainUUID, principalUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err = projectCampusAdminMembershipsTx(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
count, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if count == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
if state != "active" {
|
||||
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`UPDATE auth_sessions SET revoked_at_ms=%s WHERE principal_uuid=%s AND revoked_at_ms=0`, s.sqlParam(1), s.sqlParam(2)), time.Now().UnixMilli(), principalUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return s.projectCampusAdminMembershipsContext(ctx, tx)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) UpdatePrincipalProfile(principalUUID, displayName, email string) error {
|
||||
@@ -303,59 +265,21 @@ func (s *Store) UpdatePrincipalProfile(principalUUID, displayName, email string)
|
||||
if displayName == "" {
|
||||
return errors.New("display name is required")
|
||||
}
|
||||
_, err := s.DB.Exec(`UPDATE principals SET display_name=?,email=?,updated_at_ms=? WHERE uuid=?`, displayName, email, time.Now().UnixMilli(), principalUUID)
|
||||
return err
|
||||
_, err := s.DB.Exec(fmt.Sprintf(`UPDATE principals SET display_name=%s,email=%s,updated_at_ms=%s WHERE uuid=%s`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3), s.sqlParam(4)), displayName, email, time.Now().UnixMilli(), principalUUID)
|
||||
return s.sqlError("principal profile update", err)
|
||||
}
|
||||
|
||||
func (s *Store) CreatePasswordAccount(domainUUID, username, password, displayName, email string) (*Principal, error) {
|
||||
username = strings.ToLower(strings.TrimSpace(username))
|
||||
if !authUsernamePattern.MatchString(username) {
|
||||
return nil, errors.New("username must be 2-64 lowercase letters, digits, underscores, or hyphens")
|
||||
}
|
||||
encoded, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
now := time.Now().UnixMilli()
|
||||
p := &Principal{UUID: uuid.NewString(), DisplayName: strings.TrimSpace(displayName), Email: strings.TrimSpace(email), State: "active"}
|
||||
if p.DisplayName == "" {
|
||||
p.DisplayName = username
|
||||
}
|
||||
membershipID := uuid.NewString()
|
||||
roleID := ""
|
||||
err = tx.QueryRow(`SELECT uuid FROM auth_roles WHERE domain_uuid=? AND name='member'`, domainUUID).Scan(&roleID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
roleID = uuid.NewString()
|
||||
if _, err = tx.Exec(`INSERT INTO auth_roles(uuid,domain_uuid,name,description,created_at_ms) VALUES(?,?,?,?,?)`, roleID, domainUUID, "member", "Domain member", now); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err = tx.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(?,?,?,?,?,?)`, uuid.NewString(), roleID, "read", "domain", domainUUID, now); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
queries := []struct {
|
||||
q string
|
||||
args []any
|
||||
}{{`INSERT INTO principals(uuid,display_name,email,state,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,?)`, []any{p.UUID, p.DisplayName, p.Email, p.State, now, now}}, {`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,subject,created_at_ms) VALUES(?,?,?,?,?)`, []any{uuid.NewString(), p.UUID, "password", username, now}}, {`INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,?,?)`, []any{uuid.NewString(), p.UUID, "password", username, encoded, now, now}}, {`INSERT INTO domain_memberships(uuid,domain_uuid,principal_uuid,state,created_at_ms) VALUES(?,?,?,?,?)`, []any{membershipID, domainUUID, p.UUID, "active", now}}, {`INSERT INTO auth_role_bindings(membership_uuid,role_uuid,created_at_ms) VALUES(?,?,?)`, []any{membershipID, roleID, now}}}
|
||||
for _, item := range queries {
|
||||
if _, err = tx.Exec(item.q, item.args...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
return s.CreatePasswordAccountContext(context.Background(), domainUUID, username, password, displayName, email)
|
||||
}
|
||||
|
||||
func (s *Store) EnsureAuthSchema() error {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
if err := s.ensurePostgreSQLAuthFoundation(context.Background()); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.ensurePostgreSQLLocalityFoundation(context.Background())
|
||||
}
|
||||
statements := []string{
|
||||
`CREATE TABLE IF NOT EXISTS principals (
|
||||
uuid TEXT PRIMARY KEY, display_name TEXT NOT NULL DEFAULT '',
|
||||
@@ -453,9 +377,19 @@ func (s *Store) EnsureAuthSchema() error {
|
||||
}
|
||||
|
||||
func (s *Store) Authorize(request AuthorizationRequest) error {
|
||||
return s.AuthorizeContext(context.Background(), request)
|
||||
}
|
||||
|
||||
func (s *Store) AuthorizeContext(ctx context.Context, request AuthorizationRequest) error {
|
||||
return s.policyRead(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
||||
return s.authorizeSnapshot(ctx, tx, request, false)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) authorizeSnapshot(ctx context.Context, tx *sql.Tx, request AuthorizationRequest, exactObject bool) error {
|
||||
request.Capability = strings.ToLower(strings.TrimSpace(request.Capability))
|
||||
request.TargetType = strings.ToLower(strings.TrimSpace(request.TargetType))
|
||||
base := s.authorizeGrant(request, false)
|
||||
base := s.authorizeGrantContext(ctx, tx, request, exactObject)
|
||||
if base != nil && !errors.Is(base, ErrAccessDenied) {
|
||||
return base
|
||||
}
|
||||
@@ -465,14 +399,14 @@ func (s *Store) Authorize(request AuthorizationRequest) error {
|
||||
return base
|
||||
}
|
||||
if request.TargetType == "object" {
|
||||
if err := s.CheckObjectLocalityAccessCaps(request.DomainUUID, request.PrincipalUUID, request.Capability, request.TargetID); err != nil {
|
||||
if err := s.checkObjectLocalityAccessCaps(ctx, tx, request.DomainUUID, request.PrincipalUUID, request.Capability, request.TargetID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if request.TargetType != "path" {
|
||||
return base
|
||||
}
|
||||
p, err := loadLocalityAccess(s.DB, request.DomainUUID, request.PrincipalUUID)
|
||||
p, err := s.loadLocalityAccess(cityReadQuery{ctx, tx}, request.DomainUUID, request.PrincipalUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -485,57 +419,64 @@ func (s *Store) Authorize(request AuthorizationRequest) error {
|
||||
// AuthorizeExactObject excludes broad Domain/wildcard grants. Global identity
|
||||
// alone must not extend a Domain administrator's authority to another Domain.
|
||||
func (s *Store) AuthorizeExactObject(request AuthorizationRequest) error {
|
||||
return s.AuthorizeExactObjectContext(context.Background(), request)
|
||||
}
|
||||
|
||||
func (s *Store) AuthorizeExactObjectContext(ctx context.Context, request AuthorizationRequest) error {
|
||||
if request.TargetType != "object" || request.TargetID == "" || request.TargetID == "*" {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
request.Capability = strings.ToLower(strings.TrimSpace(request.Capability))
|
||||
switch request.Capability {
|
||||
case "read", "write", "create", "upload", "rename", "link", "delete":
|
||||
if err := s.CheckObjectLocalityAccessCaps(request.DomainUUID, request.PrincipalUUID, request.Capability, request.TargetID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return s.authorizeGrant(request, true)
|
||||
return s.policyRead(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
||||
return s.authorizeSnapshot(ctx, tx, request, true)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Store) authorizeGrant(request AuthorizationRequest, exactObject bool) error {
|
||||
return authorizeGrantFrom(s.DB, request, exactObject)
|
||||
return s.authorizeGrantContext(context.Background(), s.DB, request, exactObject)
|
||||
}
|
||||
|
||||
type authQueryer interface {
|
||||
pathQueryer
|
||||
QueryRow(string, ...any) *sql.Row
|
||||
groupPermissionQueryer
|
||||
QueryRowContext(context.Context, string, ...any) *sql.Row
|
||||
}
|
||||
|
||||
func authorizeGrantFrom(q authQueryer, request AuthorizationRequest, exactObject bool) error {
|
||||
// Compatibility entry point for existing SQLite-only transactions.
|
||||
return (&Store{}).authorizeGrantContext(context.Background(), q, request, exactObject)
|
||||
}
|
||||
|
||||
// Evaluates direct role bindings and group grants only. Object locality,
|
||||
// publication and protected namespace restrictions remain in Authorize and
|
||||
// must not be bypassed by treating this component as complete authorization.
|
||||
func (s *Store) authorizeGrantContext(ctx context.Context, q authQueryer, request AuthorizationRequest, exactObject bool) error {
|
||||
request.Capability = strings.ToLower(strings.TrimSpace(request.Capability))
|
||||
request.TargetType = strings.ToLower(strings.TrimSpace(request.TargetType))
|
||||
if request.DomainUUID == "" || request.PrincipalUUID == "" || request.Capability == "" {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
target := `((g.target_type=? AND (g.target_id=? OR g.target_id='*')) OR (g.target_type='domain' AND g.target_id=?))`
|
||||
target := fmt.Sprintf(`((g.target_type=%s AND (g.target_id=%s OR g.target_id='*')) OR (g.target_type='domain' AND g.target_id=%s))`, s.sqlParam(6), s.sqlParam(7), s.sqlParam(8))
|
||||
args := []any{request.DomainUUID, request.PrincipalUUID, request.Capability, request.Capability, request.Capability, request.TargetType, request.TargetID}
|
||||
if exactObject {
|
||||
target = `(g.target_type=? AND g.target_id=?)`
|
||||
target = fmt.Sprintf(`(g.target_type=%s AND g.target_id=%s)`, s.sqlParam(6), s.sqlParam(7))
|
||||
} else {
|
||||
args = append(args, request.DomainUUID)
|
||||
}
|
||||
var allowed int
|
||||
err := q.QueryRow(`SELECT EXISTS(
|
||||
var allowed bool
|
||||
err := q.QueryRowContext(ctx, fmt.Sprintf(`SELECT EXISTS(
|
||||
SELECT 1 FROM principals p
|
||||
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=?
|
||||
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=%s
|
||||
JOIN auth_role_bindings b ON b.membership_uuid=m.uuid
|
||||
JOIN auth_roles r ON r.uuid=b.role_uuid AND r.domain_uuid=m.domain_uuid
|
||||
JOIN auth_capability_grants g ON g.role_uuid=r.uuid
|
||||
WHERE p.uuid=? AND (p.state='active' OR (p.state='readonly' AND ?='read')) AND m.state='active'
|
||||
AND (g.capability=? OR (? IN ('create','upload') AND g.capability='write'))
|
||||
AND `+target+`)`, args...).Scan(&allowed)
|
||||
WHERE p.uuid=%s AND (p.state='active' OR (p.state='readonly' AND %s='read')) AND m.state='active'
|
||||
AND (g.capability=%s OR (%s IN ('create','upload') AND g.capability='write'))
|
||||
AND `+target+`)`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3), s.sqlParam(4), s.sqlParam(5)), args...).Scan(&allowed)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
return s.sqlError("role grant evaluation", err)
|
||||
}
|
||||
if allowed == 0 {
|
||||
if !allowed {
|
||||
if request.TargetType == "path" {
|
||||
groupAllowed, groupErr := groupPermissionAllowsFrom(q, request.DomainUUID, request.PrincipalUUID, request.Capability, request.TargetID)
|
||||
groupAllowed, groupErr := s.groupPermissionAllowsContext(ctx, q, request.DomainUUID, request.PrincipalUUID, request.Capability, request.TargetID)
|
||||
if groupErr != nil {
|
||||
return groupErr
|
||||
}
|
||||
@@ -550,6 +491,6 @@ func authorizeGrantFrom(q authQueryer, request AuthorizationRequest, exactObject
|
||||
|
||||
func (s *Store) PurgeExpiredAuthArtifacts() error {
|
||||
now := time.Now().UnixMilli()
|
||||
_, err := s.DB.Exec(`DELETE FROM auth_challenges WHERE expires_at_ms<? OR consumed_at_ms>0`, now)
|
||||
return err
|
||||
_, err := s.DB.Exec(`DELETE FROM auth_challenges WHERE expires_at_ms<`+s.sqlParam(1)+` OR consumed_at_ms>0`, now)
|
||||
return s.sqlError("expired auth artifacts", err)
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Account identity, credential, membership and role bindings are committed
|
||||
// together. Password hashing intentionally happens before acquiring DB locks.
|
||||
func (s *Store) createPasswordAccountContext(ctx context.Context, domain, username, password, displayName, email string, bootstrap bool) (*Principal, error) {
|
||||
username = strings.ToLower(strings.TrimSpace(username))
|
||||
if !authUsernamePattern.MatchString(username) {
|
||||
return nil, errors.New("username must be 2-64 lowercase letters, digits, underscores, or hyphens")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
encoded, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p := &Principal{UUID: uuid.NewString(), DisplayName: strings.TrimSpace(displayName), Email: strings.TrimSpace(email), State: "active"}
|
||||
if p.DisplayName == "" {
|
||||
p.DisplayName = username
|
||||
}
|
||||
err = s.groupWrite(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
||||
if bootstrap {
|
||||
var count int
|
||||
if err := tx.QueryRowContext(ctx, `SELECT count(*) FROM principals`).Scan(&count); err != nil {
|
||||
return err
|
||||
}
|
||||
if count != 0 {
|
||||
return namespaceValidationError("owner already exists")
|
||||
}
|
||||
}
|
||||
// PostgreSQL cannot bootstrap into a nonexistent or disabled namespace.
|
||||
// SQLite retains its historical authentication-only fixture support.
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
var present bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domains WHERE uuid=$1 AND enabled=1 AND placement='local')`, domain).Scan(&present); err != nil {
|
||||
return err
|
||||
}
|
||||
if !present {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
membership, role := uuid.NewString(), ""
|
||||
roleName, description := "member", "Domain member"
|
||||
if bootstrap {
|
||||
roleName, description = "owner", "Domain owner"
|
||||
}
|
||||
err := tx.QueryRowContext(ctx, `SELECT uuid FROM auth_roles WHERE domain_uuid=`+s.sqlParam(1)+` AND name=`+s.sqlParam(2), domain, roleName).Scan(&role)
|
||||
if bootstrap && err == nil {
|
||||
return namespaceValidationError("owner role already exists")
|
||||
}
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
role = uuid.NewString()
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO auth_roles(uuid,domain_uuid,name,description,created_at_ms) VALUES(`+s.sqlValues(5)+`)`, role, domain, roleName, description, now); err != nil {
|
||||
return err
|
||||
}
|
||||
capabilities := []string{"read"}
|
||||
if bootstrap {
|
||||
capabilities = []string{"read", "write", "rename", "delete", "link", "manage", "delegate"}
|
||||
}
|
||||
for _, capability := range capabilities {
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(`+s.sqlValues(6)+`)`, uuid.NewString(), role, capability, "domain", domain, now); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
queries := []struct {
|
||||
q string
|
||||
args []any
|
||||
}{
|
||||
{`INSERT INTO principals(uuid,display_name,email,state,created_at_ms,updated_at_ms) VALUES(` + s.sqlValues(6) + `)`, []any{p.UUID, p.DisplayName, p.Email, p.State, now, now}},
|
||||
{`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,subject,created_at_ms) VALUES(` + s.sqlValues(5) + `)`, []any{uuid.NewString(), p.UUID, "password", username, now}},
|
||||
{`INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,created_at_ms,updated_at_ms) VALUES(` + s.sqlValues(7) + `)`, []any{uuid.NewString(), p.UUID, "password", username, encoded, now, now}},
|
||||
{`INSERT INTO domain_memberships(uuid,domain_uuid,principal_uuid,state,created_at_ms) VALUES(` + s.sqlValues(5) + `)`, []any{membership, domain, p.UUID, "active", now}},
|
||||
{`INSERT INTO auth_role_bindings(membership_uuid,role_uuid,created_at_ms) VALUES(` + s.sqlValues(3) + `)`, []any{membership, role, now}},
|
||||
}
|
||||
for _, q := range queries {
|
||||
if _, err := tx.ExecContext(ctx, q.q, q.args...); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if bootstrap {
|
||||
if s.sqlBackend == storeSQLite {
|
||||
var present bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE name='domains' AND type='table')`).Scan(&present); err != nil {
|
||||
return err
|
||||
}
|
||||
if !present {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return s.ensureBuiltinAdminGroupsContext(ctx, tx)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (s *Store) BootstrapOwnerContext(ctx context.Context, domain, username, password, displayName, email string) (*Principal, error) {
|
||||
return s.createPasswordAccountContext(ctx, domain, username, password, displayName, email, true)
|
||||
}
|
||||
func (s *Store) CreatePasswordAccountContext(ctx context.Context, domain, username, password, displayName, email string) (*Principal, error) {
|
||||
return s.createPasswordAccountContext(ctx, domain, username, password, displayName, email, false)
|
||||
}
|
||||
@@ -0,0 +1,535 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAccountBackendCLIHandoff(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
p, err := s.BootstrapOwner(d, "owner", "synthetic password for backend tests", "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
browser, err := s.CreateBrowserAuthSession(d, p.UUID, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
verifier := strings.Repeat("a", 43)
|
||||
id, device, code, err := s.StartCLIAuthForLogin(ctx, d, cliTestOrigin, hashAuthToken(verifier), true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request, err := s.CLIAuthRequest(ctx, d, cliTestOrigin, id)
|
||||
if err != nil || !request.Remember {
|
||||
t.Fatal("trusted choice lost", err)
|
||||
}
|
||||
if _, _, _, err = s.ExchangeCLIAuth(ctx, d, cliTestOrigin, device, verifier); !errors.Is(err, ErrCLIPending) {
|
||||
t.Fatal("pending state", err)
|
||||
}
|
||||
if _, _, _, err = s.ExchangeCLIAuth(ctx, d, cliTestOrigin, device, verifier); !errors.Is(err, ErrCLISlowDown) {
|
||||
t.Fatal("poll limit", err)
|
||||
}
|
||||
if err = s.BeginCLIAuth(ctx, d, cliTestOrigin, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.BindCLIAuth(ctx, d, cliTestOrigin, id, browser); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.DecideCLIAuth(ctx, d, cliTestOrigin, id, browser, code, "", true); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.DB.Exec(`UPDATE auth_cli_requests SET next_poll_ms=0`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Failed issuance must not consume approval.
|
||||
if _, err = s.DB.Exec(`ALTER TABLE auth_sessions RENAME COLUMN mfa_level TO missing_mfa`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if token, _, _, e := s.ExchangeCLIAuth(ctx, d, cliTestOrigin, device, verifier); e == nil || token != "" {
|
||||
t.Fatal("partial session returned", e)
|
||||
}
|
||||
request, err = s.CLIAuthRequest(ctx, d, cliTestOrigin, id)
|
||||
if err != nil || request.State != "approved" {
|
||||
t.Fatal("grant consumed on failed issuance", err)
|
||||
}
|
||||
if _, err = s.DB.Exec(`ALTER TABLE auth_sessions RENAME COLUMN missing_mfa TO mfa_level`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
type result struct {
|
||||
token, name string
|
||||
err error
|
||||
}
|
||||
results := make(chan result, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
go func() {
|
||||
token, name, _, e := s.ExchangeCLIAuth(ctx, d, cliTestOrigin, device, verifier)
|
||||
results <- result{token, name, e}
|
||||
}()
|
||||
}
|
||||
success := 0
|
||||
for i := 0; i < 2; i++ {
|
||||
r := <-results
|
||||
if r.err != nil {
|
||||
if !errors.Is(r.err, ErrCLIExpired) {
|
||||
t.Fatal(r.err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
success++
|
||||
if r.name != "owner" || !strings.HasPrefix(r.token, "pw_api_") {
|
||||
t.Fatal("invalid exchange response")
|
||||
}
|
||||
if principal, e := s.PrincipalForSession(d, r.token); e != nil || principal.UUID != p.UUID {
|
||||
t.Fatal("CLI session invalid", e)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatal("approval consumed more than once", success)
|
||||
}
|
||||
// Include concurrent admissions at the boundary, not only sequential caps.
|
||||
for i := 0; i < 62; i++ {
|
||||
if _, _, _, e := s.StartCLIAuth(ctx, d, cliTestOrigin, hashAuthToken(verifier)); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
limits := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
go func() { _, _, _, e := s.StartCLIAuth(ctx, d, cliTestOrigin, hashAuthToken(verifier)); limits <- e }()
|
||||
}
|
||||
success = 0
|
||||
for i := 0; i < 2; i++ {
|
||||
if e := <-limits; e == nil {
|
||||
success++
|
||||
} else if !errors.Is(e, ErrCLILimit) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatal("concurrent admission bypassed limit", success)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendExternalIdentity(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
s.authKey = make([]byte, 32)
|
||||
config := validLDAPProviderConfig()
|
||||
if err := s.SetAuthProviderConfig(d, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider, err := s.AuthProviderConfig(d, "ldap")
|
||||
if err != nil || !provider.Enabled || provider.LDAPBindPassword != config.LDAPBindPassword {
|
||||
t.Fatal("provider configuration mismatch", err)
|
||||
}
|
||||
config.LDAPBindPassword = ""
|
||||
config.LDAPSearchFilter = "(mail={{username}})"
|
||||
if err = s.SetAuthProviderConfig(d, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kept, err := s.AuthProviderConfig(d, "ldap")
|
||||
if err != nil || kept.UUID != provider.UUID || kept.LDAPBindPassword != provider.LDAPBindPassword {
|
||||
t.Fatal("provider lost secret or identity", err)
|
||||
}
|
||||
p, err := s.ProvisionExternalAccount(d, "directory", "Directory User", "mail@example.test", "ldap", provider.UUID, "", "immutable-id", map[string]string{"display": "Directory User"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.AuthenticatePassword("directory", "anything with twelve characters"); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("external user has password", err)
|
||||
}
|
||||
for _, lookup := range []func() (*Principal, error){
|
||||
func() (*Principal, error) { return s.PrincipalByExternalUsername(d, "DIRECTORY") },
|
||||
func() (*Principal, error) { return s.PrincipalByVerifiedEmail(d, "MAIL@example.test") },
|
||||
func() (*Principal, error) { return s.PrincipalByDisplayName(d, "Directory User") },
|
||||
func() (*Principal, error) {
|
||||
return s.PrincipalByExternalIdentity(d, "ldap", provider.UUID, "", "immutable-id")
|
||||
},
|
||||
} {
|
||||
found, err := lookup()
|
||||
if err != nil || found.UUID != p.UUID {
|
||||
t.Fatal("external identity lookup", err)
|
||||
}
|
||||
}
|
||||
if err = s.BindExternalIdentityIfProviderUnbound(d, p.UUID, "ldap", provider.UUID, "", "replacement-id", nil); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("provider identity replaced", err)
|
||||
}
|
||||
second, err := s.CreatePasswordAccount(d, "second", "synthetic password for backend tests", "Directory User", "mail@example.test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.PrincipalByVerifiedEmail(d, "mail@example.test"); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("ambiguous email accepted", err)
|
||||
}
|
||||
if _, err = s.PrincipalByDisplayName(d, "Directory User"); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("ambiguous display accepted", err)
|
||||
}
|
||||
if err = s.BindExternalIdentity(second.UUID, "ldap", provider.UUID, "", "immutable-id", nil); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("identity owner replaced", err)
|
||||
}
|
||||
if _, err = s.ProvisionExternalAccount(d, "rollback-ext", "", "", "ldap", provider.UUID, "", "immutable-id", nil); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("duplicate identity provisioned", err)
|
||||
}
|
||||
if _, err = s.PrincipalByUsername(d, "rollback-ext"); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("partial external account remains", err)
|
||||
}
|
||||
if err = s.BindExternalIdentityIfProviderUnbound(d, second.UUID, "oidc", "oidc-provider", "https://issuer.example.test", "unique", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
challenge, err := s.CreateAuthChallenge(d, p.UUID, "magic-link", map[string]string{"return": "/docs"}, time.Minute)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.ConsumeAuthChallenge("outside", "magic-link", challenge, nil); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("cross-domain challenge accepted", err)
|
||||
}
|
||||
results := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
go func() {
|
||||
var payload map[string]string
|
||||
id, e := s.ConsumeAuthChallenge(d, "magic-link", challenge, &payload)
|
||||
if e == nil && (id != p.UUID || payload["return"] != "/docs") {
|
||||
e = fmt.Errorf("challenge payload mismatch")
|
||||
}
|
||||
results <- e
|
||||
}()
|
||||
}
|
||||
success := 0
|
||||
for i := 0; i < 2; i++ {
|
||||
if e := <-results; e == nil {
|
||||
success++
|
||||
} else if !errors.Is(e, ErrAccessDenied) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatal("challenge accepted more than once", success)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendTOTP(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
s.authKey = make([]byte, 32) // Synthetic credential encryption key.
|
||||
p, err := s.BootstrapOwner(d, "owner", "synthetic password for backend tests", "", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
secret, err := s.BeginTOTPEnrollment(p.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.HasTOTP(p.UUID) || s.PendingTOTPSecret(p.UUID) != secret {
|
||||
t.Fatal("pending enrollment mismatch")
|
||||
}
|
||||
code, err := totpCode(secret, time.Now().Unix()/30)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.ConfirmTOTPEnrollment(p.UUID, code); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !s.HasTOTP(p.UUID) || s.PendingTOTPSecret(p.UUID) != "" {
|
||||
t.Fatal("confirmed enrollment mismatch")
|
||||
}
|
||||
if err = s.VerifyTOTP(p.UUID, code); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("enrollment code replay", err)
|
||||
}
|
||||
// Reset only synthetic counters to exercise single-use concurrent login
|
||||
// without wall-clock sleeps or a test-dependent production clock.
|
||||
if _, err = s.DB.Exec(`UPDATE auth_credentials SET parameters_json='{}' WHERE kind='totp'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
code, _ = totpCode(secret, time.Now().Unix()/30)
|
||||
challenge, err := s.CreateTOTPLoginChallengeForLogin(d, p.UUID, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err = s.CompleteTOTPLoginChallengeForLogin("outside", challenge, code); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("cross-domain TOTP challenge", err)
|
||||
}
|
||||
results := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
go func() {
|
||||
id, remember, e := s.CompleteTOTPLoginChallengeForLogin(d, challenge, code)
|
||||
if e == nil && (id != p.UUID || !remember) {
|
||||
e = fmt.Errorf("challenge result mismatch")
|
||||
}
|
||||
results <- e
|
||||
}()
|
||||
}
|
||||
success := 0
|
||||
for i := 0; i < 2; i++ {
|
||||
if e := <-results; e == nil {
|
||||
success++
|
||||
} else if !errors.Is(e, ErrAccessDenied) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatal("concurrent code consumers", success)
|
||||
}
|
||||
challenge, err = s.CreateTOTPLoginChallenge(d, p.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 6; i++ {
|
||||
if _, e := s.CompleteTOTPLoginChallenge(d, challenge, "invalid"); !errors.Is(e, ErrAccessDenied) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
}
|
||||
if err = s.PurgeExpiredAuthArtifacts(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var remaining int
|
||||
if err = s.DB.QueryRow(`SELECT count(*) FROM auth_challenges`).Scan(&remaining); err != nil || remaining != 0 {
|
||||
t.Fatal("consumed challenge cleanup", remaining, err)
|
||||
}
|
||||
if err = s.DisableTOTP(p.UUID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.HasTOTP(p.UUID) {
|
||||
t.Fatal("TOTP still enabled")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendProfileAndSessionManagement(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
p, err := s.BootstrapOwner(d, "owner", "synthetic password for backend tests", "Owner", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.UpdatePrincipalProfile(p.UUID, " 日本語の名前 ", " user@example.test "); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
data := []byte{0, 255, 1, 0, 128}
|
||||
if err = s.SetPrincipalAvatar(p.UUID, "image/png", data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kind, got, err := s.PrincipalAvatar(p.UUID)
|
||||
if err != nil || kind != "image/png" || !bytes.Equal(got, data) {
|
||||
t.Fatal("avatar round trip", kind, got, err)
|
||||
}
|
||||
found, err := s.PrincipalByUsername(d, " OWNER ")
|
||||
if err != nil || found.DisplayName != "日本語の名前" || found.Email != "user@example.test" || !found.HasAvatar {
|
||||
t.Fatal(found, err)
|
||||
}
|
||||
if _, err = s.PrincipalByUsername("other-domain", "owner"); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("cross-domain profile lookup", err)
|
||||
}
|
||||
if err = s.SetPrincipalAvatar(p.UUID, "", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found, err = s.PrincipalByUsername(d, "owner")
|
||||
if err != nil || found.HasAvatar {
|
||||
t.Fatal("avatar removal", err)
|
||||
}
|
||||
principals, err := s.ListDomainPrincipals(d)
|
||||
if err != nil || len(principals) != 1 || principals[0].UUID != p.UUID {
|
||||
t.Fatal(principals, err)
|
||||
}
|
||||
principals, err = s.ListDomainPrincipals("other-domain")
|
||||
if err != nil || len(principals) != 0 {
|
||||
t.Fatal("cross-domain principal list", err)
|
||||
}
|
||||
token, err := s.CreateBrowserAuthSession(d, p.UUID, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sessions, err := s.ListAuthSessions(d)
|
||||
if err != nil || len(sessions) != 1 || sessions[0].MFALevel != 2 {
|
||||
t.Fatal(sessions, err)
|
||||
}
|
||||
id := sessions[0].UUID
|
||||
if err = s.RevokeAuthSessionByID("other-domain", id); !errors.Is(err, sql.ErrNoRows) {
|
||||
t.Fatal("cross-domain revocation", err)
|
||||
}
|
||||
if _, err = s.PrincipalForSession(d, token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = s.RevokeAuthSessionByID(d, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.PrincipalForSession(d, token); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("revoked session accepted", err)
|
||||
}
|
||||
sessions, err = s.ListAuthSessions(d)
|
||||
if err != nil || len(sessions) != 0 {
|
||||
t.Fatal("revoked session listed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendBootstrapProjectionFailureIsAtomic(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
if _, err := s.DB.Exec(`ALTER TABLE campus_admin_grants RENAME COLUMN source TO missing_source`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.BootstrapOwner(d, "owner", "synthetic password for backend tests", "", ""); err == nil {
|
||||
t.Fatal("bootstrap succeeded with broken projection")
|
||||
}
|
||||
for _, table := range []string{"principals", "principal_identities", "auth_credentials", "auth_roles", "auth_capability_grants", "auth_role_bindings", "domain_memberships"} {
|
||||
var count int
|
||||
if err := s.DB.QueryRow(`SELECT count(*) FROM ` + table).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatalf("%s has partial bootstrap data: %d %v", table, count, err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func accountBackendStore(t *testing.T, b storeSQLBackend) (*Store, string) {
|
||||
t.Helper()
|
||||
s := testNamespaceStore(t, b)
|
||||
if b == storePostgreSQL {
|
||||
if err := s.ensurePostgreSQLAuthFoundation(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
d, err := s.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return s, d.UUID
|
||||
}
|
||||
|
||||
func TestAccountBackendLoginAndSession(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
const password = "synthetic password for backend tests"
|
||||
p, err := s.BootstrapOwner(d, " Owner ", password, "所有者", "owner@example.test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !s.IsBuiltinGroupAdmin(d, p.UUID, "domain_admin") || !s.IsBuiltinGroupAdmin(d, p.UUID, "campus_admin") {
|
||||
t.Fatal("bootstrap did not publish admin groups")
|
||||
}
|
||||
if _, err := s.BootstrapOwner(d, "second", password, "", ""); err == nil {
|
||||
t.Fatal("second bootstrap accepted")
|
||||
}
|
||||
if _, err := s.AuthenticatePassword("owner", "incorrect password"); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("incorrect password accepted", err)
|
||||
}
|
||||
logged, err := s.AuthenticatePassword("OWNER", password)
|
||||
if err != nil || logged.UUID != p.UUID {
|
||||
t.Fatal("login mismatch", err)
|
||||
}
|
||||
member, err := s.CreatePasswordAccount(d, "member", password, "Member", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.authorizeGrant(AuthorizationRequest{DomainUUID: d, PrincipalUUID: member.UUID, Capability: "read", TargetType: "domain", TargetID: d}, false); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.authorizeGrant(AuthorizationRequest{DomainUUID: d, PrincipalUUID: member.UUID, Capability: "write", TargetType: "domain", TargetID: d}, false); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("member can write", err)
|
||||
}
|
||||
token, err := s.CreateBrowserAuthSession(d, p.UUID, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.PrincipalForSession(d, token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, invalid := range []string{strings.TrimPrefix(token, "pw_browser_"), strings.Replace(token, "pw_browser_", "pw_api_", 1)} {
|
||||
if _, err := s.PrincipalForSession(d, invalid); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("token purpose changed", err)
|
||||
}
|
||||
}
|
||||
if _, err := s.PrincipalForSession("outside", token); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("cross-domain session accepted", err)
|
||||
}
|
||||
if err := s.SetPrincipalState(d, p.UUID, "suspended"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.CreateAuthSession(d, p.UUID); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("suspended principal obtained session", err)
|
||||
}
|
||||
if err := s.SetPrincipalState(d, p.UUID, "active"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.PrincipalForSession(d, token); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("session resurrected", err)
|
||||
}
|
||||
token, err = s.CreateAuthSession(d, p.UUID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.RevokeAuthSession(token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.PrincipalForSession(d, token); !errors.Is(err, ErrAccessDenied) {
|
||||
t.Fatal("revocation ignored", err)
|
||||
}
|
||||
if _, err := s.DB.Exec(`ALTER TABLE auth_role_bindings RENAME COLUMN role_uuid TO missing_role`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.CreatePasswordAccount(d, "rollback", password, "", ""); err == nil {
|
||||
t.Fatal("partial account accepted")
|
||||
}
|
||||
var count int
|
||||
if err := s.DB.QueryRow(`SELECT count(*) FROM principal_identities WHERE subject='rollback'`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("partial identity remains", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountBackendConcurrentBootstrap(t *testing.T) {
|
||||
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
||||
s, d := accountBackendStore(t, b)
|
||||
var wg sync.WaitGroup
|
||||
results := make(chan error, 2)
|
||||
for _, name := range []string{"owner-one", "owner-two"} {
|
||||
wg.Add(1)
|
||||
go func(name string) {
|
||||
defer wg.Done()
|
||||
_, err := s.BootstrapOwner(d, name, "synthetic password for bootstrap", "", "")
|
||||
results <- err
|
||||
}(name)
|
||||
}
|
||||
wg.Wait()
|
||||
close(results)
|
||||
success := 0
|
||||
for err := range results {
|
||||
if err == nil {
|
||||
success++
|
||||
} else if !strings.Contains(err.Error(), "owner already exists") {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if success != 1 {
|
||||
t.Fatalf("bootstrap successes=%d", success)
|
||||
}
|
||||
count, err := s.PrincipalCount()
|
||||
if err != nil || count != 1 {
|
||||
t.Fatal("multiple bootstrap principals", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAdminAuthorizationBackendContract(t *testing.T) {
|
||||
for _, backend := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
||||
t.Run(fmt.Sprint(backend), func(t *testing.T) {
|
||||
s := testNamespaceStore(t, backend)
|
||||
ctx := context.Background()
|
||||
if backend == storePostgreSQL {
|
||||
if err := s.ensurePostgreSQLAuthFoundation(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
campus, err := s.GetDefaultCampus()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
domain, err := s.GetDefaultDomain()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := seedNamespaceDomain(t, s, campus.UUID, "other-admin-domain", "other-admin-domain")
|
||||
exec := func(query string, args ...any) {
|
||||
t.Helper()
|
||||
if _, err := s.DB.ExecContext(ctx, query, args...); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
exec(`INSERT INTO principals(uuid,display_name,created_at_ms,updated_at_ms) VALUES('admin-test-principal','管理者',1,1)`)
|
||||
exec(`INSERT INTO domain_memberships(uuid,domain_uuid,principal_uuid,created_at_ms) VALUES('admin-test-membership',`+s.sqlParam(1)+`,'admin-test-principal',1)`, domain.UUID)
|
||||
exec(`INSERT INTO groups(uuid,domain_uuid,name,builtin_key,created_at_ms,updated_at_ms) VALUES('admin-test-group',`+s.sqlParam(1)+`,'admin-test-group','campus_admin',1,1)`, domain.UUID)
|
||||
exec(`INSERT INTO group_members(group_uuid,membership_uuid,member_role,created_at_ms) VALUES('admin-test-group','admin-test-membership','owner',1)`)
|
||||
check := func(want bool) {
|
||||
t.Helper()
|
||||
got, err := s.IsBuiltinGroupAdminContext(ctx, domain.UUID, "admin-test-principal", "campus_admin")
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("group allowed=%v want=%v error=%v", got, want, err)
|
||||
}
|
||||
got, err = s.IsCampusAdminContext(ctx, campus.UUID, "admin-test-principal")
|
||||
if err != nil || got != want {
|
||||
t.Fatalf("campus allowed=%v want=%v error=%v", got, want, err)
|
||||
}
|
||||
}
|
||||
check(true)
|
||||
for _, field := range []string{"principals", "domain_memberships", "groups"} {
|
||||
exec(`UPDATE ` + field + ` SET state='suspended'`)
|
||||
check(false)
|
||||
exec(`UPDATE ` + field + ` SET state='active'`)
|
||||
}
|
||||
exec(`UPDATE domains SET enabled=0 WHERE uuid=`+s.sqlParam(1), domain.UUID)
|
||||
check(false)
|
||||
exec(`UPDATE domains SET enabled=1 WHERE uuid=`+s.sqlParam(1), domain.UUID)
|
||||
for _, role := range []string{"member", "manager", "owner"} {
|
||||
exec(`UPDATE group_members SET member_role=`+s.sqlParam(1)+` WHERE group_uuid='admin-test-group'`, role)
|
||||
check(role != "member")
|
||||
}
|
||||
// A forged cross-domain group membership must not confer authority.
|
||||
exec(`UPDATE groups SET domain_uuid=`+s.sqlParam(1)+` WHERE uuid='admin-test-group'`, other.UUID)
|
||||
check(false)
|
||||
exec(`UPDATE groups SET domain_uuid=`+s.sqlParam(1)+` WHERE uuid='admin-test-group'`, domain.UUID)
|
||||
check(true)
|
||||
for _, key := range []string{"domain_admin", "made-up", "campus_admin' OR 1=1--"} {
|
||||
if s.IsBuiltinGroupAdmin(domain.UUID, "admin-test-principal", key) {
|
||||
t.Fatal("unexpected administrative grant")
|
||||
}
|
||||
}
|
||||
if s.IsBuiltinGroupAdmin(other.UUID, "admin-test-principal", "campus_admin") || s.IsCampusAdmin("other-campus", "admin-test-principal") {
|
||||
t.Fatal("cross namespace grant")
|
||||
}
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if allowed, err := s.IsCampusAdminContext(cancelled, campus.UUID, "admin-test-principal"); allowed || !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("cancellation: allowed=%v error=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := s.IsBuiltinGroupAdminContext(cancelled, domain.UUID, "admin-test-principal", "campus_admin"); allowed || !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("cancellation: allowed=%v error=%v", allowed, err)
|
||||
}
|
||||
exec(`ALTER TABLE group_members RENAME COLUMN member_role TO unavailable_role`)
|
||||
if s.IsCampusAdmin(campus.UUID, "admin-test-principal") || s.IsBuiltinGroupAdmin(domain.UUID, "admin-test-principal", "campus_admin") {
|
||||
t.Fatal("database error must fail closed")
|
||||
}
|
||||
if allowed, err := s.IsCampusAdminContext(ctx, campus.UUID, "admin-test-principal"); allowed || err == nil {
|
||||
t.Fatal("context API must report database error")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostgreSQLAuthFoundationMigration(t *testing.T) {
|
||||
s := testNamespaceStore(t, storePostgreSQL)
|
||||
ctx := context.Background()
|
||||
if err := s.EnsureAuthSchema(); err != nil {
|
||||
t.Fatalf("native authentication schema: %v", err)
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
errorsCh := make(chan error, 4)
|
||||
for i := 0; i < 4; i++ {
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); errorsCh <- s.ensurePostgreSQLAuthFoundation(ctx) }()
|
||||
}
|
||||
wg.Wait()
|
||||
close(errorsCh)
|
||||
for err := range errorsCh {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := s.DB.Exec(`INSERT INTO principals(uuid,created_at_ms,updated_at_ms) VALUES('retained',1,1)`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var avatar []byte
|
||||
if err := s.DB.QueryRow(`SELECT avatar FROM principals WHERE uuid='retained'`).Scan(&avatar); err != nil || len(avatar) != 0 {
|
||||
t.Fatalf("avatar default: %v", err)
|
||||
}
|
||||
if _, err := s.DB.Exec(`ALTER TABLE principals RENAME COLUMN avatar TO missing_avatar`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.ensurePostgreSQLAuthFoundation(ctx); err == nil {
|
||||
t.Fatal("schema drift accepted")
|
||||
}
|
||||
var count int
|
||||
if err := s.DB.QueryRow(`SELECT count(*) FROM principals WHERE uuid='retained'`).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("existing identity lost")
|
||||
}
|
||||
}
|
||||
+60
-34
@@ -44,8 +44,8 @@ type CLIAuthRequest struct {
|
||||
Remember bool
|
||||
}
|
||||
|
||||
// Admission is one write statement; concurrent unauthenticated requests cannot
|
||||
// race the caps. Requests are retained for ten minutes for issuance accounting.
|
||||
// Admission is serialized on both backends so concurrent unauthenticated
|
||||
// requests cannot race the caps. Retain requests for issuance accounting.
|
||||
func (s *Store) StartCLIAuth(ctx context.Context, domain, origin, challenge string) (id, device, code string, err error) {
|
||||
return s.StartCLIAuthForLogin(ctx, domain, origin, challenge, false)
|
||||
}
|
||||
@@ -68,40 +68,48 @@ func (s *Store) StartCLIAuthForLogin(ctx context.Context, domain, origin, challe
|
||||
}
|
||||
code = strings.ToUpper(hex.EncodeToString(raw[:]))
|
||||
now := time.Now().UnixMilli()
|
||||
_, err = s.DB.ExecContext(ctx, `DELETE FROM auth_cli_requests WHERE expires_ms<?`, now-5*60*1000)
|
||||
if err != nil {
|
||||
return
|
||||
rememberValue := 0
|
||||
if remember {
|
||||
rememberValue = 1
|
||||
}
|
||||
var result sql.Result
|
||||
result, err = s.DB.ExecContext(ctx, `INSERT INTO auth_cli_requests(id,domain_uuid,origin,device_hash,challenge,code_hash,created_ms,expires_ms,remember)
|
||||
SELECT ?,?,?,?,?,?,?,?,? WHERE (SELECT count(*) FROM auth_cli_requests)<4096
|
||||
AND (SELECT count(*) FROM auth_cli_requests WHERE domain_uuid=? AND created_ms>?)<64`,
|
||||
id, domain, origin, hashAuthToken(device), challenge, hashAuthToken(code), now, now+300000, remember, domain, now-300000)
|
||||
if err == nil {
|
||||
if n, e := result.RowsAffected(); e != nil {
|
||||
err = e
|
||||
} else if n != 1 {
|
||||
err = ErrCLILimit
|
||||
err = s.groupWrite(ctx, func(ctx context.Context, tx *sql.Tx) error {
|
||||
if _, err := tx.ExecContext(ctx, `DELETE FROM auth_cli_requests WHERE expires_ms<`+s.sqlParam(1), now-5*60*1000); err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, `INSERT INTO auth_cli_requests(id,domain_uuid,origin,device_hash,challenge,code_hash,created_ms,expires_ms,remember)
|
||||
SELECT `+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`,`+s.sqlParam(9)+` WHERE (SELECT count(*) FROM auth_cli_requests)<4096
|
||||
AND (SELECT count(*) FROM auth_cli_requests WHERE domain_uuid=`+s.sqlParam(10)+` AND created_ms>`+s.sqlParam(11)+`)<64`,
|
||||
id, domain, origin, hashAuthToken(device), challenge, hashAuthToken(code), now, now+300000, rememberValue, domain, now-300000)
|
||||
if err == nil {
|
||||
if n, e := result.RowsAffected(); e != nil {
|
||||
err = e
|
||||
} else if n != 1 {
|
||||
err = ErrCLILimit
|
||||
}
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
id, device, code = "", "", ""
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Store) CLIAuthRequest(ctx context.Context, domain, origin, id string) (*CLIAuthRequest, error) {
|
||||
var grant CLIAuthRequest
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT id,origin,state,browser_hash,principal_uuid,expires_ms,remember FROM auth_cli_requests WHERE id=? AND domain_uuid=? AND origin=? AND expires_ms>?`, id, domain, origin, time.Now().UnixMilli()).Scan(&grant.ID, &grant.Origin, &grant.State, &grant.BrowserHash, &grant.PrincipalUUID, &grant.ExpiresMS, &grant.Remember)
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT id,origin,state,browser_hash,principal_uuid,expires_ms,remember FROM auth_cli_requests WHERE id=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND origin=`+s.sqlParam(3)+` AND expires_ms>`+s.sqlParam(4)+``, id, domain, origin, time.Now().UnixMilli()).Scan(&grant.ID, &grant.Origin, &grant.State, &grant.BrowserHash, &grant.PrincipalUUID, &grant.ExpiresMS, &grant.Remember)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrCLIExpired
|
||||
}
|
||||
return &grant, err
|
||||
return &grant, s.sqlError("CLI auth lookup", err)
|
||||
}
|
||||
|
||||
func (s *Store) BeginCLIAuth(ctx context.Context, domain, origin, id string) error {
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
result, err := s.DB.ExecContext(ctx, `UPDATE auth_cli_requests SET oidc_attempts=oidc_attempts+1 WHERE id=? AND domain_uuid=? AND origin=? AND state='pending' AND browser_hash='' AND expires_ms>? AND oidc_attempts<3`, id, domain, origin, time.Now().UnixMilli())
|
||||
return cliChanged(result, err)
|
||||
result, err := s.DB.ExecContext(ctx, `UPDATE auth_cli_requests SET oidc_attempts=oidc_attempts+1 WHERE id=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND origin=`+s.sqlParam(3)+` AND state='pending' AND browser_hash='' AND expires_ms>`+s.sqlParam(4)+` AND oidc_attempts<3`, id, domain, origin, time.Now().UnixMilli())
|
||||
return s.sqlError("CLI auth begin", cliChanged(result, err))
|
||||
}
|
||||
|
||||
// Only the verified OIDC callback calls this method. The first successful
|
||||
@@ -111,10 +119,10 @@ func (s *Store) BindCLIAuth(ctx context.Context, domain, origin, id, browser str
|
||||
return ErrAccessDenied
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
result, err := s.DB.ExecContext(ctx, `UPDATE auth_cli_requests SET browser_hash=?,principal_uuid=(SELECT principal_uuid FROM auth_sessions WHERE token_hash=?)
|
||||
WHERE id=? AND domain_uuid=? AND origin=? AND state='pending' AND browser_hash='' AND expires_ms>?
|
||||
AND EXISTS(SELECT 1 FROM auth_sessions WHERE token_hash=? AND domain_uuid=? AND revoked_at_ms=0 AND expires_at_ms>?)`, hashAuthToken(browser), hashAuthToken(browser), id, domain, origin, now, hashAuthToken(browser), domain, now)
|
||||
return cliChanged(result, err)
|
||||
result, err := s.DB.ExecContext(ctx, `UPDATE auth_cli_requests SET browser_hash=`+s.sqlParam(1)+`,principal_uuid=(SELECT principal_uuid FROM auth_sessions WHERE token_hash=`+s.sqlParam(2)+`)
|
||||
WHERE id=`+s.sqlParam(3)+` AND domain_uuid=`+s.sqlParam(4)+` AND origin=`+s.sqlParam(5)+` AND state='pending' AND browser_hash='' AND expires_ms>`+s.sqlParam(6)+`
|
||||
AND EXISTS(SELECT 1 FROM auth_sessions WHERE token_hash=`+s.sqlParam(7)+` AND domain_uuid=`+s.sqlParam(8)+` AND revoked_at_ms=0 AND expires_at_ms>`+s.sqlParam(9)+`)`, hashAuthToken(browser), hashAuthToken(browser), id, domain, origin, now, hashAuthToken(browser), domain, now)
|
||||
return s.sqlError("CLI auth bind", cliChanged(result, err))
|
||||
}
|
||||
|
||||
func cliChanged(result sql.Result, err error) error {
|
||||
@@ -141,7 +149,7 @@ func (s *Store) DecideCLIAuth(ctx context.Context, domain, origin, id, browser,
|
||||
now := time.Now().UnixMilli()
|
||||
var expectedCode string
|
||||
err := s.DB.QueryRowContext(ctx, `UPDATE auth_cli_requests SET attempts=attempts+1
|
||||
WHERE id=? AND domain_uuid=? AND origin=? AND state='pending' AND browser_hash=? AND attempts<5 AND expires_ms>?
|
||||
WHERE id=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND origin=`+s.sqlParam(3)+` AND state='pending' AND browser_hash=`+s.sqlParam(4)+` AND attempts<5 AND expires_ms>`+s.sqlParam(5)+`
|
||||
RETURNING code_hash`, id, domain, origin, hashAuthToken(browser), now).Scan(&expectedCode)
|
||||
if err != nil {
|
||||
return ErrAccessDenied
|
||||
@@ -156,11 +164,11 @@ func (s *Store) DecideCLIAuth(ctx context.Context, domain, origin, id, browser,
|
||||
if approve {
|
||||
state = "approved"
|
||||
}
|
||||
result, err := s.DB.ExecContext(ctx, `UPDATE auth_cli_requests SET state=?,mfa_level=? WHERE id=? AND domain_uuid=? AND state='pending' AND browser_hash=? AND expires_ms>?
|
||||
result, err := s.DB.ExecContext(ctx, `UPDATE auth_cli_requests SET state=`+s.sqlParam(1)+`,mfa_level=`+s.sqlParam(2)+` WHERE id=`+s.sqlParam(3)+` AND domain_uuid=`+s.sqlParam(4)+` AND state='pending' AND browser_hash=`+s.sqlParam(5)+` AND expires_ms>`+s.sqlParam(6)+`
|
||||
AND EXISTS(SELECT 1 FROM auth_sessions a JOIN principals p ON p.uuid=a.principal_uuid JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=a.domain_uuid
|
||||
WHERE a.token_hash=? AND a.domain_uuid=? AND a.revoked_at_ms=0 AND a.expires_at_ms>? AND p.state IN ('active','readonly') AND m.state='active')`, state, level, id, domain, hashAuthToken(browser), now, hashAuthToken(browser), domain, now)
|
||||
WHERE a.token_hash=`+s.sqlParam(7)+` AND a.domain_uuid=`+s.sqlParam(8)+` AND a.revoked_at_ms=0 AND a.expires_at_ms>`+s.sqlParam(9)+` AND p.state IN ('active','readonly') AND m.state='active')`, state, level, id, domain, hashAuthToken(browser), now, hashAuthToken(browser), domain, now)
|
||||
if err = cliChanged(result, err); err != nil {
|
||||
return err
|
||||
return s.sqlError("CLI auth decision", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -169,6 +177,14 @@ func (s *Store) DecideCLIAuth(ctx context.Context, domain, origin, id, browser,
|
||||
// Grant consumption and API-session creation commit together. No replay on a
|
||||
// lost HTTP response: a new login is required (no credential stored in grant).
|
||||
func (s *Store) ExchangeCLIAuth(ctx context.Context, domain, origin, device, verifier string) (token, username string, expires int64, err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
token, username, expires = "", "", 0
|
||||
err = s.sqlError("CLI auth exchange", err)
|
||||
}
|
||||
}()
|
||||
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||
defer cancel()
|
||||
if s.ReadOnly {
|
||||
err = ErrReadOnly
|
||||
return
|
||||
@@ -181,14 +197,20 @@ func (s *Store) ExchangeCLIAuth(ctx context.Context, domain, origin, device, ver
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
// Match account suspension and membership revocation lock ordering.
|
||||
if _, err = tx.ExecContext(ctx, `SELECT pg_advisory_xact_lock(1885431653, 1853058931)`); err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
var id, state, browser string
|
||||
var remember bool
|
||||
err = tx.QueryRowContext(ctx, `UPDATE auth_cli_requests SET next_poll_ms=?,state=CASE WHEN state='approved' THEN 'consumed' ELSE state END
|
||||
WHERE device_hash=? AND challenge=? AND domain_uuid=? AND origin=? AND expires_ms>? AND next_poll_ms<=? AND state IN ('pending','approved','denied')
|
||||
err = tx.QueryRowContext(ctx, `UPDATE auth_cli_requests SET next_poll_ms=`+s.sqlParam(1)+`,state=CASE WHEN state='approved' THEN 'consumed' ELSE state END
|
||||
WHERE device_hash=`+s.sqlParam(2)+` AND challenge=`+s.sqlParam(3)+` AND domain_uuid=`+s.sqlParam(4)+` AND origin=`+s.sqlParam(5)+` AND expires_ms>`+s.sqlParam(6)+` AND next_poll_ms<=`+s.sqlParam(7)+` AND state IN ('pending','approved','denied')
|
||||
RETURNING id,state,browser_hash,remember`, now+5000, hashAuthToken(device), hashAuthToken(verifier), domain, origin, now, now).Scan(&id, &state, &browser, &remember)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
var exists int
|
||||
e := tx.QueryRowContext(ctx, `SELECT count(*) FROM auth_cli_requests WHERE device_hash=? AND challenge=? AND domain_uuid=? AND origin=? AND expires_ms>? AND state IN ('pending','approved','denied')`, hashAuthToken(device), hashAuthToken(verifier), domain, origin, now).Scan(&exists)
|
||||
e := tx.QueryRowContext(ctx, `SELECT count(*) FROM auth_cli_requests WHERE device_hash=`+s.sqlParam(1)+` AND challenge=`+s.sqlParam(2)+` AND domain_uuid=`+s.sqlParam(3)+` AND origin=`+s.sqlParam(4)+` AND expires_ms>`+s.sqlParam(5)+` AND state IN ('pending','approved','denied')`, hashAuthToken(device), hashAuthToken(verifier), domain, origin, now).Scan(&exists)
|
||||
if e != nil {
|
||||
err = e
|
||||
} else if exists > 0 {
|
||||
@@ -221,12 +243,16 @@ func (s *Store) ExchangeCLIAuth(ctx context.Context, domain, origin, device, ver
|
||||
return
|
||||
}
|
||||
token = "pw_api_" + raw
|
||||
minimum, maximum := "min", "max"
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
minimum, maximum = "LEAST", "GREATEST"
|
||||
}
|
||||
// A disabled user, removed membership or revoked browser cannot delegate.
|
||||
err = tx.QueryRowContext(ctx, `INSERT INTO auth_sessions(uuid,token_hash,principal_uuid,domain_uuid,auth_time_ms,expires_at_ms,last_seen_at_ms,mfa_level)
|
||||
SELECT ?,?,p.uuid,a.domain_uuid,a.auth_time_ms,min(?,a.expires_at_ms),?,max(a.mfa_level,g.mfa_level)
|
||||
SELECT `+s.sqlParam(1)+`,`+s.sqlParam(2)+`,p.uuid,a.domain_uuid,a.auth_time_ms,`+minimum+`(`+s.sqlParam(3)+`,a.expires_at_ms),`+s.sqlParam(4)+`,`+maximum+`(a.mfa_level,g.mfa_level)
|
||||
FROM auth_cli_requests g JOIN auth_sessions a ON a.token_hash=g.browser_hash JOIN principals p ON p.uuid=a.principal_uuid JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=a.domain_uuid
|
||||
WHERE a.token_hash=? AND a.domain_uuid=? AND a.revoked_at_ms=0 AND a.expires_at_ms>? AND p.state IN ('active','readonly') AND m.state='active'
|
||||
AND g.id=?
|
||||
WHERE a.token_hash=`+s.sqlParam(5)+` AND a.domain_uuid=`+s.sqlParam(6)+` AND a.revoked_at_ms=0 AND a.expires_at_ms>`+s.sqlParam(7)+` AND p.state IN ('active','readonly') AND m.state='active'
|
||||
AND g.id=`+s.sqlParam(8)+`
|
||||
RETURNING principal_uuid,expires_at_ms`, uuid.NewString(), hashAuthToken(token), now+lifetime, now, browser, domain, now, id).Scan(&username, &expires)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
err = ErrAccessDenied
|
||||
@@ -236,7 +262,7 @@ func (s *Store) ExchangeCLIAuth(ctx context.Context, domain, origin, device, ver
|
||||
return
|
||||
}
|
||||
var name string
|
||||
err = tx.QueryRowContext(ctx, `SELECT COALESCE((SELECT subject FROM principal_identities WHERE principal_uuid=? AND provider_type='password' LIMIT 1),?)`, username, username).Scan(&name)
|
||||
err = tx.QueryRowContext(ctx, `SELECT COALESCE((SELECT subject FROM principal_identities WHERE principal_uuid=`+s.sqlParam(1)+` AND provider_type='password' ORDER BY subject,uuid LIMIT 1),`+s.sqlParam(2)+`)`, username, username).Scan(&name)
|
||||
if err != nil {
|
||||
token = ""
|
||||
return
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
@@ -134,25 +135,19 @@ func (s *Store) BindExternalIdentityIfProviderUnbound(domainUUID, principalUUID,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err = tx.Exec(`UPDATE principals SET updated_at_ms=updated_at_ms WHERE uuid=?`, principalUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
var eligible, alreadyBound bool
|
||||
if err = tx.QueryRow(`SELECT EXISTS(SELECT 1 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? AND m.state='active' WHERE p.uuid=? AND p.state IN ('active','readonly'))`, domainUUID, principalUUID).Scan(&eligible); err != nil || !eligible {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
if err = tx.QueryRow(`SELECT EXISTS(SELECT 1 FROM principal_identities WHERE principal_uuid=? AND provider_type=? AND provider_uuid=?)`, principalUUID, providerType, providerUUID).Scan(&alreadyBound); err != nil || alreadyBound {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
if _, err = tx.Exec(`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,provider_uuid,issuer,subject,attributes_json,created_at_ms) VALUES(?,?,?,?,?,?,?,?)`, uuid.NewString(), principalUUID, providerType, providerUUID, issuer, subject, payload, time.Now().UnixMilli()); err != nil {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
return tx.Commit()
|
||||
return s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
var eligible, alreadyBound bool
|
||||
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=`+s.sqlParam(1)+` AND m.state='active' WHERE p.uuid=`+s.sqlParam(2)+` AND p.state IN ('active','readonly'))`, domainUUID, principalUUID).Scan(&eligible); err != nil || !eligible {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM principal_identities WHERE principal_uuid=`+s.sqlParam(1)+` AND provider_type=`+s.sqlParam(2)+` AND provider_uuid=`+s.sqlParam(3)+`)`, principalUUID, providerType, providerUUID).Scan(&alreadyBound); err != nil || alreadyBound {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO principal_identities(uuid,principal_uuid,provider_type,provider_uuid,issuer,subject,attributes_json,created_at_ms) VALUES(`+s.sqlValues(8)+`)`, uuid.NewString(), principalUUID, providerType, providerUUID, issuer, subject, payload, time.Now().UnixMilli()); err != nil {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// ProvisionExternalAccount creates a normal Domain member with a local
|
||||
@@ -172,42 +167,49 @@ func (s *Store) ProvisionExternalAccount(domainUUID, username, displayName, emai
|
||||
if p.DisplayName == "" {
|
||||
p.DisplayName = username
|
||||
}
|
||||
tx, err := s.DB.Begin()
|
||||
err = s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
var present bool
|
||||
if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domains WHERE uuid=$1 AND enabled=1 AND placement='local')`, domainUUID).Scan(&present); err != nil {
|
||||
return err
|
||||
}
|
||||
if !present {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
}
|
||||
roleID := ""
|
||||
err = tx.QueryRowContext(ctx, `SELECT uuid FROM auth_roles WHERE domain_uuid=`+s.sqlParam(1)+` AND name='member'`, domainUUID).Scan(&roleID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
roleID = uuid.NewString()
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO auth_roles(uuid,domain_uuid,name,description,created_at_ms) VALUES(`+s.sqlValues(5)+`)`, roleID, domainUUID, "member", "Domain member", now); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(`+s.sqlValues(6)+`)`, uuid.NewString(), roleID, "read", "domain", domainUUID, now); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
membershipID := uuid.NewString()
|
||||
for _, item := range []struct {
|
||||
query string
|
||||
args []any
|
||||
}{
|
||||
{`INSERT INTO principals(uuid,display_name,email,state,created_at_ms,updated_at_ms) VALUES(` + s.sqlValues(6) + `)`, []any{p.UUID, p.DisplayName, p.Email, p.State, now, now}},
|
||||
{`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,subject,created_at_ms) VALUES(` + s.sqlValues(5) + `)`, []any{uuid.NewString(), p.UUID, "password", username, now}},
|
||||
{`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,provider_uuid,issuer,subject,attributes_json,created_at_ms) VALUES(` + s.sqlValues(8) + `)`, []any{uuid.NewString(), p.UUID, providerType, providerUUID, issuer, subject, payload, now}},
|
||||
{`INSERT INTO domain_memberships(uuid,domain_uuid,principal_uuid,state,created_at_ms) VALUES(` + s.sqlValues(5) + `)`, []any{membershipID, domainUUID, p.UUID, "active", now}},
|
||||
{`INSERT INTO auth_role_bindings(membership_uuid,role_uuid,created_at_ms) VALUES(` + s.sqlValues(3) + `)`, []any{membershipID, roleID, now}},
|
||||
} {
|
||||
if _, err = tx.ExecContext(ctx, item.query, item.args...); err != nil {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
roleID := ""
|
||||
err = tx.QueryRow(`SELECT uuid FROM auth_roles WHERE domain_uuid=? AND name='member'`, domainUUID).Scan(&roleID)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
roleID = uuid.NewString()
|
||||
if _, err = tx.Exec(`INSERT INTO auth_roles(uuid,domain_uuid,name,description,created_at_ms) VALUES(?,?,?,?,?)`, roleID, domainUUID, "member", "Domain member", now); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err = tx.Exec(`INSERT INTO auth_capability_grants(uuid,role_uuid,capability,target_type,target_id,created_at_ms) VALUES(?,?,?,?,?,?)`, uuid.NewString(), roleID, "read", "domain", domainUUID, now); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
membershipID := uuid.NewString()
|
||||
for _, item := range []struct {
|
||||
query string
|
||||
args []any
|
||||
}{
|
||||
{`INSERT INTO principals(uuid,display_name,email,state,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,?)`, []any{p.UUID, p.DisplayName, p.Email, p.State, now, now}},
|
||||
{`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,subject,created_at_ms) VALUES(?,?,?,?,?)`, []any{uuid.NewString(), p.UUID, "password", username, now}},
|
||||
{`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,provider_uuid,issuer,subject,attributes_json,created_at_ms) VALUES(?,?,?,?,?,?,?,?)`, []any{uuid.NewString(), p.UUID, providerType, providerUUID, issuer, subject, payload, now}},
|
||||
{`INSERT INTO domain_memberships(uuid,domain_uuid,principal_uuid,state,created_at_ms) VALUES(?,?,?,?,?)`, []any{membershipID, domainUUID, p.UUID, "active", now}},
|
||||
{`INSERT INTO auth_role_bindings(membership_uuid,role_uuid,created_at_ms) VALUES(?,?,?)`, []any{membershipID, roleID, now}},
|
||||
} {
|
||||
if _, err = tx.Exec(item.query, item.args...); err != nil {
|
||||
return nil, ErrAccessDenied
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
@@ -248,20 +250,23 @@ func (s *Store) SetAuthProviderConfig(domainUUID string, config AuthProviderConf
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_ = s.DB.QueryRow(`SELECT secret_encrypted FROM auth_providers WHERE domain_uuid=? AND kind=?`, domainUUID, config.Kind).Scan(&encrypted)
|
||||
}
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_providers(uuid,domain_uuid,kind,config_json,secret_encrypted,enabled,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,?,?,?) ON CONFLICT(domain_uuid,kind) DO UPDATE SET config_json=excluded.config_json,secret_encrypted=excluded.secret_encrypted,enabled=excluded.enabled,updated_at_ms=excluded.updated_at_ms`, uuid.NewString(), domainUUID, config.Kind, string(payload), encrypted, config.Enabled, time.Now().UnixMilli(), time.Now().UnixMilli())
|
||||
return err
|
||||
enabled := 0
|
||||
if config.Enabled {
|
||||
enabled = 1
|
||||
}
|
||||
// Preserve the current secret inside the UPSERT, not via a stale pre-read.
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_providers(uuid,domain_uuid,kind,config_json,secret_encrypted,enabled,created_at_ms,updated_at_ms) VALUES(`+s.sqlValues(8)+`) ON CONFLICT(domain_uuid,kind) DO UPDATE SET config_json=excluded.config_json,secret_encrypted=CASE WHEN excluded.secret_encrypted='' THEN auth_providers.secret_encrypted ELSE excluded.secret_encrypted END,enabled=excluded.enabled,updated_at_ms=excluded.updated_at_ms`, uuid.NewString(), domainUUID, config.Kind, string(payload), encrypted, enabled, time.Now().UnixMilli(), time.Now().UnixMilli())
|
||||
return s.sqlError("auth provider update", err)
|
||||
}
|
||||
|
||||
func (s *Store) AuthProviderConfig(domainUUID, kind string) (AuthProviderConfig, error) {
|
||||
var config AuthProviderConfig
|
||||
var payload, encrypted string
|
||||
var enabled bool
|
||||
err := s.DB.QueryRow(`SELECT uuid,config_json,secret_encrypted,enabled FROM auth_providers WHERE domain_uuid=? AND kind=?`, domainUUID, kind).Scan(&config.UUID, &payload, &encrypted, &enabled)
|
||||
err := s.DB.QueryRow(`SELECT uuid,config_json,secret_encrypted,enabled FROM auth_providers WHERE domain_uuid=`+s.sqlParam(1)+` AND kind=`+s.sqlParam(2)+``, domainUUID, kind).Scan(&config.UUID, &payload, &encrypted, &enabled)
|
||||
if err != nil {
|
||||
return config, err
|
||||
return config, s.sqlError("auth provider lookup", err)
|
||||
}
|
||||
if err := json.Unmarshal([]byte(payload), &config); err != nil {
|
||||
return config, err
|
||||
@@ -304,8 +309,8 @@ func (s *Store) PrincipalByExternalUsername(domainUUID, username string) (*Princ
|
||||
}
|
||||
rows, err := s.DB.Query(`SELECT p.uuid,i.subject,p.display_name,p.email,p.state,length(p.avatar)>0
|
||||
FROM principal_identities i JOIN principals p ON p.uuid=i.principal_uuid
|
||||
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? AND m.state='active'
|
||||
WHERE i.provider_type='password' AND i.subject=? AND p.state IN ('active','readonly') LIMIT 2`, domainUUID, username)
|
||||
JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=`+s.sqlParam(1)+` AND m.state='active'
|
||||
WHERE i.provider_type='password' AND i.subject=`+s.sqlParam(2)+` AND p.state IN ('active','readonly') LIMIT 2`, domainUUID, username)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -335,7 +340,7 @@ func (s *Store) PrincipalByVerifiedEmail(domainUUID, email string) (*Principal,
|
||||
if email == "" || domainUUID == "" {
|
||||
return nil, ErrAccessDenied
|
||||
}
|
||||
rows, err := s.DB.Query(`SELECT p.uuid,COALESCE((SELECT subject FROM principal_identities i WHERE i.principal_uuid=p.uuid AND i.provider_type='password' LIMIT 1),p.uuid),p.display_name,p.email,p.state,length(p.avatar)>0 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? AND m.state='active' WHERE lower(p.email)=? AND p.state IN ('active','readonly') LIMIT 2`, domainUUID, email)
|
||||
rows, err := s.DB.Query(`SELECT p.uuid,COALESCE((SELECT subject FROM principal_identities i WHERE i.principal_uuid=p.uuid AND i.provider_type='password' ORDER BY subject,uuid LIMIT 1),p.uuid),p.display_name,p.email,p.state,length(p.avatar)>0 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=`+s.sqlParam(1)+` AND m.state='active' WHERE lower(p.email)=`+s.sqlParam(2)+` AND p.state IN ('active','readonly') LIMIT 2`, domainUUID, email)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -370,11 +375,11 @@ func (s *Store) PrincipalByDisplayName(domainUUID, displayName string) (*Princip
|
||||
return nil, ErrAccessDenied
|
||||
}
|
||||
rows, err := s.DB.Query(`SELECT p.uuid,
|
||||
COALESCE((SELECT subject FROM principal_identities i WHERE i.principal_uuid=p.uuid AND i.provider_type='password' LIMIT 1),p.uuid),
|
||||
COALESCE((SELECT subject FROM principal_identities i WHERE i.principal_uuid=p.uuid AND i.provider_type='password' ORDER BY subject,uuid LIMIT 1),p.uuid),
|
||||
p.display_name,p.email,p.state,length(p.avatar)>0
|
||||
FROM principals p JOIN domain_memberships m
|
||||
ON m.principal_uuid=p.uuid AND m.domain_uuid=? AND m.state='active'
|
||||
WHERE p.display_name=? AND p.state IN ('active','readonly') LIMIT 2`, domainUUID, displayName)
|
||||
ON m.principal_uuid=p.uuid AND m.domain_uuid=`+s.sqlParam(1)+` AND m.state='active'
|
||||
WHERE p.display_name=`+s.sqlParam(2)+` AND p.state IN ('active','readonly') LIMIT 2`, domainUUID, displayName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -408,7 +413,7 @@ func (s *Store) BindExternalIdentity(principalUUID, providerType, providerUUID,
|
||||
return err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
result, err := s.DB.Exec(`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,provider_uuid,issuer,subject,attributes_json,created_at_ms) VALUES(?,?,?,?,?,?,?,?) ON CONFLICT(provider_type,provider_uuid,issuer,subject) DO UPDATE SET attributes_json=excluded.attributes_json WHERE principal_identities.principal_uuid=excluded.principal_uuid`, uuid.NewString(), principalUUID, providerType, providerUUID, issuer, subject, string(payload), now)
|
||||
result, err := s.DB.Exec(`INSERT INTO principal_identities(uuid,principal_uuid,provider_type,provider_uuid,issuer,subject,attributes_json,created_at_ms) VALUES(`+s.sqlValues(8)+`) ON CONFLICT(provider_type,provider_uuid,issuer,subject) DO UPDATE SET attributes_json=excluded.attributes_json WHERE principal_identities.principal_uuid=excluded.principal_uuid`, uuid.NewString(), principalUUID, providerType, providerUUID, issuer, subject, string(payload), now)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -424,7 +429,7 @@ func (s *Store) BindExternalIdentity(principalUUID, providerType, providerUUID,
|
||||
|
||||
func (s *Store) PrincipalByExternalIdentity(domainUUID, providerType, providerUUID, issuer, subject string) (*Principal, error) {
|
||||
var p Principal
|
||||
err := s.DB.QueryRow(`SELECT p.uuid,COALESCE((SELECT subject FROM principal_identities x WHERE x.principal_uuid=p.uuid AND x.provider_type='password' LIMIT 1),p.uuid),p.display_name,p.email,p.state,length(p.avatar)>0 FROM principal_identities i JOIN principals p ON p.uuid=i.principal_uuid JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=? AND m.state='active' WHERE i.provider_type=? AND i.provider_uuid=? AND i.issuer=? AND i.subject=? AND p.state IN ('active','readonly')`, domainUUID, providerType, providerUUID, issuer, subject).Scan(&p.UUID, &p.Username, &p.DisplayName, &p.Email, &p.State, &p.HasAvatar)
|
||||
err := s.DB.QueryRow(`SELECT p.uuid,COALESCE((SELECT subject FROM principal_identities x WHERE x.principal_uuid=p.uuid AND x.provider_type='password' ORDER BY subject,uuid LIMIT 1),p.uuid),p.display_name,p.email,p.state,length(p.avatar)>0 FROM principal_identities i JOIN principals p ON p.uuid=i.principal_uuid JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=`+s.sqlParam(1)+` AND m.state='active' WHERE i.provider_type=`+s.sqlParam(2)+` AND i.provider_uuid=`+s.sqlParam(3)+` AND i.issuer=`+s.sqlParam(4)+` AND i.subject=`+s.sqlParam(5)+` AND p.state IN ('active','readonly')`, domainUUID, providerType, providerUUID, issuer, subject).Scan(&p.UUID, &p.Username, &p.DisplayName, &p.Email, &p.State, &p.HasAvatar)
|
||||
return &p, err
|
||||
}
|
||||
|
||||
@@ -433,23 +438,29 @@ func (s *Store) CreateAuthChallenge(domainUUID, principalUUID, kind string, payl
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
encoded, _ := json.Marshal(payload)
|
||||
encoded, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_challenges(uuid,domain_uuid,principal_uuid,kind,token_hash,payload_json,expires_at_ms,created_at_ms) VALUES(?,?,?,?,?,?,?,?)`, uuid.NewString(), domainUUID, principalUUID, kind, hash, string(encoded), now+lifetime.Milliseconds(), now)
|
||||
return token, err
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_challenges(uuid,domain_uuid,principal_uuid,kind,token_hash,payload_json,expires_at_ms,created_at_ms) VALUES(`+s.sqlValues(8)+`)`, uuid.NewString(), domainUUID, principalUUID, kind, hash, string(encoded), now+lifetime.Milliseconds(), now)
|
||||
if err != nil {
|
||||
return "", s.sqlError("auth challenge creation", err)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (s *Store) ConsumeAuthChallenge(domainUUID, kind, token string, payload any) (string, error) {
|
||||
hash := hashAuthToken(token)
|
||||
var id, principalUUID, encoded string
|
||||
var expires int64
|
||||
err := s.DB.QueryRow(`SELECT uuid,principal_uuid,payload_json,expires_at_ms FROM auth_challenges WHERE domain_uuid=? AND kind=? AND token_hash=? AND consumed_at_ms=0`, domainUUID, kind, hash).Scan(&id, &principalUUID, &encoded, &expires)
|
||||
err := s.DB.QueryRow(`SELECT uuid,principal_uuid,payload_json,expires_at_ms FROM auth_challenges WHERE domain_uuid=`+s.sqlParam(1)+` AND kind=`+s.sqlParam(2)+` AND token_hash=`+s.sqlParam(3)+` AND consumed_at_ms=0`, domainUUID, kind, hash).Scan(&id, &principalUUID, &encoded, &expires)
|
||||
if err != nil || expires < time.Now().UnixMilli() {
|
||||
return "", ErrAccessDenied
|
||||
}
|
||||
result, err := s.DB.Exec(`UPDATE auth_challenges SET consumed_at_ms=? WHERE uuid=? AND consumed_at_ms=0`, time.Now().UnixMilli(), id)
|
||||
result, err := s.DB.Exec(`UPDATE auth_challenges SET consumed_at_ms=`+s.sqlParam(1)+` WHERE uuid=`+s.sqlParam(2)+` AND consumed_at_ms=0 AND expires_at_ms>`+s.sqlParam(3), time.Now().UnixMilli(), id, time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
return "", err
|
||||
return "", s.sqlError("auth challenge consumption", err)
|
||||
}
|
||||
changed, _ := result.RowsAffected()
|
||||
if changed != 1 {
|
||||
@@ -461,6 +472,17 @@ func (s *Store) ConsumeAuthChallenge(domainUUID, kind, token string, payload any
|
||||
return principalUUID, nil
|
||||
}
|
||||
|
||||
// A consumed link does not authorize a changed email address or a disabled
|
||||
// membership. Keep the HTTP layer independent of the SQL placeholder dialect.
|
||||
func (s *Store) ValidateMagicLinkIdentity(ctx context.Context, domain, principal, email string) (bool, error) {
|
||||
if email == "" {
|
||||
return false, nil
|
||||
}
|
||||
var valid bool
|
||||
err := s.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM principals p JOIN domain_memberships m ON m.principal_uuid=p.uuid AND m.domain_uuid=`+s.sqlParam(1)+` WHERE p.uuid=`+s.sqlParam(2)+` AND p.state IN ('active','readonly') AND m.state='active' AND p.email=`+s.sqlParam(3)+`)`, domain, principal, email).Scan(&valid)
|
||||
return err == nil && valid, s.sqlError("magic link identity", err)
|
||||
}
|
||||
|
||||
func hashAuthToken(token string) string {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
return base64.RawURLEncoding.EncodeToString(sum[:])
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
)
|
||||
|
||||
// This foundation is deliberately separate from EnsureAuthSchema: full
|
||||
// authorization, audit and locality policies are required before serving auth.
|
||||
// Existing tables are never adopted or repaired implicitly.
|
||||
func (s *Store) ensurePostgreSQLAuthFoundation(ctx context.Context) error {
|
||||
steps := []db.PostgreSQLMigration{{
|
||||
Version: 1, Name: "identities memberships and administrative groups",
|
||||
Statements: []string{
|
||||
`CREATE TABLE principals(uuid TEXT COLLATE "C" PRIMARY KEY,display_name TEXT NOT NULL DEFAULT '',email TEXT NOT NULL DEFAULT '',state TEXT NOT NULL DEFAULT 'active',avatar BYTEA NOT NULL DEFAULT ''::bytea,avatar_content_type TEXT NOT NULL DEFAULT '',created_at_ms BIGINT NOT NULL,updated_at_ms BIGINT NOT NULL)`,
|
||||
`CREATE TABLE principal_identities(uuid TEXT COLLATE "C" PRIMARY KEY,principal_uuid TEXT COLLATE "C" NOT NULL,provider_type TEXT COLLATE "C" NOT NULL,provider_uuid TEXT COLLATE "C" NOT NULL DEFAULT '',issuer TEXT COLLATE "C" NOT NULL DEFAULT '',subject TEXT COLLATE "C" NOT NULL,attributes_json TEXT NOT NULL DEFAULT '{}',created_at_ms BIGINT NOT NULL,UNIQUE(provider_type,provider_uuid,issuer,subject))`,
|
||||
`CREATE TABLE domain_memberships(uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,principal_uuid TEXT COLLATE "C" NOT NULL,state TEXT NOT NULL DEFAULT 'active',created_at_ms BIGINT NOT NULL,UNIQUE(domain_uuid,principal_uuid))`,
|
||||
`CREATE TABLE auth_roles(uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,name TEXT COLLATE "C" NOT NULL,description TEXT NOT NULL DEFAULT '',created_at_ms BIGINT NOT NULL,UNIQUE(domain_uuid,name))`,
|
||||
`CREATE TABLE auth_role_bindings(membership_uuid TEXT COLLATE "C" NOT NULL,role_uuid TEXT COLLATE "C" NOT NULL,created_at_ms BIGINT NOT NULL,PRIMARY KEY(membership_uuid,role_uuid))`,
|
||||
`CREATE TABLE auth_capability_grants(uuid TEXT COLLATE "C" PRIMARY KEY,role_uuid TEXT COLLATE "C" NOT NULL,capability TEXT COLLATE "C" NOT NULL,target_type TEXT COLLATE "C" NOT NULL,target_id TEXT COLLATE "C" NOT NULL DEFAULT '*',created_at_ms BIGINT NOT NULL,UNIQUE(role_uuid,capability,target_type,target_id))`,
|
||||
`CREATE TABLE groups(uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,name TEXT COLLATE "C" NOT NULL,display_name TEXT NOT NULL DEFAULT '',description TEXT NOT NULL DEFAULT '',visibility TEXT NOT NULL DEFAULT 'members',state TEXT NOT NULL DEFAULT 'active',builtin_key TEXT NOT NULL DEFAULT '',inherit_members INTEGER NOT NULL DEFAULT 0,created_at_ms BIGINT NOT NULL,updated_at_ms BIGINT NOT NULL,UNIQUE(domain_uuid,name))`,
|
||||
`CREATE TABLE group_members(group_uuid TEXT COLLATE "C" NOT NULL,membership_uuid TEXT COLLATE "C" NOT NULL,member_role TEXT NOT NULL DEFAULT 'member',source TEXT NOT NULL DEFAULT 'local',created_at_ms BIGINT NOT NULL,PRIMARY KEY(group_uuid,membership_uuid))`,
|
||||
`CREATE TABLE campus_admin_grants(group_uuid TEXT COLLATE "C" NOT NULL,membership_uuid TEXT COLLATE "C" NOT NULL,source TEXT COLLATE "C" NOT NULL,member_role TEXT NOT NULL,created_at_ms BIGINT NOT NULL,PRIMARY KEY(group_uuid,membership_uuid,source))`,
|
||||
`CREATE INDEX campus_admin_grants_membership ON campus_admin_grants(membership_uuid)`,
|
||||
`CREATE TABLE campus_admin_membership_origins(membership_uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,principal_uuid TEXT COLLATE "C" NOT NULL,UNIQUE(domain_uuid,principal_uuid))`,
|
||||
`CREATE TABLE campus_admin_migrations(key TEXT COLLATE "C" PRIMARY KEY)`,
|
||||
`CREATE INDEX domain_memberships_principal ON domain_memberships(principal_uuid,domain_uuid)`,
|
||||
`CREATE INDEX group_members_membership ON group_members(membership_uuid,group_uuid)`,
|
||||
},
|
||||
Assertions: []string{
|
||||
`SELECT uuid,display_name,email,state,avatar,avatar_content_type,created_at_ms,updated_at_ms FROM principals WHERE 1=0`,
|
||||
`SELECT uuid,principal_uuid,provider_type,provider_uuid,issuer,subject,attributes_json,created_at_ms FROM principal_identities WHERE 1=0`,
|
||||
`SELECT uuid,domain_uuid,principal_uuid,state,created_at_ms FROM domain_memberships WHERE 1=0`,
|
||||
`SELECT uuid,domain_uuid,name,description,created_at_ms FROM auth_roles WHERE 1=0`,
|
||||
`SELECT membership_uuid,role_uuid,created_at_ms FROM auth_role_bindings WHERE 1=0`,
|
||||
`SELECT uuid,role_uuid,capability,target_type,target_id,created_at_ms FROM auth_capability_grants WHERE 1=0`,
|
||||
`SELECT uuid,domain_uuid,name,display_name,description,visibility,state,builtin_key,inherit_members,created_at_ms,updated_at_ms FROM groups WHERE 1=0`,
|
||||
`SELECT group_uuid,membership_uuid,member_role,source,created_at_ms FROM group_members WHERE 1=0`,
|
||||
`SELECT group_uuid,membership_uuid,source,member_role,created_at_ms FROM campus_admin_grants WHERE 1=0`,
|
||||
`SELECT membership_uuid,domain_uuid,principal_uuid FROM campus_admin_membership_origins WHERE 1=0`,
|
||||
`SELECT key FROM campus_admin_migrations WHERE 1=0`,
|
||||
},
|
||||
}}
|
||||
// Session storage is required for atomic revocation of derived membership.
|
||||
// Account/session APIs are added in later component ports; this step's DDL
|
||||
// and checksum remain unchanged as those APIs become available.
|
||||
steps = append(steps, db.PostgreSQLMigration{
|
||||
Version: 2, Name: "session storage for administrative revocation",
|
||||
Statements: []string{
|
||||
`CREATE TABLE auth_sessions(uuid TEXT COLLATE "C" PRIMARY KEY,token_hash TEXT COLLATE "C" NOT NULL UNIQUE,principal_uuid TEXT COLLATE "C" NOT NULL,domain_uuid TEXT COLLATE "C" NOT NULL,auth_time_ms BIGINT NOT NULL,expires_at_ms BIGINT NOT NULL,last_seen_at_ms BIGINT NOT NULL,mfa_level INTEGER NOT NULL DEFAULT 0,revoked_at_ms BIGINT NOT NULL DEFAULT 0)`,
|
||||
`CREATE INDEX auth_sessions_membership ON auth_sessions(domain_uuid,principal_uuid) WHERE revoked_at_ms=0`,
|
||||
},
|
||||
Assertions: append(append([]string{}, steps[0].Assertions...), `SELECT uuid,token_hash,principal_uuid,domain_uuid,auth_time_ms,expires_at_ms,last_seen_at_ms,mfa_level,revoked_at_ms FROM auth_sessions WHERE 1=0`),
|
||||
})
|
||||
steps = append(steps, db.PostgreSQLMigration{
|
||||
Version: 3, Name: "scoped group permissions",
|
||||
Statements: []string{
|
||||
`CREATE TABLE group_permissions(uuid TEXT COLLATE "C" PRIMARY KEY,group_uuid TEXT COLLATE "C" NOT NULL,scope_path TEXT COLLATE "C" NOT NULL,permission TEXT NOT NULL,created_at_ms BIGINT NOT NULL,UNIQUE(group_uuid,scope_path))`,
|
||||
`CREATE INDEX principal_identities_principal_provider ON principal_identities(principal_uuid,provider_type,subject,uuid)`,
|
||||
},
|
||||
Assertions: append(append([]string{}, steps[1].Assertions...), `SELECT uuid,group_uuid,scope_path,permission,created_at_ms FROM group_permissions WHERE 1=0`),
|
||||
})
|
||||
steps = append(steps, db.PostgreSQLMigration{Version: 4, Name: "account credentials", Statements: []string{
|
||||
`CREATE TABLE auth_credentials(uuid TEXT COLLATE "C" PRIMARY KEY,principal_uuid TEXT COLLATE "C" NOT NULL,kind TEXT COLLATE "C" NOT NULL,identifier TEXT COLLATE "C" NOT NULL DEFAULT '',secret_hash TEXT NOT NULL,parameters_json TEXT NOT NULL DEFAULT '{}',enabled INTEGER NOT NULL DEFAULT 1,created_at_ms BIGINT NOT NULL,updated_at_ms BIGINT NOT NULL,UNIQUE(kind,identifier))`,
|
||||
}, Assertions: append(append([]string{}, steps[2].Assertions...), `SELECT uuid,principal_uuid,kind,identifier,secret_hash,parameters_json,enabled,created_at_ms,updated_at_ms FROM auth_credentials WHERE 1=0`)})
|
||||
steps = append(steps, db.PostgreSQLMigration{Version: 5, Name: "authentication challenges", Statements: []string{
|
||||
`CREATE TABLE auth_challenges(uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,principal_uuid TEXT COLLATE "C" NOT NULL DEFAULT '',kind TEXT COLLATE "C" NOT NULL,token_hash TEXT COLLATE "C" NOT NULL UNIQUE,payload_json TEXT NOT NULL DEFAULT '{}',expires_at_ms BIGINT NOT NULL,consumed_at_ms BIGINT NOT NULL DEFAULT 0,attempt_count INTEGER NOT NULL DEFAULT 0,created_at_ms BIGINT NOT NULL)`,
|
||||
`CREATE INDEX auth_challenges_expiry ON auth_challenges(expires_at_ms)`,
|
||||
}, Assertions: append(append([]string{}, steps[3].Assertions...), `SELECT uuid,domain_uuid,principal_uuid,kind,token_hash,payload_json,expires_at_ms,consumed_at_ms,attempt_count,created_at_ms FROM auth_challenges WHERE 1=0`)})
|
||||
steps = append(steps, db.PostgreSQLMigration{Version: 6, Name: "authentication provider configuration", Statements: []string{
|
||||
`CREATE TABLE auth_providers(uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,kind TEXT COLLATE "C" NOT NULL,config_json TEXT NOT NULL DEFAULT '{}',secret_encrypted TEXT NOT NULL DEFAULT '',enabled INTEGER NOT NULL DEFAULT 0,created_at_ms BIGINT NOT NULL,updated_at_ms BIGINT NOT NULL,UNIQUE(domain_uuid,kind))`,
|
||||
}, Assertions: append(append([]string{}, steps[4].Assertions...), `SELECT uuid,domain_uuid,kind,config_json,secret_encrypted,enabled,created_at_ms,updated_at_ms FROM auth_providers WHERE 1=0`)})
|
||||
steps = append(steps, db.PostgreSQLMigration{Version: 7, Name: "CLI authorization handoff", Statements: []string{
|
||||
`CREATE TABLE auth_cli_requests(id TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,origin TEXT COLLATE "C" NOT NULL,device_hash TEXT COLLATE "C" NOT NULL UNIQUE,challenge TEXT COLLATE "C" NOT NULL,code_hash TEXT COLLATE "C" NOT NULL,state TEXT NOT NULL DEFAULT 'pending',browser_hash TEXT COLLATE "C" NOT NULL DEFAULT '',principal_uuid TEXT COLLATE "C" NOT NULL DEFAULT '',mfa_level INTEGER NOT NULL DEFAULT 0,attempts INTEGER NOT NULL DEFAULT 0,oidc_attempts INTEGER NOT NULL DEFAULT 0,created_ms BIGINT NOT NULL,expires_ms BIGINT NOT NULL,next_poll_ms BIGINT NOT NULL DEFAULT 0,remember INTEGER NOT NULL DEFAULT 0 CHECK(remember IN (0,1)))`,
|
||||
`CREATE INDEX auth_cli_domain_created ON auth_cli_requests(domain_uuid,created_ms)`,
|
||||
`CREATE INDEX auth_cli_expiry ON auth_cli_requests(expires_ms)`,
|
||||
}, Assertions: append(append([]string{}, steps[5].Assertions...), `SELECT id,domain_uuid,origin,device_hash,challenge,code_hash,state,browser_hash,principal_uuid,mfa_level,attempts,oidc_attempts,created_ms,expires_ms,next_poll_ms,remember FROM auth_cli_requests WHERE 1=0`)})
|
||||
steps = append(steps, db.PostgreSQLMigration{Version: 8, Name: "authorization mapping and audit storage", Statements: []string{
|
||||
`CREATE TABLE auth_group_role_bindings(group_uuid TEXT COLLATE "C" NOT NULL,role_uuid TEXT COLLATE "C" NOT NULL,created_at_ms BIGINT NOT NULL,PRIMARY KEY(group_uuid,role_uuid))`,
|
||||
`CREATE TABLE auth_group_mappings(uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,provider_type TEXT NOT NULL,provider_uuid TEXT COLLATE "C" NOT NULL,external_group TEXT COLLATE "C" NOT NULL,group_uuid TEXT COLLATE "C" NOT NULL,mode TEXT NOT NULL DEFAULT 'additive',enabled INTEGER NOT NULL DEFAULT 1,UNIQUE(domain_uuid,provider_type,provider_uuid,external_group))`,
|
||||
`CREATE TABLE auth_audit_log(id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL DEFAULT '',principal_uuid TEXT COLLATE "C" NOT NULL DEFAULT '',event TEXT NOT NULL,outcome TEXT NOT NULL,target_type TEXT NOT NULL DEFAULT '',target_id TEXT NOT NULL DEFAULT '',request_id TEXT NOT NULL DEFAULT '',detail_json TEXT NOT NULL DEFAULT '{}',created_at_ms BIGINT NOT NULL)`,
|
||||
}, Assertions: append(append([]string{}, steps[6].Assertions...),
|
||||
`SELECT group_uuid,role_uuid,created_at_ms FROM auth_group_role_bindings WHERE 1=0`,
|
||||
`SELECT uuid,domain_uuid,provider_type,provider_uuid,external_group,group_uuid,mode,enabled FROM auth_group_mappings WHERE 1=0`,
|
||||
`SELECT id,domain_uuid,principal_uuid,event,outcome,target_type,target_id,request_id,detail_json,created_at_ms FROM auth_audit_log WHERE 1=0`)})
|
||||
return db.ApplyPostgreSQLMigrations(ctx, s.DB, "store-auth-foundation", steps)
|
||||
}
|
||||
+64
-46
@@ -1,12 +1,14 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/base32"
|
||||
"encoding/base64"
|
||||
"encoding/binary"
|
||||
@@ -63,8 +65,14 @@ func (s *Store) BeginTOTPEnrollment(principalUUID string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,parameters_json,enabled,created_at_ms,updated_at_ms) VALUES(?,?,?,?,?,'{}',0,?,?) ON CONFLICT(kind,identifier) DO UPDATE SET secret_hash=excluded.secret_hash,parameters_json='{}',enabled=0,updated_at_ms=excluded.updated_at_ms`, uuid.NewString(), principalUUID, "totp-pending", principalUUID, encrypted, now, now)
|
||||
return secret, err
|
||||
err = s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `INSERT INTO auth_credentials(uuid,principal_uuid,kind,identifier,secret_hash,parameters_json,enabled,created_at_ms,updated_at_ms) VALUES(`+s.sqlValues(9)+`) ON CONFLICT(kind,identifier) DO UPDATE SET secret_hash=excluded.secret_hash,parameters_json='{}',enabled=0,updated_at_ms=excluded.updated_at_ms`, uuid.NewString(), principalUUID, "totp-pending", principalUUID, encrypted, "{}", 0, now, now)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return secret, nil
|
||||
}
|
||||
func totpCode(secret string, counter int64) (string, error) {
|
||||
key, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(secret))
|
||||
@@ -95,9 +103,9 @@ func verifyTOTPCode(secret, code string, last int64) (int64, bool) {
|
||||
}
|
||||
return 0, false
|
||||
}
|
||||
func (s *Store) confirmTOTP(principalUUID, kind, code string) (int64, error) {
|
||||
func (s *Store) confirmTOTP(ctx context.Context, tx *sql.Tx, principalUUID, kind, code string) (int64, error) {
|
||||
var encrypted, params string
|
||||
err := s.DB.QueryRow(`SELECT secret_hash,parameters_json FROM auth_credentials WHERE principal_uuid=? AND kind=? AND identifier=?`, principalUUID, kind, principalUUID).Scan(&encrypted, ¶ms)
|
||||
err := tx.QueryRowContext(ctx, fmt.Sprintf(`SELECT secret_hash,parameters_json FROM auth_credentials WHERE principal_uuid=%s AND kind=%s AND identifier=%s AND (kind='totp-pending' OR enabled=1)`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), principalUUID, kind, principalUUID).Scan(&encrypted, ¶ms)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -108,7 +116,9 @@ func (s *Store) confirmTOTP(principalUUID, kind, code string) (int64, error) {
|
||||
var state struct {
|
||||
LastCounter int64 `json:"last_counter"`
|
||||
}
|
||||
_ = json.Unmarshal([]byte(params), &state)
|
||||
if json.Unmarshal([]byte(params), &state) != nil {
|
||||
return 0, ErrAccessDenied
|
||||
}
|
||||
counter, ok := verifyTOTPCode(string(plain), code, state.LastCounter)
|
||||
if !ok {
|
||||
return 0, ErrAccessDenied
|
||||
@@ -116,31 +126,37 @@ func (s *Store) confirmTOTP(principalUUID, kind, code string) (int64, error) {
|
||||
return counter, nil
|
||||
}
|
||||
func (s *Store) ConfirmTOTPEnrollment(principalUUID, code string) error {
|
||||
counter, err := s.confirmTOTP(principalUUID, "totp-pending", code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params, _ := json.Marshal(map[string]int64{"last_counter": counter})
|
||||
tx, err := s.DB.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
_, _ = tx.Exec(`DELETE FROM auth_credentials WHERE principal_uuid=? AND kind='totp'`, principalUUID)
|
||||
_, err = tx.Exec(`UPDATE auth_credentials SET kind='totp',enabled=1,parameters_json=?,updated_at_ms=? WHERE principal_uuid=? AND kind='totp-pending'`, string(params), time.Now().UnixMilli(), principalUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
return s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
counter, err := s.confirmTOTP(ctx, tx, principalUUID, "totp-pending", code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params, _ := json.Marshal(map[string]int64{"last_counter": counter})
|
||||
if _, err = tx.ExecContext(ctx, `DELETE FROM auth_credentials WHERE principal_uuid=`+s.sqlParam(1)+` AND kind='totp'`, principalUUID); err != nil {
|
||||
return err
|
||||
}
|
||||
result, err := tx.ExecContext(ctx, fmt.Sprintf(`UPDATE auth_credentials SET kind='totp',enabled=1,parameters_json=%s,updated_at_ms=%s WHERE principal_uuid=%s AND kind='totp-pending'`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), string(params), time.Now().UnixMilli(), principalUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n != 1 {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
func (s *Store) HasTOTP(principalUUID string) bool {
|
||||
var exists int
|
||||
_ = s.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM auth_credentials WHERE principal_uuid=? AND kind='totp' AND enabled=1)`, principalUUID).Scan(&exists)
|
||||
return exists != 0
|
||||
var exists bool
|
||||
_ = s.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM auth_credentials WHERE principal_uuid=`+s.sqlParam(1)+` AND kind='totp' AND enabled=1)`, principalUUID).Scan(&exists)
|
||||
return exists
|
||||
}
|
||||
func (s *Store) PendingTOTPSecret(principalUUID string) string {
|
||||
var encrypted string
|
||||
if s.DB.QueryRow(`SELECT secret_hash FROM auth_credentials WHERE principal_uuid=? AND kind='totp-pending'`, principalUUID).Scan(&encrypted) != nil {
|
||||
if s.DB.QueryRow(`SELECT secret_hash FROM auth_credentials WHERE principal_uuid=`+s.sqlParam(1)+` AND kind='totp-pending'`, principalUUID).Scan(&encrypted) != nil {
|
||||
return ""
|
||||
}
|
||||
plain, err := s.decryptAuthSecret(encrypted)
|
||||
@@ -150,24 +166,23 @@ func (s *Store) PendingTOTPSecret(principalUUID string) string {
|
||||
return string(plain)
|
||||
}
|
||||
func (s *Store) VerifyTOTP(principalUUID, code string) error {
|
||||
counter, err := s.confirmTOTP(principalUUID, "totp", code)
|
||||
if err != nil {
|
||||
return s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
// Validation and counter advancement share the same lock on both DBs.
|
||||
// No JSON SQL dialect is needed, and enrollment cannot race verification.
|
||||
counter, err := s.confirmTOTP(ctx, tx, principalUUID, "totp", code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params, _ := json.Marshal(map[string]int64{"last_counter": counter})
|
||||
_, err = tx.ExecContext(ctx, fmt.Sprintf(`UPDATE auth_credentials SET parameters_json=%s,updated_at_ms=%s WHERE principal_uuid=%s AND kind='totp' AND enabled=1`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), string(params), time.Now().UnixMilli(), principalUUID)
|
||||
return err
|
||||
}
|
||||
params, _ := json.Marshal(map[string]int64{"last_counter": counter})
|
||||
result, err := s.DB.Exec(`UPDATE auth_credentials SET parameters_json=?,updated_at_ms=? WHERE principal_uuid=? AND kind='totp' AND CAST(COALESCE(json_extract(parameters_json,'$.last_counter'),0) AS INTEGER)<?`, string(params), time.Now().UnixMilli(), principalUUID, counter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
changed, _ := result.RowsAffected()
|
||||
if changed != 1 {
|
||||
return ErrAccessDenied
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
func (s *Store) DisableTOTP(principalUUID string) error {
|
||||
_, err := s.DB.Exec(`DELETE FROM auth_credentials WHERE principal_uuid=? AND kind IN ('totp','totp-pending')`, principalUUID)
|
||||
return err
|
||||
return s.groupWrite(context.Background(), func(ctx context.Context, tx *sql.Tx) error {
|
||||
_, err := tx.ExecContext(ctx, `DELETE FROM auth_credentials WHERE principal_uuid=`+s.sqlParam(1)+` AND kind IN ('totp','totp-pending')`, principalUUID)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func TOTPURI(secret, account, issuer string) string {
|
||||
@@ -186,8 +201,11 @@ func (s *Store) CreateTOTPLoginChallengeForLogin(domainUUID, principalUUID strin
|
||||
}
|
||||
now := time.Now().UnixMilli()
|
||||
payload, _ := json.Marshal(map[string]any{"principal_uuid": principalUUID, "remember": remember})
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_challenges(uuid,domain_uuid,principal_uuid,kind,token_hash,payload_json,expires_at_ms,created_at_ms) VALUES(?,?,?,?,?,?,?,?)`, uuid.NewString(), domainUUID, principalUUID, "totp-login", hash, string(payload), now+5*60*1000, now)
|
||||
return token, err
|
||||
_, err = s.DB.Exec(`INSERT INTO auth_challenges(uuid,domain_uuid,principal_uuid,kind,token_hash,payload_json,expires_at_ms,created_at_ms) VALUES(`+s.sqlValues(8)+`)`, uuid.NewString(), domainUUID, principalUUID, "totp-login", hash, string(payload), now+5*60*1000, now)
|
||||
if err != nil {
|
||||
return "", s.sqlError("TOTP login challenge", err)
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
func (s *Store) CompleteTOTPLoginChallenge(domainUUID, token, code string) (string, error) {
|
||||
id, _, err := s.CompleteTOTPLoginChallengeForLogin(domainUUID, token, code)
|
||||
@@ -200,7 +218,7 @@ func (s *Store) CompleteTOTPLoginChallengeForLogin(domainUUID, token, code strin
|
||||
var challengeUUID, principalUUID, payload string
|
||||
var expires int64
|
||||
var attempts int
|
||||
err := s.DB.QueryRow(`SELECT uuid,principal_uuid,expires_at_ms,attempt_count,payload_json FROM auth_challenges WHERE domain_uuid=? AND kind='totp-login' AND token_hash=? AND consumed_at_ms=0`, domainUUID, hash).Scan(&challengeUUID, &principalUUID, &expires, &attempts, &payload)
|
||||
err := s.DB.QueryRow(fmt.Sprintf(`SELECT uuid,principal_uuid,expires_at_ms,attempt_count,payload_json FROM auth_challenges WHERE domain_uuid=%s AND kind='totp-login' AND token_hash=%s AND consumed_at_ms=0`, s.sqlParam(1), s.sqlParam(2)), domainUUID, hash).Scan(&challengeUUID, &principalUUID, &expires, &attempts, &payload)
|
||||
if err != nil || expires < time.Now().UnixMilli() || attempts >= 5 {
|
||||
return "", false, ErrAccessDenied
|
||||
}
|
||||
@@ -211,12 +229,12 @@ func (s *Store) CompleteTOTPLoginChallengeForLogin(domainUUID, token, code strin
|
||||
return "", false, ErrAccessDenied
|
||||
}
|
||||
if err = s.VerifyTOTP(principalUUID, code); err != nil {
|
||||
_, _ = s.DB.Exec(`UPDATE auth_challenges SET attempt_count=attempt_count+1,consumed_at_ms=CASE WHEN attempt_count+1>=5 THEN ? ELSE consumed_at_ms END WHERE uuid=? AND consumed_at_ms=0`, time.Now().UnixMilli(), challengeUUID)
|
||||
_, _ = s.DB.Exec(fmt.Sprintf(`UPDATE auth_challenges SET attempt_count=attempt_count+1,consumed_at_ms=CASE WHEN attempt_count+1>=5 THEN %s ELSE consumed_at_ms END WHERE uuid=%s AND consumed_at_ms=0`, s.sqlParam(1), s.sqlParam(2)), time.Now().UnixMilli(), challengeUUID)
|
||||
return "", false, err
|
||||
}
|
||||
result, err := s.DB.Exec(`UPDATE auth_challenges SET consumed_at_ms=? WHERE uuid=? AND consumed_at_ms=0`, time.Now().UnixMilli(), challengeUUID)
|
||||
result, err := s.DB.Exec(fmt.Sprintf(`UPDATE auth_challenges SET consumed_at_ms=%s WHERE uuid=%s AND consumed_at_ms=0 AND attempt_count<5 AND expires_at_ms>%s`, s.sqlParam(1), s.sqlParam(2), s.sqlParam(3)), time.Now().UnixMilli(), challengeUUID, time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
return "", false, s.sqlError("TOTP challenge consumption", err)
|
||||
}
|
||||
changed, _ := result.RowsAffected()
|
||||
if changed != 1 {
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -37,7 +36,7 @@ type AutoPublicationChange struct {
|
||||
ImpactToken string `json:"impact_token"`
|
||||
}
|
||||
|
||||
func autoScope(ctx context.Context, tx *sql.Tx, domain, kind, id string) (string, error) {
|
||||
func (s *Store) autoScope(ctx context.Context, tx *sql.Tx, domain, kind, id string) (string, error) {
|
||||
if kind == "domain" {
|
||||
if id != domain {
|
||||
return "", ErrGovernanceInvalid
|
||||
@@ -49,11 +48,11 @@ func autoScope(ctx context.Context, tx *sql.Tx, domain, kind, id string) (string
|
||||
table = "spots"
|
||||
}
|
||||
var scope string
|
||||
err := tx.QueryRowContext(ctx, `SELECT path_prefix FROM `+table+` WHERE domain_uuid=? AND uuid=? AND state!='archived'`, domain, id).Scan(&scope)
|
||||
err := tx.QueryRowContext(ctx, `SELECT path_prefix FROM `+table+` WHERE domain_uuid=`+s.sqlParam(1)+` AND uuid=`+s.sqlParam(2)+` AND state!='archived'`, domain, id).Scan(&scope)
|
||||
return scope, err
|
||||
}
|
||||
func autoRules(ctx context.Context, tx *sql.Tx, domain string) ([]autoPublicationRule, error) {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT node_kind,node_uuid,mode,scope_path,actor_uuid,version FROM locality_auto_publication WHERE domain_uuid=? ORDER BY node_kind,node_uuid LIMIT 129`, domain)
|
||||
func (s *Store) autoRules(ctx context.Context, tx *sql.Tx, domain string) ([]autoPublicationRule, error) {
|
||||
rows, err := tx.QueryContext(ctx, `SELECT node_kind,node_uuid,mode,scope_path,actor_uuid,version FROM locality_auto_publication WHERE domain_uuid=`+s.sqlParam(1)+` ORDER BY node_kind,node_uuid LIMIT 129`, domain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -71,37 +70,45 @@ func autoRules(ctx context.Context, tx *sql.Tx, domain string) ([]autoPublicatio
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func requireManualPublication(ctx context.Context, tx *sql.Tx, domain, actor, kind, id string) error {
|
||||
scope, err := autoScope(ctx, tx, domain, kind, id)
|
||||
func (s *Store) requireManualPublication(ctx context.Context, tx *sql.Tx, domain, actor, kind, id string) error {
|
||||
scope, err := s.autoScope(ctx, tx, domain, kind, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO locality_auto_publication VALUES(?,?,?,'required',?,?,1) ON CONFLICT(domain_uuid,node_kind,node_uuid) DO UPDATE SET mode='required',scope_path=excluded.scope_path,actor_uuid=excluded.actor_uuid,version=version+1`, domain, kind, id, scope, actor)
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO locality_auto_publication VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,'required',`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,1) ON CONFLICT(domain_uuid,node_kind,node_uuid) DO UPDATE SET mode='required',scope_path=excluded.scope_path,actor_uuid=excluded.actor_uuid,version=locality_auto_publication.version+1`, domain, kind, id, scope, actor)
|
||||
return err
|
||||
}
|
||||
func (s *Store) AutoPublication(ctx context.Context, domain, actor, kind, id string, change *AutoPublicationChange) (AutoPublication, error) {
|
||||
func (s *Store) AutoPublication(ctx context.Context, domain, actor, kind, id string, change *AutoPublicationChange) (_ AutoPublication, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("AutoPublication", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
state := AutoPublication{Mode: "inherit"}
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
options := s.policyReadOptions()
|
||||
if change != nil {
|
||||
options = &sql.TxOptions{Isolation: sql.LevelReadCommitted}
|
||||
}
|
||||
tx, err := s.DB.BeginTx(ctx, options)
|
||||
if err != nil {
|
||||
return state, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if change != nil {
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO city_governance_epochs VALUES(?,0) ON CONFLICT(domain_uuid) DO UPDATE SET version=version`, domain); err != nil {
|
||||
if err = s.lockPolicyWrite(ctx, tx); err != nil {
|
||||
return state, err
|
||||
}
|
||||
if _, err = tx.ExecContext(ctx, `INSERT INTO city_governance_epochs VALUES(`+s.sqlParam(1)+`,0) ON CONFLICT(domain_uuid) DO UPDATE SET version=city_governance_epochs.version`, domain); err != nil {
|
||||
return state, err
|
||||
}
|
||||
}
|
||||
g, err := governanceSnapshot(ctx, tx, domain, actor, kind, id)
|
||||
g, err := s.governanceSnapshot(ctx, tx, domain, actor, kind, id)
|
||||
if err != nil {
|
||||
return state, err
|
||||
}
|
||||
scope, err := autoScope(ctx, tx, domain, kind, id)
|
||||
scope, err := s.autoScope(ctx, tx, domain, kind, id)
|
||||
if err != nil {
|
||||
return state, err
|
||||
}
|
||||
rules, err := autoRules(ctx, tx, domain)
|
||||
rules, err := s.autoRules(ctx, tx, domain)
|
||||
if err != nil {
|
||||
return state, err
|
||||
}
|
||||
@@ -113,7 +120,7 @@ func (s *Store) AutoPublication(ctx context.Context, domain, actor, kind, id str
|
||||
state.Mode, state.Version = rule.Mode, rule.Version
|
||||
state.Active = rule.Mode == "automatic" && rule.Scope == scope
|
||||
if state.Active && rule.Actor != actor {
|
||||
_, e := governanceSnapshot(ctx, tx, domain, rule.Actor, kind, id)
|
||||
_, e := s.governanceSnapshot(ctx, tx, domain, rule.Actor, kind, id)
|
||||
if errors.Is(e, sql.ErrNoRows) || errors.Is(e, ErrAccessDenied) {
|
||||
state.Active = false
|
||||
} else if e != nil {
|
||||
@@ -122,7 +129,7 @@ func (s *Store) AutoPublication(ctx context.Context, domain, actor, kind, id str
|
||||
}
|
||||
}
|
||||
if rule.Mode == "required" && !(rule.Kind == kind && rule.ID == id) {
|
||||
parent, e := autoScope(ctx, tx, domain, rule.Kind, rule.ID)
|
||||
parent, e := s.autoScope(ctx, tx, domain, rule.Kind, rule.ID)
|
||||
if e != nil && !errors.Is(e, sql.ErrNoRows) {
|
||||
return state, e
|
||||
}
|
||||
@@ -139,7 +146,7 @@ func (s *Store) AutoPublication(ctx context.Context, domain, actor, kind, id str
|
||||
return state, ErrGovernanceConflict
|
||||
}
|
||||
if change == nil || change.Mode != "required" {
|
||||
state.Impact, err = publicationImpact(ctx, tx, domain, actor, kind, id, g)
|
||||
state.Impact, err = s.publicationImpact(ctx, tx, domain, actor, kind, id, g)
|
||||
if err != nil {
|
||||
if change == nil && errors.Is(err, ErrPublicationImpact) {
|
||||
state.CanAutomatic = false
|
||||
@@ -184,7 +191,7 @@ func (s *Store) AutoPublication(ctx context.Context, domain, actor, kind, id str
|
||||
if !change.Confirm || change.ImpactToken == "" || change.ImpactToken != state.Impact.ImpactToken {
|
||||
return state, ErrPublicationImpact
|
||||
}
|
||||
if err = installPublicationGuards(ctx, tx); err != nil {
|
||||
if err = s.installPublicationGuards(ctx, tx); err != nil {
|
||||
return state, err
|
||||
}
|
||||
}
|
||||
@@ -192,7 +199,7 @@ func (s *Store) AutoPublication(ctx context.Context, domain, actor, kind, id str
|
||||
return state, ErrPublicationImpact
|
||||
}
|
||||
before, _ := json.Marshal(state)
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO locality_auto_publication VALUES(?,?,?,?,?,?,1) ON CONFLICT(domain_uuid,node_kind,node_uuid) DO UPDATE SET mode=excluded.mode,scope_path=excluded.scope_path,actor_uuid=excluded.actor_uuid,version=version+1`, domain, kind, id, change.Mode, scope, actor)
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO locality_auto_publication VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,1) ON CONFLICT(domain_uuid,node_kind,node_uuid) DO UPDATE SET mode=excluded.mode,scope_path=excluded.scope_path,actor_uuid=excluded.actor_uuid,version=locality_auto_publication.version+1`, domain, kind, id, change.Mode, scope, actor)
|
||||
if err != nil {
|
||||
return state, err
|
||||
}
|
||||
@@ -201,7 +208,7 @@ func (s *Store) AutoPublication(ctx context.Context, domain, actor, kind, id str
|
||||
state.Active = change.Mode == "automatic"
|
||||
state.Impact.ImpactToken = ""
|
||||
after, _ := json.Marshal(state)
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO city_governance_events(domain_uuid,epoch,actor_uuid,node_kind,node_uuid,before_json,after_json,created_at_ms) VALUES(?,?,?,?,?,?,?,?)`, domain, g.Epoch, actor, kind, id, string(before), string(after), time.Now().UnixMilli())
|
||||
_, err = tx.ExecContext(ctx, `INSERT INTO city_governance_events(domain_uuid,epoch,actor_uuid,node_kind,node_uuid,before_json,after_json,created_at_ms) VALUES(`+s.sqlParam(1)+`,`+s.sqlParam(2)+`,`+s.sqlParam(3)+`,`+s.sqlParam(4)+`,`+s.sqlParam(5)+`,`+s.sqlParam(6)+`,`+s.sqlParam(7)+`,`+s.sqlParam(8)+`)`, domain, g.Epoch, actor, kind, id, string(before), string(after), time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
return state, err
|
||||
}
|
||||
@@ -211,26 +218,31 @@ func (s *Store) AutoPublication(ctx context.Context, domain, actor, kind, id str
|
||||
// Evaluate live heads, audience, publisher authority and literal Path boundaries
|
||||
// in one read transaction. No request mutates approval manifests or source content.
|
||||
func (s *Store) automaticPublicPages(ctx context.Context, domain, onlyPath string) ([]PublishedNavigationPage, error) {
|
||||
tx, err := s.DB.BeginTx(ctx, &sql.TxOptions{ReadOnly: true})
|
||||
tx, err := s.DB.BeginTx(ctx, s.policyReadOptions())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
return s.automaticPublicPagesTx(ctx, tx, domain, onlyPath)
|
||||
}
|
||||
|
||||
func (s *Store) automaticPublicPagesTx(ctx context.Context, tx *sql.Tx, domain, onlyPath string) ([]PublishedNavigationPage, error) {
|
||||
var err error
|
||||
var live bool
|
||||
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domains d JOIN campuses c ON c.uuid=d.campus_uuid JOIN config cfg ON cfg.domain_uuid=d.uuid AND cfg.key='authorization_mode' AND cfg.value='enforcing' WHERE d.uuid=? AND d.enabled=1 AND c.enabled=1)`, domain).Scan(&live); err != nil {
|
||||
if err = tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM domains d JOIN campuses c ON c.uuid=d.campus_uuid JOIN config cfg ON cfg.domain_uuid=d.uuid AND cfg.key='authorization_mode' AND cfg.value='enforcing' WHERE d.uuid=`+s.sqlParam(1)+` AND d.enabled=1 AND c.enabled=1)`, domain).Scan(&live); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !live {
|
||||
return nil, nil
|
||||
}
|
||||
rules, err := autoRules(ctx, tx, domain)
|
||||
rules, err := s.autoRules(ctx, tx, domain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blocked := []string{}
|
||||
for _, rule := range rules {
|
||||
if rule.Mode == "required" {
|
||||
scope, e := autoScope(ctx, tx, domain, rule.Kind, rule.ID)
|
||||
scope, e := s.autoScope(ctx, tx, domain, rule.Kind, rule.ID)
|
||||
if errors.Is(e, sql.ErrNoRows) {
|
||||
continue
|
||||
}
|
||||
@@ -259,21 +271,21 @@ func (s *Store) automaticPublicPages(ctx context.Context, domain, onlyPath strin
|
||||
if onlyPath != "" && !autoContains(rule.Scope, onlyPath) {
|
||||
continue
|
||||
}
|
||||
g, e := governanceSnapshot(ctx, tx, domain, rule.Actor, rule.Kind, rule.ID)
|
||||
g, e := s.governanceSnapshot(ctx, tx, domain, rule.Actor, rule.Kind, rule.ID)
|
||||
if errors.Is(e, sql.ErrNoRows) || errors.Is(e, ErrAccessDenied) || errors.Is(e, ErrGovernanceInvalid) {
|
||||
continue
|
||||
}
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
current, e := autoScope(ctx, tx, domain, rule.Kind, rule.ID)
|
||||
current, e := s.autoScope(ctx, tx, domain, rule.Kind, rule.ID)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if current != rule.Scope || !g.DraftEffectivePublic || deny(current) {
|
||||
continue
|
||||
}
|
||||
p, e := publicationImpactBounded(ctx, tx, domain, rule.Actor, rule.Kind, rule.ID, g, MaxNavigationEntries, onlyPath)
|
||||
p, e := s.publicationImpactBounded(ctx, tx, domain, rule.Actor, rule.Kind, rule.ID, g, MaxNavigationEntries, onlyPath)
|
||||
if errors.Is(e, ErrAccessDenied) {
|
||||
continue
|
||||
}
|
||||
@@ -296,7 +308,7 @@ func (s *Store) automaticPublicPages(ctx context.Context, domain, onlyPath strin
|
||||
for id := range entries {
|
||||
args = append(args, id)
|
||||
}
|
||||
rows, err := tx.QueryContext(ctx, `SELECT uuid,object_uuid,document_hash,COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),created_at_ms FROM revisions WHERE uuid IN (`+strings.TrimSuffix(strings.Repeat("?,", len(args)), ",")+`) ORDER BY created_at_ms DESC,uuid`, args...)
|
||||
rows, err := tx.QueryContext(ctx, `SELECT uuid,object_uuid,document_hash,COALESCE(NULLIF(lang,''),'und'),COALESCE(NULLIF(content_type,''),'text/html; charset=utf-8'),COALESCE(filename,''),created_at_ms FROM revisions WHERE uuid IN (`+s.sqlValues(len(args))+`) ORDER BY created_at_ms DESC,uuid`, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -30,11 +31,14 @@ func scanBroadcast(scanner interface{ Scan(...any) error }, b *Broadcast) error
|
||||
return scanner.Scan(&b.UUID, &b.DomainUUID, &b.Title, &b.Body, &b.Severity, &b.StartsAtMS, &b.EndsAtMS, &b.CreatedAtMS, &b.UpdatedAtMS)
|
||||
}
|
||||
|
||||
func (s *Store) ListActiveBroadcasts(ctx context.Context, domain string, nowMS int64) ([]Broadcast, error) {
|
||||
func (s *Store) ListActiveBroadcasts(ctx context.Context, domain string, nowMS int64) (_ []Broadcast, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("active broadcasts", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
if nowMS == 0 {
|
||||
nowMS = time.Now().UnixMilli()
|
||||
}
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT `+broadcastColumns+` FROM broadcasts WHERE domain_uuid=? AND (starts_at_ms=0 OR starts_at_ms<=?) AND (ends_at_ms=0 OR ends_at_ms>?) ORDER BY severity DESC,updated_at_ms DESC LIMIT 20`, domain, nowMS, nowMS)
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT `+broadcastColumns+` FROM broadcasts WHERE domain_uuid=`+s.sqlParam(1)+` AND (starts_at_ms=0 OR starts_at_ms<=`+s.sqlParam(2)+`) AND (ends_at_ms=0 OR ends_at_ms>`+s.sqlParam(3)+`) ORDER BY severity DESC,updated_at_ms DESC LIMIT 20`, domain, nowMS, nowMS)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -49,8 +53,11 @@ func (s *Store) ListActiveBroadcasts(ctx context.Context, domain string, nowMS i
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (s *Store) ListBroadcasts(ctx context.Context, domain string) ([]Broadcast, error) {
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT `+broadcastColumns+` FROM broadcasts WHERE domain_uuid=? ORDER BY updated_at_ms DESC LIMIT 100`, domain)
|
||||
func (s *Store) ListBroadcasts(ctx context.Context, domain string) (_ []Broadcast, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("broadcast listing", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
rows, err := s.DB.QueryContext(ctx, `SELECT `+broadcastColumns+` FROM broadcasts WHERE domain_uuid=`+s.sqlParam(1)+` ORDER BY updated_at_ms DESC LIMIT 100`, domain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -65,7 +72,11 @@ func (s *Store) ListBroadcasts(ctx context.Context, domain string) ([]Broadcast,
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (s *Store) SaveBroadcast(b Broadcast) error {
|
||||
func (s *Store) SaveBroadcast(b Broadcast) (resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("broadcast save", resultErr) }()
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
b.DomainUUID = strings.TrimSpace(b.DomainUUID)
|
||||
b.Title = strings.TrimSpace(b.Title)
|
||||
b.Body = strings.TrimSpace(b.Body)
|
||||
@@ -80,26 +91,55 @@ func (s *Store) SaveBroadcast(b Broadcast) error {
|
||||
b.CreatedAtMS = now
|
||||
}
|
||||
b.UpdatedAtMS = now
|
||||
_, err := s.DB.Exec(`INSERT INTO broadcasts(`+broadcastColumns+`) VALUES(?,?,?,?,?,?,?,?,?) ON CONFLICT(uuid) DO UPDATE SET title=excluded.title,body=excluded.body,severity=excluded.severity,starts_at_ms=excluded.starts_at_ms,ends_at_ms=excluded.ends_at_ms,updated_at_ms=excluded.updated_at_ms`, b.UUID, b.DomainUUID, b.Title, b.Body, b.Severity, b.StartsAtMS, b.EndsAtMS, b.CreatedAtMS, b.UpdatedAtMS)
|
||||
return err
|
||||
}
|
||||
func (s *Store) DeleteBroadcast(domain, id string) error {
|
||||
tx, err := s.DB.Begin()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if _, err = tx.Exec(`DELETE FROM notification_inbox WHERE domain_uuid=? AND event_uuid=? AND kind='broadcast'`, domain, id); err != nil {
|
||||
if err = s.lockTaskWriter(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(`DELETE FROM broadcasts WHERE domain_uuid=? AND uuid=?`, domain, id); err != nil {
|
||||
result, err := tx.ExecContext(ctx, `INSERT INTO broadcasts(`+broadcastColumns+`) VALUES(`+s.sqlValues(9)+`) ON CONFLICT(uuid) DO UPDATE SET title=excluded.title,body=excluded.body,severity=excluded.severity,starts_at_ms=excluded.starts_at_ms,ends_at_ms=excluded.ends_at_ms,updated_at_ms=excluded.updated_at_ms WHERE broadcasts.domain_uuid=excluded.domain_uuid`, b.UUID, b.DomainUUID, b.Title, b.Body, b.Severity, b.StartsAtMS, b.EndsAtMS, b.CreatedAtMS, b.UpdatedAtMS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if n != 1 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
func (s *Store) DeleteBroadcast(domain, id string) (resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("broadcast deletion", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.DB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err = s.lockTaskWriter(ctx, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(`DELETE FROM notification_inbox WHERE domain_uuid=`+s.sqlParam(1)+` AND event_uuid=`+s.sqlParam(2)+` AND kind='broadcast'`, domain, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(`DELETE FROM broadcasts WHERE domain_uuid=`+s.sqlParam(1)+` AND uuid=`+s.sqlParam(2)+``, domain, id); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Store) GetBroadcast(ctx context.Context, domain, id string) (Broadcast, error) {
|
||||
func (s *Store) GetBroadcast(ctx context.Context, domain, id string) (_ Broadcast, resultErr error) {
|
||||
defer func() { resultErr = s.sqlError("broadcast lookup", resultErr) }()
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
var b Broadcast
|
||||
err := scanBroadcast(s.DB.QueryRowContext(ctx, `SELECT `+broadcastColumns+` FROM broadcasts WHERE domain_uuid=? AND uuid=?`, domain, id), &b)
|
||||
err := scanBroadcast(s.DB.QueryRowContext(ctx, `SELECT `+broadcastColumns+` FROM broadcasts WHERE domain_uuid=`+s.sqlParam(1)+` AND uuid=`+s.sqlParam(2)+``, domain, id), &b)
|
||||
return b, err
|
||||
}
|
||||
+61
-14
@@ -1,11 +1,37 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"pageswiki/internal/mestionora"
|
||||
)
|
||||
|
||||
func (s *Store) fillNativeBrowserTitles(entries []BrowserEntry) error {
|
||||
if s.sqlBackend != storePostgreSQL {
|
||||
return nil
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), cityReadTimeout)
|
||||
defer cancel()
|
||||
budget := int64(32 << 20)
|
||||
for i := range entries {
|
||||
e := &entries[i]
|
||||
if e.IsDir || e.DocumentHash == "" || !strings.HasPrefix(strings.ToLower(e.ContentType), "text/html") {
|
||||
continue
|
||||
}
|
||||
if budget < 65536 {
|
||||
return ErrNavigationBudget
|
||||
}
|
||||
window, err := s.ReadDocumentWindowContext(ctx, e.DocumentHash, 0, 65536)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
budget -= int64(len(window.Data))
|
||||
e.Title = firstHTMLHeading(string(window.Data))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type BrowserEntry struct {
|
||||
Name string
|
||||
Path string
|
||||
@@ -46,34 +72,43 @@ func (s *Store) ListBrowserEntries(parent string) ([]BrowserEntry, error) {
|
||||
}
|
||||
|
||||
func (s *Store) ListBrowserEntriesInDomain(domainUUID, parent string) ([]BrowserEntry, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), cityReadTimeout)
|
||||
defer cancel()
|
||||
parent = normalizeBrowserPath(parent)
|
||||
prefix := strings.TrimRight(parent, "/") + "/"
|
||||
// Collapse descendants before consulting revisions. A folder listing must
|
||||
// not rank every revision of every document in the subtree. Binary ranges
|
||||
// keep prefix matching literal (including %, _) and use the Path index.
|
||||
rows, err := s.DB.Query(`
|
||||
pos := s.sqlStringPosition("name", "/")
|
||||
byteSize := "length(CAST(r.body AS BLOB))"
|
||||
tie := "rowid"
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
byteSize = "octet_length(r.body)"
|
||||
tie = "uuid"
|
||||
}
|
||||
rows, err := s.DB.QueryContext(ctx, `
|
||||
WITH relative AS (
|
||||
SELECT substr(path, length(?) + 1) AS name, object_uuid
|
||||
FROM paths WHERE domain_uuid=? AND path >= ? AND path < ? AND path != ?
|
||||
SELECT substr(path, length(`+s.sqlParam(1)+`) + 1) AS name, object_uuid
|
||||
FROM paths WHERE domain_uuid=`+s.sqlParam(2)+` AND path >= `+s.sqlParam(3)+` AND path < `+s.sqlParam(4)+` AND path != `+s.sqlParam(5)+`
|
||||
), children AS (
|
||||
SELECT CASE WHEN instr(name,'/')=0 THEN name ELSE substr(name,1,instr(name,'/')-1) END AS name,
|
||||
MAX(CASE WHEN instr(name,'/')=0 THEN object_uuid ELSE '' END) AS object_uuid,
|
||||
MAX(instr(name,'/')>0) AS is_dir,
|
||||
SUM(instr(name,'/')>0) AS child_count
|
||||
SELECT CASE WHEN `+pos+`=0 THEN name ELSE substr(name,1,`+pos+`-1) END AS name,
|
||||
MAX(CASE WHEN `+pos+`=0 THEN object_uuid ELSE '' END) AS object_uuid,
|
||||
MAX(CASE WHEN `+pos+`>0 THEN 1 ELSE 0 END) AS is_dir,
|
||||
SUM(CASE WHEN `+pos+`>0 THEN 1 ELSE 0 END) AS child_count
|
||||
FROM relative WHERE name != '' GROUP BY 1
|
||||
)
|
||||
SELECT c.name,c.object_uuid,c.is_dir,c.child_count,
|
||||
COALESCE(length(CAST(r.body AS BLOB)),0),COALESCE(r.created_at,0),
|
||||
COALESCE(`+byteSize+`,0),COALESCE(r.created_at,0),
|
||||
COALESCE(r.content_type,''),COALESCE(r.filename,''),
|
||||
COALESCE(r.document_hash,''),COALESCE(r.uuid,''),
|
||||
COALESCE(substr(CAST(r.body AS TEXT),1,65536),'')
|
||||
FROM children c LEFT JOIN revisions r ON r.rowid=(
|
||||
SELECT head.rowid FROM revisions head
|
||||
FROM children c LEFT JOIN revisions r ON r.`+tie+`=(
|
||||
SELECT head.`+tie+` FROM revisions head
|
||||
WHERE c.is_dir=0 AND head.object_uuid=c.object_uuid
|
||||
ORDER BY head.created_at_ms DESC, head.rowid DESC LIMIT 1
|
||||
ORDER BY head.created_at_ms DESC, head.`+tie+` DESC LIMIT 1
|
||||
)
|
||||
ORDER BY c.is_dir DESC,c.name ASC
|
||||
`, prefix, domainUUID, prefix, strings.TrimSuffix(prefix, "/")+"0", parent)
|
||||
ORDER BY c.is_dir DESC,c.name ASC LIMIT `+s.sqlParam(6)+`
|
||||
`, prefix, domainUUID, prefix, strings.TrimSuffix(prefix, "/")+"0", parent, MaxNavigationEntries+1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -95,6 +130,18 @@ func (s *Store) ListBrowserEntriesInDomain(domainUUID, parent string) ([]Browser
|
||||
}
|
||||
}
|
||||
out = append(out, entry)
|
||||
if len(out) > MaxNavigationEntries {
|
||||
return nil, ErrNavigationBudget
|
||||
}
|
||||
}
|
||||
return out, rows.Err()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.fillNativeBrowserTitles(out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -1,11 +1,14 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"pageswiki/internal/db"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
@@ -23,13 +26,18 @@ type BrowserNavigationLink struct {
|
||||
}
|
||||
|
||||
func (s *Store) EnsureBrowserNavigationSchema() error {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
return db.ApplyPostgreSQLMigrations(context.Background(), s.DB, "store-browser-navigation", []db.PostgreSQLMigration{{Version: 1, Name: "browser navigator links", Statements: []string{
|
||||
`CREATE TABLE browser_navigation_links(uuid TEXT COLLATE "C" PRIMARY KEY,domain_uuid TEXT COLLATE "C" NOT NULL,principal_uuid TEXT COLLATE "C" NOT NULL,label TEXT NOT NULL,path TEXT COLLATE "C" NOT NULL,is_directory INTEGER NOT NULL DEFAULT 0,position BIGINT NOT NULL DEFAULT 0,created_at_ms BIGINT NOT NULL,updated_at_ms BIGINT NOT NULL,UNIQUE(domain_uuid,principal_uuid,path))`,
|
||||
}, Assertions: []string{`SELECT uuid,domain_uuid,principal_uuid,label,path,is_directory,position,created_at_ms,updated_at_ms FROM browser_navigation_links WHERE 1=0`}}})
|
||||
}
|
||||
return s.ensureSchemaComponent("browser-navigation")
|
||||
}
|
||||
|
||||
func (s *Store) ListBrowserNavigationLinks(domainUUID, principalUUID string) ([]BrowserNavigationLink, error) {
|
||||
rows, err := s.DB.Query(`SELECT uuid,label,path,is_directory,position,updated_at_ms
|
||||
FROM browser_navigation_links
|
||||
WHERE domain_uuid=? AND principal_uuid=?
|
||||
WHERE domain_uuid=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2)+`
|
||||
ORDER BY position,created_at_ms,uuid`, domainUUID, principalUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -52,7 +60,8 @@ func (s *Store) AddBrowserNavigationLink(domainUUID, principalUUID, label, path
|
||||
if domainUUID == "" || principalUUID == "" || label == "" || len(label) > 120 || path == "/" || len(path) > 2048 {
|
||||
return nil, errors.New("invalid browser navigator link")
|
||||
}
|
||||
tx, err := s.DB.Begin()
|
||||
tx, cancel, err := s.beginPathMutation(domainUUID)
|
||||
defer cancel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -60,7 +69,7 @@ func (s *Store) AddBrowserNavigationLink(domainUUID, principalUUID, label, path
|
||||
var link BrowserNavigationLink
|
||||
err = tx.QueryRow(`SELECT uuid,label,path,is_directory,position,updated_at_ms
|
||||
FROM browser_navigation_links
|
||||
WHERE domain_uuid=? AND principal_uuid=? AND path=?`, domainUUID, principalUUID, path).
|
||||
WHERE domain_uuid=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2)+` AND path=`+s.sqlParam(3), domainUUID, principalUUID, path).
|
||||
Scan(&link.UUID, &link.Label, &link.Path, &link.IsDir, &link.Position, &link.UpdatedAt)
|
||||
if err == nil {
|
||||
return &link, nil
|
||||
@@ -70,7 +79,7 @@ func (s *Store) AddBrowserNavigationLink(domainUUID, principalUUID, label, path
|
||||
}
|
||||
var count, position int
|
||||
if err := tx.QueryRow(`SELECT COUNT(*),COALESCE(MAX(position),-1)+1
|
||||
FROM browser_navigation_links WHERE domain_uuid=? AND principal_uuid=?`, domainUUID, principalUUID).
|
||||
FROM browser_navigation_links WHERE domain_uuid=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2), domainUUID, principalUUID).
|
||||
Scan(&count, &position); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -81,7 +90,7 @@ func (s *Store) AddBrowserNavigationLink(domainUUID, principalUUID, label, path
|
||||
link = BrowserNavigationLink{UUID: uuid.NewString(), Label: label, Path: path, IsDir: isDir, Position: position, UpdatedAt: now}
|
||||
if _, err := tx.Exec(`INSERT INTO browser_navigation_links
|
||||
(uuid,domain_uuid,principal_uuid,label,path,is_directory,position,created_at_ms,updated_at_ms)
|
||||
VALUES(?,?,?,?,?,?,?,?,?)`, link.UUID, domainUUID, principalUUID, link.Label, link.Path, link.IsDir, link.Position, now, now); err != nil {
|
||||
VALUES(`+s.sqlValues(9)+`)`, link.UUID, domainUUID, principalUUID, link.Label, link.Path, boolToInt(link.IsDir), link.Position, now, now); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
@@ -95,8 +104,11 @@ func (s *Store) RenameBrowserNavigationLink(domainUUID, principalUUID, id, label
|
||||
if label == "" || len(label) > 120 {
|
||||
return errors.New("invalid browser navigator label")
|
||||
}
|
||||
result, err := s.DB.Exec(`UPDATE browser_navigation_links SET label=?,updated_at_ms=?
|
||||
WHERE uuid=? AND domain_uuid=? AND principal_uuid=?`, label, time.Now().UnixMilli(), id, domainUUID, principalUUID)
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
result, err := s.DB.Exec(`UPDATE browser_navigation_links SET label=`+s.sqlParam(1)+`,updated_at_ms=`+s.sqlParam(2)+`
|
||||
WHERE uuid=`+s.sqlParam(3)+` AND domain_uuid=`+s.sqlParam(4)+` AND principal_uuid=`+s.sqlParam(5), label, time.Now().UnixMilli(), id, domainUUID, principalUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -108,8 +120,11 @@ func (s *Store) RenameBrowserNavigationLink(domainUUID, principalUUID, id, label
|
||||
}
|
||||
|
||||
func (s *Store) DeleteBrowserNavigationLink(domainUUID, principalUUID, id string) error {
|
||||
if s.ReadOnly {
|
||||
return ErrReadOnly
|
||||
}
|
||||
result, err := s.DB.Exec(`DELETE FROM browser_navigation_links
|
||||
WHERE uuid=? AND domain_uuid=? AND principal_uuid=?`, id, domainUUID, principalUUID)
|
||||
WHERE uuid=`+s.sqlParam(1)+` AND domain_uuid=`+s.sqlParam(2)+` AND principal_uuid=`+s.sqlParam(3), id, domainUUID, principalUUID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -123,19 +138,26 @@ func (s *Store) DeleteBrowserNavigationLink(domainUUID, principalUUID, id string
|
||||
// renameBrowserNavigationPathsTx keeps a user's saved navigator targets aligned
|
||||
// with semantic Path mutations. If the destination was already saved, the
|
||||
// existing destination link wins and the now-duplicate source link is removed.
|
||||
func renameBrowserNavigationPathsTx(tx *sql.Tx, domainUUID, oldPath, newPath string, recursive bool) error {
|
||||
func (s *Store) renameBrowserNavigationPathsTx(tx *sql.Tx, domainUUID, oldPath, newPath string, recursive bool) error {
|
||||
var tableExists bool
|
||||
if err := tx.QueryRow(`SELECT EXISTS(
|
||||
existsSQL := `SELECT EXISTS(
|
||||
SELECT 1 FROM sqlite_master WHERE type='table' AND name='browser_navigation_links'
|
||||
)`).Scan(&tableExists); err != nil {
|
||||
)`
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
existsSQL = `SELECT to_regclass('browser_navigation_links') IS NOT NULL`
|
||||
}
|
||||
if err := tx.QueryRow(existsSQL).Scan(&tableExists); err != nil {
|
||||
return err
|
||||
}
|
||||
if !tableExists {
|
||||
if s.sqlBackend == storePostgreSQL {
|
||||
return ErrPostgreSQLComponentNotReady
|
||||
}
|
||||
return nil
|
||||
}
|
||||
rows, err := tx.Query(`SELECT uuid,principal_uuid,path
|
||||
FROM browser_navigation_links
|
||||
WHERE domain_uuid=? AND (path=? OR (? AND path>=? AND path<?))
|
||||
WHERE domain_uuid=`+s.sqlParam(1)+` AND (path=`+s.sqlParam(2)+` OR (`+s.sqlParam(3)+` AND path>=`+s.sqlParam(4)+` AND path<`+s.sqlParam(5)+`))
|
||||
ORDER BY length(path),path`, domainUUID, oldPath, recursive, oldPath+"/", oldPath+"0")
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -162,9 +184,9 @@ func renameBrowserNavigationPathsTx(tx *sql.Tx, domainUUID, oldPath, newPath str
|
||||
for _, item := range targets {
|
||||
var existing string
|
||||
err := tx.QueryRow(`SELECT uuid FROM browser_navigation_links
|
||||
WHERE domain_uuid=? AND principal_uuid=? AND path=?`, domainUUID, item.principal, item.next).Scan(&existing)
|
||||
WHERE domain_uuid=`+s.sqlParam(1)+` AND principal_uuid=`+s.sqlParam(2)+` AND path=`+s.sqlParam(3), domainUUID, item.principal, item.next).Scan(&existing)
|
||||
if err == nil && existing != item.id {
|
||||
if _, err := tx.Exec(`DELETE FROM browser_navigation_links WHERE uuid=?`, item.id); err != nil {
|
||||
if _, err := tx.Exec(`DELETE FROM browser_navigation_links WHERE uuid=`+s.sqlParam(1), item.id); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
@@ -172,7 +194,7 @@ func renameBrowserNavigationPathsTx(tx *sql.Tx, domainUUID, oldPath, newPath str
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
if _, err := tx.Exec(`UPDATE browser_navigation_links SET path=?,updated_at_ms=? WHERE uuid=?`, item.next, now, item.id); err != nil {
|
||||
if _, err := tx.Exec(`UPDATE browser_navigation_links SET path=`+s.sqlParam(1)+`,updated_at_ms=`+s.sqlParam(2)+` WHERE uuid=`+s.sqlParam(3), item.next, now, item.id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
Loaded 100 of 243 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user