server: config, migrations, redacted logging, /healthz entrypoint (#16)
This commit is contained in:
+115
@@ -0,0 +1,115 @@
|
||||
## Migration runner (DESIGN 4.4; DDL and version rule FIXED owner-delegated, #16).
|
||||
## A fresh DB is created and migrated; re-opening is a no-op; a newer DB is refused; a failing
|
||||
## migration leaves no trace (one transaction per step).
|
||||
import std/[unittest, os, strutils]
|
||||
import db_connector/db_sqlite
|
||||
import db
|
||||
|
||||
var tmpCount = 0
|
||||
proc tmpDir(): string =
|
||||
inc tmpCount
|
||||
result = getTempDir() / "ffftdb-" & $getCurrentProcessId() & "-" & $tmpCount
|
||||
removeDir(result)
|
||||
createDir(result)
|
||||
|
||||
proc openFresh(dir: string): DbConn = openDatabase(dir / "state.db")
|
||||
|
||||
suite "open":
|
||||
test "creates the file, sets WAL, synchronous NORMAL, busy_timeout, foreign_keys":
|
||||
let d = tmpDir()
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
check fileExists(d / "state.db")
|
||||
check db.getValue(sql"PRAGMA journal_mode").toLowerAscii == "wal"
|
||||
check db.getValue(sql"PRAGMA synchronous") == "1" # NORMAL (Q12)
|
||||
check db.getValue(sql"PRAGMA busy_timeout") == "5000"
|
||||
check db.getValue(sql"PRAGMA foreign_keys") == "1"
|
||||
|
||||
suite "migrate":
|
||||
test "fresh DB: applies every step, sets user_version and records history":
|
||||
let d = tmpDir()
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
check schemaVersion(db) == 0
|
||||
check migrate(db) == LatestVersion
|
||||
check LatestVersion == migrations[^1].version
|
||||
check schemaVersion(db) == LatestVersion
|
||||
check LatestVersion == 1
|
||||
let rows = appliedMigrations(db)
|
||||
check rows.len == 1
|
||||
check rows[0][0] == 1
|
||||
check rows[0][1] == "meta"
|
||||
# the step's own DDL ran: meta exists and is empty
|
||||
check db.getValue(sql"SELECT count(*) FROM meta") == "0"
|
||||
|
||||
test "re-open is a no-op: no step applied, history and timestamps untouched":
|
||||
let d = tmpDir()
|
||||
block:
|
||||
let db = openFresh(d)
|
||||
check migrate(db) == 1
|
||||
close(db)
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
let before = appliedMigrations(db)
|
||||
check migrate(db) == 0
|
||||
check migrate(db) == 0
|
||||
check schemaVersion(db) == 1
|
||||
check appliedMigrations(db) == before
|
||||
|
||||
test "history and user_version must agree":
|
||||
let d = tmpDir()
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
db.exec(sql"CREATE TABLE schema_migration (version INTEGER PRIMARY KEY, name TEXT NOT NULL, applied_at TEXT NOT NULL)")
|
||||
db.exec(sql"INSERT INTO schema_migration VALUES (1, 'meta', 'x')")
|
||||
db.exec(sql"PRAGMA user_version = 0")
|
||||
var msg = ""
|
||||
try:
|
||||
discard migrate(db)
|
||||
except MigrationError as e:
|
||||
msg = e.msg
|
||||
check "disagree" in msg
|
||||
|
||||
test "a DB from a newer server is refused, not downgraded":
|
||||
let d = tmpDir()
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
db.exec(sql"CREATE TABLE schema_migration (version INTEGER PRIMARY KEY, name TEXT NOT NULL, applied_at TEXT NOT NULL)")
|
||||
db.exec(sql"PRAGMA user_version = 9")
|
||||
var msg = ""
|
||||
try:
|
||||
discard migrate(db)
|
||||
except MigrationError as e:
|
||||
msg = e.msg
|
||||
check "newer" in msg
|
||||
check schemaVersion(db) == 9
|
||||
check appliedMigrations(db).len == 0
|
||||
|
||||
test "a failing step rolls back: no DDL, no history, version unchanged":
|
||||
let d = tmpDir()
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
# step 1 creates meta; pre-creating it makes the step fail half-way through
|
||||
db.exec(sql"CREATE TABLE meta (k TEXT PRIMARY KEY, v TEXT NOT NULL)")
|
||||
var raised = false
|
||||
try:
|
||||
discard migrate(db)
|
||||
except MigrationError:
|
||||
raised = true
|
||||
check raised
|
||||
check schemaVersion(db) == 0
|
||||
check appliedMigrations(db).len == 0
|
||||
# the runner's own bookkeeping table may exist, but the failed step left nothing else
|
||||
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'meta'") == "1"
|
||||
Reference in New Issue
Block a user