nandi/gleanpublic Fork
⑂ main
Commits
⬇ Clone ▾
git clone https://git.rickub.com/nandi/glean.git
git clone ssh://git@rickub.com/nandi/glean.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
Simplify application structure and remove obsolete code b205012 · on main · nandithebull · Sep 22, 2026
pulseboarddb_probe.nim · 135 lines · 5.4 KBNim Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
## Checks the three-database layout, the schema, and the behaviour that
## depends on both.
##
##   nim c -r -d:ssl pulseboarddb_probe.nim
##
## Most of this is about things that fail quietly. An ATTACH that did not
## happen, a pragma that did not take, an FTS index whose triggers never fire
## -- each leaves a database that works for the first query anyone tries and
## degrades or diverges later.

import std/[options, os, strformat, strutils]
import reader/[pulseboarddb, schema, sqlite]

var failures = 0

proc report(name: string, ok: bool, detail = "") =
  if not ok: inc failures
  let label = if ok: "PASS" else: "FAIL"
  if detail.len > 0: echo &"  {label}  {name}  -- {detail}"
  else: echo &"  {label}  {name}"

proc cleanup(base: string) =
  for suffix in ["_users", "_articles"]:
    for ext in ["", "-wal", "-shm"]:
      removeFile(base & suffix & ext)

proc main() =
  echo "Pulseboard database layer probe"
  echo ""

  let base = getTempDir() / "pulseboard_db_probe"
  cleanup(base)
  defer: cleanup(base)

  var g = pulseboarddb.open(base)
  defer: g.close()

  report("the schema covers the reader's tables", readerSchema.len == 15,
         &"{readerSchema.len} statements")

  # --- the three databases ------------------------------------------------
  block:
    let schemas = g.attachedSchemas()
    report("both databases are attached",
           "main" in schemas and "articles" in schemas, schemas.join(", "))
    report("each is a separate file on disk",
           fileExists(base & "_users") and fileExists(base & "_articles"))

  # --- pragmas ------------------------------------------------------------
  # WAL has to be set per schema: PRAGMA applies to one at a time, so setting
  # it on main leaves the others journalling the slow way.
  block:
    for name in ["", "articles"]:
      let mode = g.journalMode(name).toLowerAscii
      report(&"WAL is on for {(if name.len == 0: \"main\" else: name)}",
             mode == "wal", mode)

  # --- schema -------------------------------------------------------------
  g.migrate()
  report("migrate() is idempotent", (g.migrate(); true))

  block:
    proc tableCount(schemaName: string): int =
      let n = g.db.queryInt(
        &"SELECT count(*) FROM {schemaName}.sqlite_master WHERE type='table'")
      n.get(0).int
    # main holds only the local user now; everything that grows lives in
    # the articles database.
    report("the users database holds just the user table",
           tableCount("main") == 1, $tableCount("main"))
    report("articles holds feeds, subscriptions, articles and read state",
           tableCount("articles") >= 5, $tableCount("articles"))

  # --- a real cross-database query ---------------------------------------
  # The whole point of attaching rather than opening three connections.
  block:
    # `users` is keyed on the DID alone -- handles are resolved from atproto
    # rather than stored, so there is no handle column to join on.
    g.db.run("INSERT INTO users (did) VALUES (?)", p("did:plc:probe"))
    g.db.run("""INSERT INTO articles.feeds (feed_url, title)
                VALUES (?, ?)""", p("https://ex.test/feed"), p("Example"))
    g.db.run("""INSERT INTO articles.subscriptions (user_did, feed_url)
                VALUES (?, ?)""", p("did:plc:probe"), p("https://ex.test/feed"))

    let joined = g.db.queryText("""
      SELECT f.title FROM articles.subscriptions s
      JOIN users u ON u.did = s.user_did
      JOIN articles.feeds f ON f.feed_url = s.feed_url
      WHERE u.did = ?""", p("did:plc:probe"))
    report("a query joins across attached databases",
           joined == some("Example"), joined.get("nothing"))

  # --- FTS5 ---------------------------------------------------------------
  # The index is kept current by triggers, so inserting into `articles` alone
  # must make the row findable. If the triggers were missed, search silently
  # returns nothing while everything else looks fine.
  block:
    g.db.run("""INSERT INTO articles.articles
                (feed_url, guid, title, summary, content, author)
                VALUES (?, ?, ?, ?, ?, ?)""",
             p("https://ex.test/feed"), p("g1"),
             p("Nim systems programming"), p("a summary"),
             p("the body text"), p("An Author"))

    let hit = g.db.queryText(
      "SELECT title FROM articles.articles_fts WHERE articles_fts MATCH ?",
      p("systems"))
    report("FTS5 triggers index new rows", hit.isSome, hit.get("no match"))

    g.db.run("UPDATE articles.articles SET title = ? WHERE guid = ?",
             p("Rust systems programming"), p("g1"))
    let updated = g.db.queryText(
      "SELECT title FROM articles.articles_fts WHERE articles_fts MATCH ?",
      p("Rust"))
    report("FTS5 triggers follow updates", updated.isSome, updated.get("no match"))

    g.db.run("DELETE FROM articles.articles WHERE guid = ?", p("g1"))
    let afterDelete = g.db.queryText(
      "SELECT title FROM articles.articles_fts WHERE articles_fts MATCH ?",
      p("systems"))
    # The detail is only interesting when this fails, and printing the
    # fallback on success reads like the opposite of what happened.
    report("FTS5 triggers follow deletes", afterDelete.isNone,
           if afterDelete.isNone: "row gone from the index"
           else: "STILL INDEXED: " & afterDelete.get)

  echo ""
  if failures == 0:
    echo "RESULT: the database layer matches Pulseboard's layout and behaviour."
  else:
    echo &"RESULT: {failures} check(s) failed."
    quit 1

when isMainModule:
  main()