-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathsqlite-import.test.ts
More file actions
115 lines (103 loc) · 3.33 KB
/
sqlite-import.test.ts
File metadata and controls
115 lines (103 loc) · 3.33 KB
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
import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest";
import { Database } from "bun:sqlite";
import { existsSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { collectTables } from "@executor-js/sdk";
import { withQueryContext } from "fumadb/query";
import { importSqliteDataToFuma } from "./sqlite-import";
import { createSqliteFumaDb, type SqliteFumaDb } from "./sqlite-fumadb";
let workDir: string;
let sqlite: SqliteFumaDb | null;
beforeEach(() => {
workDir = mkdtempSync(join(tmpdir(), "executor-sqlite-import-"));
sqlite = null;
});
afterEach(async () => {
await sqlite?.close();
rmSync(workDir, { recursive: true, force: true });
});
const seedSqlite = (path: string) => {
const db = new Database(path);
db.exec(`
CREATE TABLE source (
id TEXT PRIMARY KEY NOT NULL,
plugin_id TEXT NOT NULL,
kind TEXT NOT NULL,
name TEXT NOT NULL,
url TEXT,
can_remove INTEGER NOT NULL,
can_refresh INTEGER NOT NULL,
can_edit INTEGER NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
CREATE TABLE blob (
namespace TEXT NOT NULL,
key TEXT NOT NULL,
value TEXT NOT NULL,
PRIMARY KEY (namespace, key)
);
`);
db.prepare(
`INSERT INTO source (
id, plugin_id, kind, name, url, can_remove, can_refresh, can_edit, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
).run(
"src_1",
"plugin",
"remote",
"Imported",
null,
1,
0,
1,
1_700_000_000_000,
1_700_000_001_000,
);
db.prepare("INSERT INTO blob (namespace, key, value) VALUES (?, ?, ?)").run(
"scope_a/plugin",
"spec",
"{}",
);
db.close();
};
describe("importSqliteDataToFuma", () => {
it("imports current SQLite rows into FumaDB SQLite and moves the old DB aside", async () => {
const sqlitePath = join(workDir, "data.db");
const markerPath = join(workDir, "fumadb-sqlite-imported");
seedSqlite(sqlitePath);
const tables = collectTables([]);
sqlite = await createSqliteFumaDb({
tables,
namespace: "executor_local_test",
path: join(workDir, "target.db"),
});
const scopedDb = withQueryContext(sqlite.db, { allowedScopeIds: new Set(["scope_a"]) });
const result = await importSqliteDataToFuma({
sqlitePath,
markerPath,
target: scopedDb,
tables,
scopeId: "scope_a",
});
expect(result.imported).toBe(true);
expect(result.importedRows).toBe(2);
expect(result.importedTables).toEqual(["source", "blob"]);
expect(existsSync(markerPath)).toBe(true);
expect(existsSync(sqlitePath)).toBe(false);
expect(result.backupPath && existsSync(result.backupPath)).toBe(true);
const source = (await scopedDb.findFirst("source", {
where: (b) => b("id", "=", "src_1"),
})) as Record<string, unknown>;
expect(source.scope_id).toBe("scope_a");
expect(source.can_remove).toBe(true);
expect(source.can_refresh).toBe(false);
expect(source.can_edit).toBe(true);
expect(source.created_at).toBeInstanceOf(Date);
const blob = (await scopedDb.findFirst("blob", {
where: (b) => b("id", "=", JSON.stringify(["scope_a/plugin", "spec"])),
})) as Record<string, unknown>;
expect(blob.value).toBe("{}");
});
});