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.
144 lines
7.4 KiB
Go
144 lines
7.4 KiB
Go
package store
|
|
|
|
import (
|
|
"errors"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
func newSchemaTestStore(t *testing.T) *Store {
|
|
t.Helper()
|
|
database, err := db.Open(filepath.Join(t.TempDir(), "schema.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { database.Close() })
|
|
return &Store{DB: database}
|
|
}
|
|
|
|
func TestManagedSchemasFreshRepeatAndReadOnly(t *testing.T) {
|
|
for component := range managedStoreSchemas {
|
|
t.Run(component, func(t *testing.T) {
|
|
st := newSchemaTestStore(t)
|
|
if err := st.ensureSchemaComponent(component); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.ensureSchemaComponent(component); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var count int
|
|
if err := st.DB.QueryRow(`SELECT count(*) FROM schema_migration_steps WHERE component=?`, component).Scan(&count); err != nil || count != len(managedStoreSchemas[component]) {
|
|
t.Fatalf("ledger=%d err=%v", count, err)
|
|
}
|
|
st.ReadOnly = true
|
|
if err := st.ensureSchemaComponent(component); !errors.Is(err, ErrReadOnly) {
|
|
t.Fatalf("standby migration: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestManagedSchemasAdoptLegacyRowsWithoutRecreatingTables(t *testing.T) {
|
|
for _, tc := range []struct{ component, ddl, insert, query, want string }{
|
|
{"browser-navigation", `CREATE TABLE browser_navigation_links(uuid TEXT PRIMARY KEY,domain_uuid TEXT NOT NULL,principal_uuid TEXT NOT NULL,label TEXT NOT NULL,path TEXT NOT NULL,position INTEGER NOT NULL DEFAULT 0,created_at_ms INTEGER NOT NULL,updated_at_ms INTEGER NOT NULL,UNIQUE(domain_uuid,principal_uuid,path))`,
|
|
`INSERT INTO browser_navigation_links VALUES('u','d','p','keep','/page',3,10,20)`, `SELECT label||':'||position||':'||is_directory FROM browser_navigation_links WHERE uuid='u'`, "keep:3:0"},
|
|
{"adapter-sessions", `CREATE TABLE adapter_sessions(ticket_hash TEXT PRIMARY KEY,session_hash TEXT NOT NULL DEFAULT '',domain_uuid TEXT NOT NULL,plugin_id TEXT NOT NULL,object_uuid TEXT NOT NULL,base_revision_uuid TEXT NOT NULL,ticket_expires_at_ms INTEGER NOT NULL,session_expires_at_ms INTEGER NOT NULL DEFAULT 0,exchanged_at_ms INTEGER NOT NULL DEFAULT 0,created_at_ms INTEGER NOT NULL)`,
|
|
`INSERT INTO adapter_sessions VALUES('ticket','session','d','code','o','r',100,200,50,10)`, `SELECT session_hash||':'||root_path||':'||principal_uuid FROM adapter_sessions WHERE ticket_hash='ticket'`, "session::"},
|
|
{"imports", `CREATE TABLE import_journal(session_uuid TEXT NOT NULL,sequence INTEGER NOT NULL,path TEXT NOT NULL,object_uuid TEXT NOT NULL,created_at_ms INTEGER NOT NULL,PRIMARY KEY(session_uuid,sequence))`,
|
|
`INSERT INTO import_journal VALUES('s',2,'/keep','o',20)`, `SELECT path||':'||completed FROM import_journal WHERE session_uuid='s' AND sequence=2`, "/keep:0"},
|
|
{"resource-history", `CREATE TABLE resource_samples(id INTEGER PRIMARY KEY,job_uuid TEXT NOT NULL UNIQUE,timestamp_ms INTEGER NOT NULL,payload TEXT NOT NULL)`,
|
|
`INSERT INTO resource_samples VALUES(9,'j',100,'keep')`, `SELECT id||':'||payload FROM resource_samples WHERE job_uuid='j'`, "9:keep"},
|
|
{"projection-metadata", `CREATE TABLE projection_metadata(document_hash TEXT NOT NULL,kind TEXT NOT NULL,version INTEGER NOT NULL,payload TEXT NOT NULL DEFAULT '{}',error TEXT NOT NULL DEFAULT '',updated_at_ms INTEGER NOT NULL,retry_at_ms INTEGER NOT NULL DEFAULT 0,PRIMARY KEY(document_hash,kind,version))`,
|
|
`INSERT INTO projection_metadata VALUES('h','pdf',1,'keep','',10,0)`, `SELECT payload FROM projection_metadata WHERE document_hash='h'`, "keep"},
|
|
{"broadcasts", `CREATE TABLE broadcasts(uuid TEXT PRIMARY KEY,domain_uuid TEXT NOT NULL,title TEXT NOT NULL,body TEXT NOT NULL,severity TEXT NOT NULL DEFAULT 'info' CHECK(severity IN ('info','success','warning','critical')),starts_at_ms INTEGER NOT NULL DEFAULT 0,ends_at_ms INTEGER NOT NULL DEFAULT 0,created_at_ms INTEGER NOT NULL,updated_at_ms INTEGER NOT NULL)`,
|
|
`INSERT INTO broadcasts VALUES('u','d','title','keep','info',0,0,10,20)`, `SELECT body FROM broadcasts WHERE uuid='u'`, "keep"},
|
|
} {
|
|
t.Run(tc.component, func(t *testing.T) {
|
|
st := newSchemaTestStore(t)
|
|
for _, statement := range []string{tc.ddl, tc.insert} {
|
|
if _, err := st.DB.Exec(statement); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := st.ensureSchemaComponent(tc.component); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var value string
|
|
if err := st.DB.QueryRow(tc.query).Scan(&value); err != nil || value != tc.want {
|
|
t.Fatalf("row=%q want=%q err=%v", value, tc.want, err)
|
|
}
|
|
if err := st.ensureSchemaComponent(tc.component); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestManagedSchemasRejectBrokenAdoptionAndLaterDrift(t *testing.T) {
|
|
st := newSchemaTestStore(t)
|
|
// Matching columns without the conflict-target uniqueness is not sufficient.
|
|
if _, err := st.DB.Exec(`CREATE TABLE resource_samples(id INTEGER PRIMARY KEY,job_uuid TEXT NOT NULL,timestamp_ms INTEGER NOT NULL,payload TEXT NOT NULL); INSERT INTO resource_samples VALUES(1,'j',10,'keep')`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.EnsureResourceHistorySchema(); err == nil {
|
|
t.Fatal("missing unique constraint accepted")
|
|
}
|
|
var payload string
|
|
if err := st.DB.QueryRow(`SELECT payload FROM resource_samples WHERE id=1`).Scan(&payload); err != nil || payload != "keep" {
|
|
t.Fatal("failed adoption modified data", err)
|
|
}
|
|
var count int
|
|
if err := st.DB.QueryRow(`SELECT count(*) FROM sqlite_master WHERE name='idx_resource_samples_time'`).Scan(&count); err != nil || count != 0 {
|
|
t.Fatal("failed adoption left partial DDL", err)
|
|
}
|
|
if _, err := st.DB.Exec(`CREATE UNIQUE INDEX restored_resource_unique ON resource_samples(job_uuid)`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.EnsureResourceHistorySchema(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := st.DB.Exec(`DROP INDEX idx_resource_samples_time`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.EnsureResourceHistorySchema(); err == nil {
|
|
t.Fatal("ledger masked missing index")
|
|
}
|
|
if err := st.EnsureAdapterSessionsSchema(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := st.DB.Exec(`DROP INDEX idx_adapter_session_hash; CREATE UNIQUE INDEX idx_adapter_session_hash ON adapter_sessions(session_hash) WHERE session_hash = ''`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.EnsureAdapterSessionsSchema(); err == nil {
|
|
t.Fatal("incorrect partial index predicate accepted")
|
|
}
|
|
}
|
|
|
|
func TestEventJobsAdoptLegacyDueIndex(t *testing.T) {
|
|
st := newSchemaTestStore(t)
|
|
// The deployed pre-ledger scheduler used the same index name without priority.
|
|
for _, statement := range []string{
|
|
eventJobMigrations[0].Statements[0],
|
|
`CREATE INDEX idx_event_jobs_due ON event_jobs(status,run_at_ms,created_at_ms)`,
|
|
`INSERT INTO event_jobs(uuid,event_type,event_key,payload,status,attempts,run_at_ms,created_at_ms,updated_at_ms) VALUES('keep','test','key','unchanged','running',2,10,20,30)`,
|
|
} {
|
|
if _, err := st.DB.Exec(statement); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for range 2 {
|
|
if err := st.ensureSchemaComponent("event-jobs"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var columns, row string
|
|
if err := st.DB.QueryRow(`SELECT group_concat(name,',') FROM (SELECT name FROM pragma_index_info('idx_event_jobs_due') ORDER BY seqno)`).Scan(&columns); err != nil || columns != "status,priority,run_at_ms,created_at_ms" {
|
|
t.Fatalf("index=%q err=%v", columns, err)
|
|
}
|
|
if err := st.DB.QueryRow(`SELECT payload||':'||status||':'||attempts||':'||run_at_ms||':'||created_at_ms||':'||updated_at_ms FROM event_jobs WHERE uuid='keep'`).Scan(&row); err != nil || row != "unchanged:running:2:10:20:30" {
|
|
t.Fatalf("adoption changed job: %q err=%v", row, err)
|
|
}
|
|
}
|