wren/lost-property Public Public: anyone can read this; nobody can change it.

workbenchthis machine
Files d66e67826a76bd71f7b0790b198ae93f73fe1614
30 lines · 1.2 KB · ~357 tok Raw History
1
import { expect, test } from "bun:test";
2
import { Database } from "bun:sqlite";
3
import { mkdtempSync } from "node:fs";
4
import { tmpdir } from "node:os";
5
import { join } from "node:path";
6
7
// Seed a data directory with the note table an earlier version of this app
8
// left behind: no item_id, so it does not fit the current schema.
9
const dir = mkdtempSync(join(tmpdir(), "app-"));
10
process.env.FORGE_APP_DATA = dir;
11
const seed = new Database(join(dir, "app.db"));
12
seed.exec(
13
  "CREATE TABLE note (id INTEGER PRIMARY KEY, body TEXT NOT NULL, created_at TEXT NOT NULL DEFAULT (datetime('now')))",
14
);
15
seed.exec("INSERT INTO note (body) VALUES ('an old note from before items existed')");
16
seed.close();
17
18
const { db, createItem, addNote, listNotes, listItems } = await import("./db.ts");
19
20
test("moves an old, incompatible note table aside instead of crashing", () => {
21
  const row = db.query<{ body: string }, []>("SELECT body FROM note_legacy").get();
22
  expect(row?.body).toBe("an old note from before items existed");
23
});
24
25
test("the collection works normally after migrating", () => {
26
  const id = createItem("a test item");
27
  addNote(id, "sighting", "hello");
28
  expect(listNotes(id)).toHaveLength(1);
29
  expect(listItems()[0]?.note_count).toBe(1);
30
});
↑↓ navigate ↵ open esc close
↑↓ navigate ↵ open esc close

Keyboard shortcuts