drop DB functionality added

This commit is contained in:
Dmitry Anderson 2024-10-25 20:10:48 +02:00
parent ce6802faff
commit a1e6284a16
2 changed files with 89 additions and 66 deletions

View File

@ -10,6 +10,26 @@ import pg from "npm:pg";
import { Database } from "./scheme.ts"
import { CompiledConfig } from "../cfg/config.ts";
export const dropDb = async (migrator: Migrator, downLimit?: number) => {
console.log(`droping the DB`)
let downCounter: number = 0 // to prevent infinite loop
while (true) {
if (downLimit && downCounter === downLimit) return
const { results } = await migrator.migrateDown()
if (!results) continue
for (const { status, migrationName } of results) {
if (status === "Error" || status === "NotExecuted") {
return
}
console.log(`down migration "${migrationName}" was executed successfully`)
}
downCounter++
}
}
export type SetupDbOutput = {
pool: pg.Pool,
db: Kysely<Database>,
@ -42,6 +62,9 @@ export const setupDB = async (cfg: CompiledConfig): Promise<SetupDbOutput> => {
allowUnorderedMigrations: true
})
if (cfg.migrationCfg.dropDb) {
await dropDb(migrator, 100)
}
const { error, results } = await migrator.migrateToLatest()
results?.forEach((it) => {