relay filter/appview bootstrap
1import * as path from 'path'
2import { promises as fs } from 'fs'
3import { Migrator, FileMigrationProvider } from 'kysely'
4import { db } from '../db'
5
6async function migrate(direction: 'up' | 'down' | 'latest') {
7 const migrator = new Migrator({
8 db,
9 provider: new FileMigrationProvider({
10 fs,
11 path,
12 migrationFolder: path.join(__dirname, '../../migrations'),
13 }),
14 })
15
16 console.log(`Running migration: ${direction}`);
17
18 if (direction === 'down') {
19 const { error, results } = await migrator.migrateDown()
20 results?.forEach((it) => {
21 if (it.status === 'Success') {
22 console.log(`migration "${it.migrationName}" was reverted successfully`)
23 } else if (it.status === 'Error') {
24 console.error(`failed to revert migration "${it.migrationName}"`)
25 }
26 })
27 if (error) {
28 console.error('failed to migrate')
29 console.error(error)
30 process.exit(1)
31 }
32 }
33
34 if (direction === 'up' || direction === 'latest') {
35 const { error, results } = await migrator.migrateToLatest()
36 results?.forEach((it) => {
37 if (it.status === 'Success') {
38 console.log(`migration "${it.migrationName}" was executed successfully`)
39 } else if (it.status === 'Error') {
40 console.error(`failed to execute migration "${it.migrationName}"`)
41 }
42 })
43 if (error) {
44 console.error('failed to migrate')
45 console.error(error)
46 process.exit(1)
47 }
48 }
49
50 await db.destroy()
51}
52
53const direction = process.argv[2] as 'up' | 'down' | 'latest' | undefined;
54
55if (!direction || !['up', 'down', 'latest'].includes(direction)) {
56 console.error('Please provide a migration direction: up, down, or latest');
57 process.exit(1);
58}
59
60migrate(direction);