Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming. Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
108 lines
3.1 KiB
Go
108 lines
3.1 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/db"
|
|
)
|
|
|
|
type recordingPathQuery struct {
|
|
db *sql.DB
|
|
query string
|
|
args []any
|
|
}
|
|
|
|
func (q *recordingPathQuery) Query(query string, args ...any) (*sql.Rows, error) {
|
|
q.query, q.args = query, args
|
|
return q.db.Query(query, args...)
|
|
}
|
|
|
|
func TestPathMutationEnumerationUsesIndexedRange(t *testing.T) {
|
|
database, err := db.Open(t.TempDir() + "/query.db")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer database.Close()
|
|
if _, err := database.Exec(`CREATE TABLE paths(domain_uuid TEXT, path TEXT, object_uuid TEXT, created_at INTEGER, is_primary INTEGER,
|
|
PRIMARY KEY(domain_uuid,path))`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
query := &recordingPathQuery{db: database}
|
|
if _, err := listMutationPaths(query, "domain", "/a%_", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rows, err := database.Query("EXPLAIN QUERY PLAN "+query.query, query.args...)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer rows.Close()
|
|
var plan []string
|
|
for rows.Next() {
|
|
var id, parent, unused int
|
|
var detail string
|
|
if err := rows.Scan(&id, &parent, &unused, &detail); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
plan = append(plan, detail)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
joined := strings.Join(plan, "\n")
|
|
if !strings.Contains(joined, "domain_uuid=? AND path>?") || strings.Contains(joined, "SCAN paths") || strings.Contains(joined, "USE TEMP B-TREE") {
|
|
t.Fatalf("mutation query no longer bounded by an ordered index range:\n%s", joined)
|
|
}
|
|
}
|
|
|
|
func TestPathSnapshotSQLiteConcurrentWriteCannotUpgrade(t *testing.T) {
|
|
filename := t.TempDir() + "/concurrent.db"
|
|
database, err := db.Open(filename)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer database.Close()
|
|
if _, err := database.Exec(`CREATE TABLE paths(domain_uuid TEXT, path TEXT, object_uuid TEXT, created_at INTEGER, is_primary INTEGER,
|
|
PRIMARY KEY(domain_uuid,path)); INSERT INTO paths VALUES('d','/source/checked','first',1,0)`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := &Store{DB: database}
|
|
snapshot, err := st.SnapshotPathsInDomain("d", "/source", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
tx, err := database.Begin()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer tx.Rollback()
|
|
bindings, err := listMutationPaths(tx, "d", "/source", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := snapshot.validate("d", "/source", true, bindings); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// This interleaving occurs after in-transaction revalidation, exercising
|
|
// the SQLite read-snapshot guarantee used by all checked mutations.
|
|
writer, err := db.Open(filename)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer writer.Close()
|
|
if _, err := writer.Exec(`INSERT INTO paths VALUES('d','/source/unchecked','second',1,0)`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = tx.Exec(`DELETE FROM paths WHERE domain_uuid='d' AND path='/source/checked'`)
|
|
if !errors.Is(pathMutationTransactionError(err), ErrPathSnapshotChanged) {
|
|
t.Fatalf("stale read snapshot unexpectedly upgraded to a writer: %v", err)
|
|
}
|
|
_ = tx.Rollback()
|
|
var count int
|
|
if err := database.QueryRow(`SELECT COUNT(*) FROM paths`).Scan(&count); err != nil || count != 2 {
|
|
t.Fatalf("concurrent commit or rejected mutation lost data: %d, %v", count, err)
|
|
}
|
|
}
|