Skip to content

Commit 57bc149

Browse files
committed
surreal: improve schema generation and error handling
Body - Add `responseMessage(JsonNode)` helper and use it to produce clearer, robust error messages when parsing SurrealDB `info()` responses. - Harden exec/execThenSaveHistory to check presence/type of `status` and to raise errors using `responseMessage` instead of assuming keys. - Treat Surreal `id` field as a special case: skip generating autoincrement definitions for `id` (avoid defining increments on Surreal primary id) and adjust createIncrements/createInt behavior. - Map Surreal `object` type to Nim `JsonNode` in `create_schema` generation. - Update test `tests/surrealdb/test_create_schema.nim` to use `index` as the increments column in fixtures and assert generated schema accordingly. - Files changed: src/allographer/schema_builder/queries/surreal/schema_utils.nim, src/allographer/schema_builder/queries/surreal/sub/create_column_query.nim, src/allographer/schema_builder/usecases/surreal/create_schema.nim, tests/surrealdb/test_create_schema.nim. Rationale Improve robustness when consuming SurrealDB metadata and ensure generated Nim schema/types and column-definition SQL are consistent and safe for common edge cases.
1 parent f722475 commit 57bc149

4 files changed

Lines changed: 30 additions & 7 deletions

File tree

src/allographer/schema_builder/queries/surreal/schema_utils.nim

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ proc notAllowedType*(typ:string) =
2020
raise newException(DbError, &"type {typ} is not allowed")
2121

2222

23+
proc responseMessage(node: JsonNode): string =
24+
if node.kind == JObject:
25+
for key in ["detail", "information", "result", "message", "error"]:
26+
if node.hasKey(key):
27+
let value = node[key]
28+
if value.kind == JString:
29+
return value.getStr()
30+
return value.pretty
31+
return node.pretty
32+
33+
2334
# ==================================================
2435
# SurrealDB
2536
# ==================================================
@@ -60,8 +71,8 @@ proc execThenSaveHistory*(rdb:SurrealConnections, tableName:string, queries:seq[
6071
for query in queries:
6172
let resp = rdb.raw(query).info().waitFor
6273
for row in resp:
63-
if row["status"].getStr != "OK":
64-
raise newException(DbError, row["detail"].getStr)
74+
if row.hasKey("status") and row["status"].kind == JString and row["status"].getStr != "OK":
75+
raise newException(DbError, responseMessage(row))
6576
isSuccess = true
6677
except DbError:
6778
echo getCurrentExceptionMsg()
@@ -91,10 +102,10 @@ proc execThenSaveHistory*(rdb:SurrealConnections, tableName:string, query:string
91102
try:
92103
let resp = rdb.raw(query).info().waitFor
93104
if resp.kind == JObject:
94-
raise newException(DbError, resp["information"].getStr)
105+
raise newException(DbError, responseMessage(resp))
95106
for row in resp:
96-
if row["status"].getStr != "OK":
97-
raise newException(DbError, row["detail"].getStr)
107+
if row.hasKey("status") and row["status"].kind == JString and row["status"].getStr != "OK":
108+
raise newException(DbError, responseMessage(row))
98109
isSuccess = true
99110
except DbError:
100111
echo getCurrentExceptionMsg()

src/allographer/schema_builder/queries/surreal/sub/create_column_query.nim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,17 @@ proc autoIncrementValueExpr(table: Table, column: Column): string =
4747
&"(SELECT `max_index` FROM `_autoincrement_sequences` WHERE `table` = \"{table.name}\" AND `column` = \"{column.name}\" LIMIT 1)[0].max_index + 1"
4848

4949

50+
proc isSurrealIdField(column: Column): bool =
51+
column.name == "id"
52+
53+
5054
# =============================================================================
5155
# int
5256
# =============================================================================
5357
proc createIncrementsColumn(column:Column, table:Table):seq[string] =
58+
if isSurrealIdField(column):
59+
return @[]
60+
5461
let nextIndexExpr = autoIncrementValueExpr(table, column)
5562
result.add(&"""
5663
INSERT INTO `_autoincrement_sequences` {{table: "{table.name}", column: "{column.name}", max_index: 0}};
@@ -67,6 +74,9 @@ proc createIntColumn(column:Column, table:Table):seq[string] =
6774
var assertClause = ""
6875

6976
if column.isAutoIncrement:
77+
if isSurrealIdField(column):
78+
return @[]
79+
7080
let nextIndexExpr = autoIncrementValueExpr(table, column)
7181
query.add(&"""
7282
INSERT INTO `_autoincrement_sequences` {{table: "{table.name}", column: "{column.name}", max_index: 0}};

src/allographer/schema_builder/usecases/surreal/create_schema.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ proc generateSchemaCode(tablesInfo: Table[string, seq[tuple[name: string, typ: s
6666
"int"
6767
of "string", "datetime":
6868
"string"
69+
of "object":
70+
"JsonNode"
6971
of "bool":
7072
"bool"
7173
of "decimal", "float":

tests/surrealdb/test_create_schema.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ suite "Schema output after migration":
2626

2727
rdb.create(
2828
table("int_relation", [
29-
Column.increments("id")
29+
Column.increments("index")
3030
]),
3131
table("str_relation", [
3232
Column.uuid("uuid")
3333
]),
3434
table("test_schema_output", [
35-
Column.increments("id"),
35+
Column.increments("index"),
3636
Column.integer("integer"),
3737
Column.smallInteger("smallInteger"),
3838
Column.mediumInteger("mediumInteger"),

0 commit comments

Comments
 (0)