-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrator.ts
59 lines (49 loc) · 1.4 KB
/
migrator.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
52
53
54
55
56
57
58
59
import * as path from "path";
import {promises as fs} from "fs";
import {Database} from "./app/database";
import {Kysely, Migrator, FileMigrationProvider, PostgresDialect} from "kysely";
import dotenv from "dotenv";
import {fileURLToPath} from "url";
import {dirname} from "path";
import pg from "pg";
const {Pool} = pg;
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
dotenv.config();
export const migrator = async () => {
const db = new Kysely<Database>({
dialect: new PostgresDialect({
pool: new Pool({
database: process.env.DATABASE_NAME,
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
port: Number(process.env.DATABASE_PORT),
max: 10,
password: process.env.DATABASE_PWD,
}),
}),
});
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
path,
migrationFolder: path.join(__dirname, "./migrations"),
}),
});
const {error, results} = await migrator.migrateToLatest();
results?.forEach((it) => {
if (it.status === "Success") {
console.log(`migration "${it.migrationName}" was executed successfully`);
} else if (it.status === "Error") {
console.error(`failed to execute migration "${it.migrationName}"`);
}
});
if (error) {
console.error("failed to migrate");
console.error(JSON.stringify(error, null, 4));
process.exit(1);
}
await db.destroy();
};
migrator();