47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { promises as fs } from "fs"
|
|
import { FileMigrationProvider, Migrator } from "kysely"
|
|
import * as path from "path"
|
|
import { db } from "./database"
|
|
import { fileURLToPath } from "url"
|
|
|
|
let dirname
|
|
|
|
try {
|
|
dirname = __dirname
|
|
} catch {
|
|
const filename = fileURLToPath(import.meta.url)
|
|
dirname = path.dirname(filename)
|
|
}
|
|
|
|
const migrator = new Migrator({
|
|
db,
|
|
provider: new FileMigrationProvider({
|
|
fs,
|
|
path,
|
|
migrationFolder: path.join(dirname, "migrations"),
|
|
}),
|
|
})
|
|
|
|
async function migrateToLatest() {
|
|
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)
|
|
}
|
|
}
|
|
|
|
export async function resetAll() {
|
|
while ((await migrator.migrateDown()).error !== undefined) {}
|
|
}
|
|
|
|
export default migrateToLatest
|