forked from jherr/trpc-on-the-app-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.ts
62 lines (53 loc) · 1.66 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
52
53
54
55
56
57
58
59
60
61
62
import {
PostgresDialect,
Kysely,
Migrator,
FileMigrationProvider,
} from "kysely";
import path from "path";
import { Pool } from "pg";
import { Database } from "./src/db/schema";
import { promises as fs } from "fs";
require('dotenv').config({ path: './.env.local' })
async function migrateToLatest() {
const dialect = new PostgresDialect({
pool: new Pool({
database: process.env.POSTGRES_DATABASE,
host: process.env.POSTGRES_HOST,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
port: Number.isNaN(parseInt(process.env.POSTGRES_PORT as string)) ? undefined: parseInt(process.env.POSTGRES_PORT as string),
max: 10,
ssl: process.env.NODE_ENV === 'production' ? true : false
}),
});
const db = new Kysely<Database>({
dialect,
});
const migrator = new Migrator({
db: db,
provider: new FileMigrationProvider({
fs,
path,
// This needs to be an absolute path.
migrationFolder: path.join(__dirname, "./migrations"),
}),
});
const m = await migrator.getMigrations();
console.log(m);
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(error);
process.exit(1);
}
await db.destroy();
}
migrateToLatest();