-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate.ts
40 lines (34 loc) · 1007 Bytes
/
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
import { promises as fs } from 'fs'
import * as path from 'path'
import { fileURLToPath } from 'url'
import { FileMigrationProvider, Migrator } from 'kysely'
import { db } from './app/db.ts'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
async function migrateToLatest() {
const migrator = new Migrator({
db,
provider: new FileMigrationProvider({
fs,
path,
// This needs to be an absolute 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(error)
process.exit(1)
}
await db.destroy()
console.log('Migrations done.')
}
migrateToLatest()