feat(db): typed wall storage, marker_seen and height backfill (#53)
This commit is contained in:
+130
-6
@@ -38,9 +38,9 @@ suite "migrate":
|
||||
check migrate(db) == LatestVersion
|
||||
check LatestVersion == migrations[^1].version
|
||||
check schemaVersion(db) == LatestVersion
|
||||
check LatestVersion == 7
|
||||
check LatestVersion == 8
|
||||
let rows = appliedMigrations(db)
|
||||
check rows.len == 7
|
||||
check rows.len == 8
|
||||
check rows[0][0] == 1
|
||||
check rows[0][1] == "meta"
|
||||
check rows[1][0] == 2
|
||||
@@ -55,6 +55,8 @@ suite "migrate":
|
||||
check rows[5][1] == "token details"
|
||||
check rows[6][0] == 7
|
||||
check rows[6][1] == "characters"
|
||||
check rows[7][0] == 8
|
||||
check rows[7][1] == "wall record"
|
||||
# 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"
|
||||
@@ -77,6 +79,16 @@ suite "migrate":
|
||||
check db.getValue(sql"SELECT count(*) FROM encountered") == "0"
|
||||
for col in ["character_id", "reveal_all"]:
|
||||
check db.getValue(sql("SELECT count(*) FROM pragma_table_info('entity') WHERE name = '" & col & "'")) == "1"
|
||||
# step 8 rebuilds `wall` into typed columns, drops `kind`, and creates `marker_seen`
|
||||
check db.getValue(sql"SELECT count(*) FROM pragma_table_info('wall')") == "19"
|
||||
check db.getValue(sql"SELECT count(*) FROM pragma_table_info('wall') WHERE name = 'kind'") == "0"
|
||||
for col in ["thickness", "position", "blocks_sight", "blocks_movement", "sight_dir",
|
||||
"player_marker", "door", "open", "secret", "locked", "height", "label", "flags"]:
|
||||
check db.getValue(sql("SELECT count(*) FROM pragma_table_info('wall') WHERE name = '" & col & "'")) == "1"
|
||||
check db.getValue(sql"SELECT count(*) FROM marker_seen") == "0"
|
||||
check db.getValue(sql"SELECT sql FROM sqlite_master WHERE name = 'marker_seen'")
|
||||
.contains("FOREIGN KEY (scene_id, wall_id) REFERENCES wall(scene_id, id) ON DELETE CASCADE")
|
||||
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'wall_v1'") == "0"
|
||||
|
||||
test "re-open is a no-op: no step applied, history and timestamps untouched":
|
||||
let d = tmpDir()
|
||||
@@ -165,7 +177,7 @@ suite "migrate":
|
||||
check schemaVersion(db) == 2
|
||||
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'scene'") == "0"
|
||||
|
||||
check migrate(db) == LatestVersion - 2 # steps 3 to 7 run
|
||||
check migrate(db) == LatestVersion - 2 # steps 3 to 8 run
|
||||
check schemaVersion(db) == LatestVersion
|
||||
check db.getValue(sql"SELECT count(*) FROM scene") == "1"
|
||||
check db.getValue(sql"SELECT next_wall_id FROM scene WHERE id = 1") == "1"
|
||||
@@ -198,15 +210,17 @@ suite "migrate":
|
||||
check schemaVersion(db) == 4
|
||||
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'explored_chunk'") == "0"
|
||||
|
||||
check migrate(db) == 3 # steps 5, 6 and 7 run
|
||||
check migrate(db) == 4 # steps 5, 6, 7 and 8 run
|
||||
check schemaVersion(db) == LatestVersion
|
||||
check appliedMigrations(db).len == 7
|
||||
check appliedMigrations(db).len == 8
|
||||
check appliedMigrations(db)[4] == (version: 5, name: "explored")
|
||||
check appliedMigrations(db)[5] == (version: 6, name: "token details")
|
||||
check appliedMigrations(db)[6] == (version: 7, name: "characters")
|
||||
check appliedMigrations(db)[7] == (version: 8, name: "wall record")
|
||||
# step 6: the status table exists, and the old token's buckets default to empty objects
|
||||
check db.getValue(sql"SELECT count(*) FROM entity_status") == "0"
|
||||
check db.getValue(sql"SELECT props_public || props_owner || props_gm FROM entity") == "{}{}{}"
|
||||
check db.getValue(sql"SELECT props_public || props_owner FROM entity") == "{}{}"
|
||||
check db.getValue(sql"SELECT props_gm FROM entity") == """{"height":1.8}"""
|
||||
# the explored store exists, with both FKs and the 512-byte chunk invariant
|
||||
check db.getValue(sql"SELECT count(*) FROM explored_chunk") == "0"
|
||||
check db.getValue(sql"SELECT sql FROM sqlite_master WHERE name = 'explored_chunk'").contains("length(bits) = 512")
|
||||
@@ -222,3 +236,113 @@ suite "migrate":
|
||||
check db.getValue(sql"SELECT count(*) FROM explored_chunk") == "1"
|
||||
db.exec(sql"DELETE FROM player WHERE id = 2")
|
||||
check db.getValue(sql"SELECT count(*) FROM explored_chunk") == "0"
|
||||
|
||||
# Bring a fresh, empty DB to step `n` exactly as an older build left it.
|
||||
proc applySteps(db: DbConn, n: int) =
|
||||
db.exec(sql(MigrationHistoryDdl))
|
||||
for m in migrations[0 ..< n]:
|
||||
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")
|
||||
|
||||
suite "step 8 wall CHECKs (DESIGN 4.4)":
|
||||
test "every impossible wall row is rejected by the database":
|
||||
let d = tmpDir()
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
discard migrate(db)
|
||||
# A valid legacy wall (today's behaviour after the mapping) and a valid open door.
|
||||
db.exec(sql"""INSERT INTO wall (scene_id, id, ax, ay, bx, by, thickness, position,
|
||||
blocks_sight, blocks_movement, sight_dir, player_marker, door, open, secret,
|
||||
locked, height, label, flags)
|
||||
VALUES (1, 1, 0, 0, 100, 0, 0, 'center', 1, 1, 'both', 0, 0, 0, 0, 0,
|
||||
NULL, NULL, '{}')""")
|
||||
db.exec(sql"""INSERT INTO wall (scene_id, id, ax, ay, bx, by, thickness, position,
|
||||
blocks_sight, blocks_movement, sight_dir, player_marker, door, open, secret,
|
||||
locked, height, label, flags)
|
||||
VALUES (1, 2, 0, 0, 100, 0, 128, 'left', 1, 1, 'left', 1, 1, 1, 0, 0,
|
||||
1.8, 'railing', '{}')""")
|
||||
checkpoint "a valid secret door, fixed window and height/label row also insert"
|
||||
db.exec(sql"""INSERT INTO wall (scene_id, id, ax, ay, bx, by, thickness, position,
|
||||
blocks_sight, blocks_movement, sight_dir, player_marker, door, open, secret,
|
||||
locked, height, label, flags)
|
||||
VALUES (1, 3, 0, 0, 100, 0, 0, 'center', 1, 1, 'both', 0, 1, 0, 1, 1,
|
||||
NULL, NULL, '{}')""")
|
||||
db.exec(sql"""INSERT INTO wall (scene_id, id, ax, ay, bx, by, thickness, position,
|
||||
blocks_sight, blocks_movement, sight_dir, player_marker, door, open, secret,
|
||||
locked, height, label, flags)
|
||||
VALUES (1, 4, 0, 0, 100, 0, 0, 'center', 0, 1, 'both', 1, 0, 0, 0, 0,
|
||||
NULL, NULL, '{}')""")
|
||||
|
||||
proc insert(row: string): bool =
|
||||
try:
|
||||
db.exec(sql("INSERT INTO wall (scene_id, id, ax, ay, bx, by, thickness, position, " &
|
||||
"blocks_sight, blocks_movement, sight_dir, player_marker, door, open, secret, locked, " &
|
||||
"height, label, flags) VALUES " & row))
|
||||
true
|
||||
except CatchableError:
|
||||
false
|
||||
|
||||
var id = 10
|
||||
proc row(thickness = "0", position = "'center'", blocksSight = "1",
|
||||
blocksMovement = "1", sightDir = "'both'", marker = "0", door = "0",
|
||||
open = "0", secret = "0", locked = "0", height = "NULL",
|
||||
label = "NULL"): string =
|
||||
inc id
|
||||
"(1, " & $id & ", 0, 0, 100, 0, " & thickness & ", " & position & ", " & blocksSight &
|
||||
", " & blocksMovement & ", " & sightDir & ", " & marker & ", " & door & ", " &
|
||||
open & ", " & secret & ", " & locked & ", " & height & ", " & label & ", '{}')"
|
||||
|
||||
check not insert(row(thickness = "-1"))
|
||||
check not insert(row(thickness = "257"))
|
||||
check not insert(row(position = "'north'"))
|
||||
check not insert(row(blocksSight = "2"))
|
||||
check not insert(row(blocksMovement = "2"))
|
||||
check not insert(row(sightDir = "'north'"))
|
||||
check not insert(row(marker = "2"))
|
||||
check not insert(row(door = "2"))
|
||||
check not insert(row(open = "2"))
|
||||
check not insert(row(secret = "2"))
|
||||
check not insert(row(locked = "2"))
|
||||
check not insert(row(height = "0"))
|
||||
check not insert(row(height = "101"))
|
||||
check not insert(row(label = "''"))
|
||||
check not insert(row(label = "'" & repeat('x', 65) & "'"))
|
||||
check not insert(row(door = "0", open = "1")) # open non-door
|
||||
check not insert(row(door = "0", secret = "1")) # secret non-door
|
||||
check not insert(row(door = "1", open = "1", secret = "1")) # open secret door
|
||||
check not insert(row(door = "1", secret = "1", blocksSight = "0"))
|
||||
check not insert(row(door = "1", secret = "1", blocksMovement = "0"))
|
||||
check not insert(row(door = "1", secret = "1", sightDir = "'left'"))
|
||||
check not insert(row(door = "0", locked = "1")) # locked non-door
|
||||
check not insert(row(blocksSight = "0", sightDir = "'left'")) # direction with no sight
|
||||
check insert(row(door = "1", open = "1")) # an open door is legal
|
||||
check db.getValue(sql"SELECT count(*) FROM wall") == "5"
|
||||
|
||||
test "a failing step 8 rolls back the rebuild, the history row and the version":
|
||||
let d = tmpDir()
|
||||
let db = openFresh(d)
|
||||
defer:
|
||||
close(db)
|
||||
removeDir(d)
|
||||
applySteps(db, 7)
|
||||
# A pre-existing `marker_seen` makes the last statement of step 8 fail after the wall table
|
||||
# has been renamed, rebuilt, filled and dropped: the whole step must roll back.
|
||||
db.exec(sql"CREATE TABLE marker_seen (x INTEGER)")
|
||||
var raised = false
|
||||
try:
|
||||
discard migrate(db)
|
||||
except MigrationError:
|
||||
raised = true
|
||||
check raised
|
||||
check schemaVersion(db) == 7
|
||||
check appliedMigrations(db).len == 7
|
||||
# the legacy wall table is back: `kind` present, the typed columns absent
|
||||
check db.getValue(sql"SELECT count(*) FROM pragma_table_info('wall') WHERE name = 'kind'") == "1"
|
||||
check db.getValue(sql"SELECT count(*) FROM pragma_table_info('wall') WHERE name = 'thickness'") == "0"
|
||||
check db.getValue(sql"SELECT count(*) FROM sqlite_master WHERE name = 'wall_v1'") == "0"
|
||||
|
||||
+2
-2
@@ -127,7 +127,7 @@ suite "player view (3.1: controlled always, LOS + not gm_only otherwise)":
|
||||
check other.tokens[1].includeVisionRadius == false
|
||||
|
||||
test "a scan of every player frame finds no wall-ish or GM-only key":
|
||||
let sc = sceneWith(@[WallRec(a: (700, 0), b: (700, 4096), flags: newJObject())])
|
||||
let sc = sceneWith(@[defaultWall((700.0, 0.0), (700.0, 4096.0))])
|
||||
let tokens = @[tok("mine", 400, 400, Pid), tok("near", 600, 400, 0),
|
||||
tok("secret", 500, 500, 0, gmOnly = true)]
|
||||
var known: Known
|
||||
@@ -195,7 +195,7 @@ suite "diff (6.4: only what changed)":
|
||||
var ctx = ctxOf(sceneWith(@[]), @[tok("mine", 400, 400, Pid)], @[Pid])
|
||||
var known: Known
|
||||
discard pass(ctx, known, resync = true)
|
||||
ctx.scene.walls = @[WallRec(a: (700, 0), b: (700, 4096), flags: newJObject())]
|
||||
ctx.scene.walls = @[defaultWall((700.0, 0.0), (700.0, 4096.0))]
|
||||
refreshAll(ctx)
|
||||
let frames = pass(ctx, known)
|
||||
check frames[0]["t"].getStr == "vision" # the polygons shrank
|
||||
|
||||
+3
-1
@@ -10,7 +10,9 @@ proc sceneWith(walls: seq[WallRec]): SceneRec =
|
||||
extra: newJObject(), walls: walls)
|
||||
|
||||
proc wall(ax, ay, bx, by: float, kind = wkWall, open = false): WallRec =
|
||||
WallRec(a: (ax, ay), b: (bx, by), kind: kind, open: open, flags: newJObject())
|
||||
result = defaultWall((ax, ay), (bx, by))
|
||||
result.kind = kind
|
||||
result.open = open
|
||||
|
||||
proc tok(id: string, x, y: float, owner: int, visionFlag = true, gmOnly = false): TokenRec =
|
||||
TokenRec(id: id, scene: 1, kind: "pc", descriptiveName: id, actualName: id, pos: (x, y),
|
||||
|
||||
+33
-5
@@ -13,7 +13,9 @@ proc tmpDir(): string =
|
||||
createDir(result)
|
||||
|
||||
proc wall(ax, ay, bx, by: float, kind = wkWall, open = false): WallRec =
|
||||
WallRec(a: (ax, ay), b: (bx, by), kind: kind, open: open, flags: newJObject())
|
||||
result = defaultWall((ax, ay), (bx, by))
|
||||
result.kind = kind
|
||||
result.open = open
|
||||
|
||||
proc freshScene(): tuple[db: DbConn, dir: string] =
|
||||
## A migrated DB (the scene row comes from migration step 3) plus the scene loaded into memory.
|
||||
@@ -136,12 +138,15 @@ suite "persistence (write-through, restart-identical)":
|
||||
close(conn)
|
||||
removeDir(d)
|
||||
var sc = loadScene(conn).get
|
||||
let id1 = addWall(conn, sc, WallRec(a: (0, 0), b: (4096, 0), kind: wkWall,
|
||||
flags: %*{"note": "north"})).get
|
||||
var north = defaultWall((0.0, 0.0), (4096.0, 0.0))
|
||||
north.flags = %*{"note": "north"}
|
||||
let id1 = addWall(conn, sc, north).get
|
||||
let id2 = addWall(conn, sc, wall(100, 25, 300, 25, wkDoor)).get
|
||||
check setDoor(conn, sc, id2, true) == waOk
|
||||
let changed = WallRec(id: id1, a: (0, 0), b: (2000, 0), kind: wkDoor,
|
||||
open: false, flags: %*{"note": "north wall"})
|
||||
var changed = defaultWall((0.0, 0.0), (2000.0, 0.0))
|
||||
changed.id = id1
|
||||
changed.kind = wkDoor
|
||||
changed.flags = %*{"note": "north wall"}
|
||||
check updateWall(conn, sc, changed) == waOk
|
||||
|
||||
let reloaded = loadScene(conn).get
|
||||
@@ -162,6 +167,29 @@ suite "persistence (write-through, restart-identical)":
|
||||
check loadScene(conn).get.walls.len == 1
|
||||
check loadScene(conn).get.nextWallId == 3
|
||||
|
||||
test "nullable height and label use SQL literals and round-trip as none, not 0 or ''":
|
||||
let (conn, d) = freshScene()
|
||||
defer:
|
||||
close(conn)
|
||||
removeDir(d)
|
||||
var sc = loadScene(conn).get
|
||||
var withBoth = defaultWall((10.0, 10.0), (100.0, 10.0))
|
||||
withBoth.height = some(2.5)
|
||||
withBoth.label = some("the king's window")
|
||||
let id = addWall(conn, sc, withBoth).get
|
||||
let plain = addWall(conn, sc, defaultWall((10.0, 20.0), (100.0, 20.0))).get
|
||||
let reloaded = loadScene(conn).get
|
||||
check reloaded.walls[0].height == some(2.5)
|
||||
check reloaded.walls[0].label == some("the king's window")
|
||||
check reloaded.walls[1].height.isNone # NULL, never 0 (a height is > 0)
|
||||
check reloaded.walls[1].label.isNone # NULL, never '' (a label is 1..64)
|
||||
check conn.getValue(sql("SELECT label FROM wall WHERE id = " & $id)) == "the king's window"
|
||||
check sqlLitText("the king's window") == "'the king''s window'"
|
||||
check sqlLitTextOrNull(none(string)) == "NULL"
|
||||
check sqlLitRealOrNull(none(float)) == "NULL"
|
||||
check sqlLitRealOrNull(some(1.8)) == "1.8"
|
||||
check conn.getValue(sql("SELECT count(*) FROM wall WHERE id = " & $plain & " AND height IS NULL")) == "1"
|
||||
|
||||
test "authoring runs with synchronous=FULL and leaves NORMAL behind (Q12)":
|
||||
let (conn, d) = freshScene()
|
||||
defer:
|
||||
|
||||
Reference in New Issue
Block a user