feat(db): typed wall storage, marker_seen and height backfill (#53)

This commit is contained in:
2026-11-19 22:03:11 +01:00
parent 6a443b4ec0
commit c1b1e9cf91
10 changed files with 551 additions and 45 deletions
+33 -5
View File
@@ -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: