server: scene and wall state, slice-4 migration (#19)

Migration step 3 ("world") creates scene and wall and seeds the single v1 scene
(id 1, 4096x4096, tile = cell 32, vision 400, token radius 20, art_version 1,
next_wall_id 1). server/world.nim holds the in-memory scene with SQLite as
write-through persistence: per-scene wall ids from next_wall_id (monotonic,
never reused, #3), 6.1 validation (a != b, in bounds, a wall is never open),
flags as an opaque JSON object, and one transaction with synchronous=FULL
around every GM authoring write (Q12); memory follows COMMIT, so a failed
write leaves both sides unchanged.

Tests: tests/tworld.nim (ids, reload, doors, validation, pragma, failed write),
tests/tdb.nim (version 3, history, a slice-2 DB migrating to slice 3).
This commit is contained in:
2026-11-15 08:49:16 +01:00
parent 2a1e2dd71c
commit bf9ab3edfd
4 changed files with 649 additions and 4 deletions
+34 -4
View File
@@ -38,24 +38,29 @@ suite "migrate":
check migrate(db) == LatestVersion
check LatestVersion == migrations[^1].version
check schemaVersion(db) == LatestVersion
check LatestVersion == 2
check LatestVersion == 3
let rows = appliedMigrations(db)
check rows.len == 2
check rows.len == 3
check rows[0][0] == 1
check rows[0][1] == "meta"
check rows[1][0] == 2
check rows[1][1] == "auth"
check rows[2][0] == 3
check rows[2][1] == "world"
# the steps' own DDL ran: meta and the auth tables exist and are empty
check db.getValue(sql"SELECT count(*) FROM meta") == "0"
check db.getValue(sql"SELECT count(*) FROM player") == "0"
check db.getValue(sql"SELECT count(*) FROM player_key") == "0"
check db.getValue(sql"SELECT count(*) FROM session") == "0"
# step 3 seeds the single v1 scene (Q3) and no walls
check db.getValue(sql"SELECT count(*) FROM scene") == "1"
check db.getValue(sql"SELECT count(*) FROM wall") == "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) == 2
check migrate(db) == 3
close(db)
let db = openFresh(d)
defer:
@@ -64,7 +69,7 @@ suite "migrate":
let before = appliedMigrations(db)
check migrate(db) == 0
check migrate(db) == 0
check schemaVersion(db) == 2
check schemaVersion(db) == 3
check appliedMigrations(db) == before
test "history and user_version must agree":
@@ -118,3 +123,28 @@ suite "migrate":
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"
test "a slice-2 DB migrates to slice 3: the scene is seeded, players and keys survive":
let d = tmpDir()
let db = openFresh(d)
defer:
close(db)
removeDir(d)
# Bring the DB to step 2 only, exactly as a slice-2 server left it.
db.exec(sql(MigrationHistoryDdl))
for m in migrations[0 ..< 2]:
db.exec(sql"BEGIN")
for stmt in m.stmts: db.exec(sql(stmt))
db.exec(sql"INSERT INTO schema_migration (version, name, applied_at) VALUES (?, ?, 'x')",
m.version, m.name)
db.exec(sql("PRAGMA user_version = " & $m.version))
db.exec(sql"COMMIT")
db.exec(sql"INSERT INTO player (name, role, colour, created_at) VALUES ('GM', 'gm', '#6b7280', 'x')")
check schemaVersion(db) == 2
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'scene'") == "0"
check migrate(db) == 1 # only step 3 runs
check schemaVersion(db) == 3
check db.getValue(sql"SELECT count(*) FROM scene") == "1"
check db.getValue(sql"SELECT next_wall_id FROM scene WHERE id = 1") == "1"
check db.getValue(sql"SELECT count(*) FROM player WHERE role = 'gm'") == "1"