-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.ts
More file actions
62 lines (53 loc) · 1.98 KB
/
init_db.ts
File metadata and controls
62 lines (53 loc) · 1.98 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
import * as path from "path";
import { readdir } from "fs/promises";
import { D1Database, type D1ExecResult } from "@cloudflare/workers-types";
import assert from "assert";
// Loads all migration files into database
export async function applyMigrations(db: D1Database, migrationsPath: string) {
const migrationFiles = await readdir(migrationsPath);
migrationFiles.sort();
for (const filename of migrationFiles) {
if (filename.endsWith(".sql")) {
const file = Bun.file(path.join(migrationsPath, filename));
const migration = await file.text();
// we need to put each statement in a single line
const cleanedMigration = migration
.replace(/--.*/g, "")
.replace(/\n/g, " ")
.replace(/\s{2,}/g, " ")
// separate each statement in a new line to have a correct result.count
.replace(/;/g, "\n")
.trim();
assert(cleanedMigration.length > 0, "empty migration: " + filename);
const result = await db.exec(cleanedMigration);
const numCreate = cleanedMigration.match(/\bcreate\b/gi)?.length || 0;
assert(result.count > 0, "migrations execution failed");
assert(result.count === numCreate, "migrations execution failed");
}
}
}
const MIGRATIONS_PATH = path.resolve(__dirname, "../../../btcindexer/db/migrations");
// NOTE: Drop tables in correct order: child tables first to avoid foreign key constraints
export const tables = [
"nbtc_redeem_solutions",
"nbtc_utxos",
"nbtc_redeem_requests",
"nbtc_minting",
"nbtc_deposit_addresses",
"btc_blocks",
"indexer_state",
"presign_objects",
"cron_locks",
"setups",
];
export async function initDb(db: D1Database, path?: string) {
return applyMigrations(db, path || MIGRATIONS_PATH);
}
export function dropTables(db: D1Database) {
const dropStms = tables.map((t) => `DROP TABLE IF EXISTS ${t};`).join(" ");
return db.exec(dropStms);
}
export function purgeTables(db: D1Database, tableNames: string[]): Promise<D1ExecResult> {
const sql = tableNames.map((t) => `DELETE FROM ${t};`).join(" ");
return db.exec(sql);
}