Files
pages/internal/db/migration_inspect_test.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts.

Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation.

Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
2026-11-01 15:23:31 +09:00

122 lines
4.6 KiB
Go

package db
import (
"context"
"errors"
"path/filepath"
"testing"
)
func TestInspectMigrationsReadOnlyLifecycle(t *testing.T) {
path := filepath.Join(t.TempDir(), "inspect.db")
writer, err := Open(path)
if err != nil {
t.Fatal(err)
}
defer writer.Close()
if err := writer.Ping(); err != nil {
t.Fatal(err)
}
reader, err := OpenReadOnly(path)
if err != nil {
t.Fatal(err)
}
defer reader.Close()
steps := []SQLMigration{{Version: 1, Name: "initial", Statements: []string{`CREATE TABLE sample(id INTEGER PRIMARY KEY, value TEXT)`, `INSERT INTO sample VALUES(1,'keep')`}, Assertions: []string{`SELECT id,value FROM sample LIMIT 0`}}}
plans := map[string][]SQLMigration{"sample": steps}
inspect := func(state string, issues bool) {
t.Helper()
got, err := InspectSQLiteMigrations(context.Background(), reader, plans)
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].State != state || (len(got[0].Issues) > 0) != issues || !got[0].ContractChecked {
t.Fatalf("report: %+v", got)
}
}
inspect("pending", true)
var count int
if err := writer.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name IN ('sample','schema_migration_steps')`).Scan(&count); err != nil || count != 0 {
t.Fatalf("inspection created tables: %d %v", count, err)
}
if _, err := reader.Exec(`CREATE TABLE forbidden(id INTEGER)`); err == nil {
t.Fatal("connection is writable")
}
// Keep the writer open: the reader must see committed WAL contents too.
if err := ApplySQLiteMigrations(context.Background(), writer, "sample", steps); err != nil {
t.Fatal(err)
}
inspect("current", false)
if _, err := writer.Exec(`ALTER TABLE sample RENAME COLUMN value TO lost`); err != nil {
t.Fatal(err)
}
inspect("blocked", true)
var value string
if err := writer.QueryRow(`SELECT lost FROM sample WHERE id=1`).Scan(&value); err != nil || value != "keep" {
t.Fatalf("inspection changed data: %q %v", value, err)
}
ctx, cancel := context.WithCancel(context.Background())
cancel()
if _, err := InspectSQLiteMigrations(ctx, reader, plans); !errors.Is(err, context.Canceled) {
t.Fatalf("cancel: %v", err)
}
}
func TestInspectMigrationsRejectHistoryMismatch(t *testing.T) {
for _, tc := range []struct{ name, mutation string }{
{"checksum", `UPDATE schema_migration_steps SET checksum='changed'`},
{"name", `UPDATE schema_migration_steps SET name='changed'`},
{"zero", `UPDATE schema_migration_steps SET version=0`},
{"negative", `UPDATE schema_migration_steps SET version=-1`},
{"newer", `UPDATE schema_migration_steps SET version=2`},
} {
t.Run(tc.name, func(t *testing.T) {
database, err := Open(filepath.Join(t.TempDir(), "inspect.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
steps := []SQLMigration{{Version: 1, Name: "initial", Statements: []string{`CREATE TABLE sample(id INTEGER)`}}}
if err := ApplySQLiteMigrations(context.Background(), database, "sample", steps); err != nil {
t.Fatal(err)
}
if _, err := database.Exec(tc.mutation); err != nil {
t.Fatal(err)
}
got, err := InspectSQLiteMigrations(context.Background(), database, map[string][]SQLMigration{"sample": steps})
if err != nil || len(got) != 1 || got[0].State != "blocked" {
t.Fatalf("report=%+v err=%v", got, err)
}
})
}
}
func TestInspectMigrationsUnknownAndPendingPlan(t *testing.T) {
database, err := Open(filepath.Join(t.TempDir(), "inspect.db"))
if err != nil {
t.Fatal(err)
}
defer database.Close()
steps := []SQLMigration{{Version: 1, Name: "initial", Statements: []string{`CREATE TABLE sample(id INTEGER)`}}}
if err := ApplySQLiteMigrations(context.Background(), database, "sample", steps); err != nil {
t.Fatal(err)
}
got, err := InspectSQLiteMigrations(context.Background(), database, nil)
if err != nil || len(got) != 1 || got[0].State != "unknown" {
t.Fatalf("unknown=%+v %v", got, err)
}
steps = append(steps, SQLMigration{Version: 2, Name: "add_value", AddColumns: []SQLiteColumn{{Table: "sample", Name: "value", Declaration: "TEXT NOT NULL DEFAULT ''"}}, Assertions: []string{`SELECT value FROM sample LIMIT 0`}})
got, err = InspectSQLiteMigrations(context.Background(), database, map[string][]SQLMigration{"sample": steps})
if err != nil || len(got) != 1 {
t.Fatalf("pending=%+v %v", got, err)
}
item := got[0]
if item.State != "pending" || item.AppliedVersion != 1 || item.LatestVersion != 2 || len(item.Pending) != 1 || len(item.Pending[0].AddColumns) != 1 || len(item.Issues) == 0 {
t.Fatalf("pending=%+v", item)
}
var count int
if err := database.QueryRow(`SELECT count(*) FROM pragma_table_info('sample') WHERE name='value'`).Scan(&count); err != nil || count != 0 {
t.Fatalf("pending DDL applied: %d %v", count, err)
}
}