fix: walls r1 review — server (ref #52)

This commit is contained in:
2026-11-20 08:42:50 +01:00
parent f9f26a4a01
commit e1a5b297e4
11 changed files with 198 additions and 167 deletions
+23 -3
View File
@@ -41,7 +41,7 @@
## db_connector cannot bind NULL (4.4). The step's `data` hook is the height backfill: every entity
## and character whose `props_gm` lacks a `height` key gets `1.8`, and nothing else is touched (5.7).
## One connection, used under the server's global lock (4.3); this module does no locking itself.
import std/[json, strutils, times]
import std/[json, options, strutils, times]
import db_connector/db_sqlite
type
@@ -59,6 +59,22 @@ type
## backfill). Runs after `stmts` and before the history row, inside the same transaction.
MigrationRow* = tuple[version: int, name: string]
proc sqlLitText*(value: string): string =
## A SQL text literal (DESIGN 4.4): single quotes doubled.
##
## Precondition, enforced here rather than left to the caller: `value` contains no NUL or other
## control character. db_connector cannot bind NULL and callers may pass GM-authored text, so
## this helper refuses a value whose control characters could corrupt the statement; a raise is
## a bug in the caller, never a way to smuggle one past validation.
for c in value:
if c.ord < 0x20 or c.ord == 0x7F:
raise newException(ValueError, "SQL text literal contains a control character")
"'" & value.replace("'", "''") & "'"
proc sqlLitTextOrNull*(value: Option[string]): string =
## `sqlLitText`, or `NULL` for an absent value.
if value.isSome: sqlLitText(value.get) else: "NULL"
proc backfillHeights(db: DbConn) =
## Migration step 8's `data` hook (DESIGN 4.4, 5.7): every existing token and character gets
## `height: 1.8` in its GM bucket when that bucket is a JSON object without a `height` key.
@@ -68,8 +84,12 @@ proc backfillHeights(db: DbConn) =
var props: JsonNode
try:
props = parseJson(row[1])
except CatchableError:
continue # unreadable props are left exactly as they are
except CatchableError as e:
# DESIGN 4.4: a failing step rolls back completely. A row whose props_gm is not JSON means
# the backfill cannot decide, so the step fails instead of leaving that row stale.
raise newException(ValueError,
"migration 8 height backfill: " & table & " row " & row[0] &
" has malformed props_gm: " & e.msg)
if props.isNil or props.kind != JObject or props.hasKey("height"):
continue
props["height"] = newJFloat(1.8)