forked from undb-io/undb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.ts
51 lines (39 loc) · 1.34 KB
/
migrate.ts
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
import crypto from "node:crypto"
import fs from "node:fs"
import path from "node:path"
import url from "node:url"
const { default: journal } = await import("../apps/backend/drizzle/meta/_journal.json", {
with: { type: "json" },
})
interface Migration {
idx: number
when: number
tag: string
hash: string
sql: string[]
}
const migrate: Migration[] = []
const root = path.resolve(url.fileURLToPath(path.dirname(import.meta.url)), "..")
const outdir = path.resolve(root, "./apps/backend/migrations/")
const outfile = path.resolve(outdir, "deployment.json")
console.log()
for (let index = 0; index < journal.entries.length; index++) {
const { when, idx, tag } = journal.entries[index]
console.log('(%d) Parsing migration tagged "%s"', index + 1, tag)
const filepath = path.resolve(root, "./apps/backend/drizzle", `${tag}.sql`)
const migration_file = fs.readFileSync(filepath).toString()
migrate.push({
idx,
when,
tag,
hash: crypto.createHash("sha256").update(migration_file).digest("hex"),
sql: migration_file
.replace(/\n\t?/g, "")
.split("--> statement-breakpoint")
.map((x) => x.trim()),
})
}
if (fs.existsSync(outdir) === false) fs.mkdirSync(outdir)
fs.writeFileSync(outfile, JSON.stringify(migrate, null, 2))
console.log()
console.log('Migration deployment config file written out to "%s"\n', outfile)