-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmigrate.ts
51 lines (44 loc) · 1.24 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 "./env.js";
import path from "path";
import url from "url";
import fs from "fs/promises";
import { Migrator, FileMigrationProvider } from "kysely";
import { db } from "./database.js";
const option = process.env.MIGRATE_OPTION ?? "latest";
const dirname = url.fileURLToPath(new URL(".", import.meta.url));
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
path,
migrationFolder: path.join(dirname, "./migrations"),
}),
});
if (option === "list") {
const migrations = await migrator.getMigrations();
for (const migration of migrations) {
console.log(`${migration.name} - Executed at: ${migration.executedAt}`);
}
await db.destroy();
process.exit(0);
}
const { error, results } = await (option === "latest"
? migrator.migrateToLatest()
: migrator.migrateDown());
results?.forEach((it) => {
if (it.status === "Success") {
console.log(
`[${it.direction}] migration "${it.migrationName}" was executed successfully!`
);
} else if (it.status === "Error") {
console.error(
`[${it.direction}] failed to execute migration "${it.migrationName}"!`
);
}
});
if (error) {
console.error("Failed to migrate:");
console.error(error);
process.exit(1);
}
await db.destroy();