-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate.ts
More file actions
26 lines (20 loc) · 870 Bytes
/
Copy pathmigrate.ts
File metadata and controls
26 lines (20 loc) · 870 Bytes
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
// Drops + recreates the SQLite schema at ./dev.db by running every
// migration file under ./migrations/*.sql. `tg generate` reads
// ./dev.db to introspect the resulting schema.
import Database from "better-sqlite3";
import * as fs from "node:fs";
import * as path from "node:path";
// Fresh file each run so DROP TABLE isn't needed inside the SQL.
const dbPath = "./dev.db";
if (fs.existsSync(dbPath)) { fs.unlinkSync(dbPath); }
const db = new Database(dbPath);
db.pragma("foreign_keys = ON");
const migrationsDir = path.resolve(import.meta.dirname, "migrations");
const files = fs.readdirSync(migrationsDir).filter((f) => f.endsWith(".sql")).sort();
for (const file of files) {
const sqlText = fs.readFileSync(path.join(migrationsDir, file), "utf-8");
console.log(`Running ${file}...`);
db.exec(sqlText);
}
db.close();
console.log(`Migrated ${dbPath}`);