diff --git a/config.ts b/config.ts index 4c31b26..d743f04 100644 --- a/config.ts +++ b/config.ts @@ -13,30 +13,30 @@ export const DEFAULT_DB_PORT = 5432 export const DEFAULT_DB_TLS = false export const loadConfig = (): CompiledConfig => { - const cfg = getConfig() - const pgPoolCfg: PoolConfig = { - host: cfg.db_host || DEFAULT_DB_HOST, - user: cfg.db_user || DEFAULT_DB_USER, - database: cfg.db_name || DEFAULT_DB_NAME, - port: cfg.db_port || DEFAULT_DB_PORT, - ssl: cfg.db_tls || DEFAULT_DB_TLS, - password: cfg.db_password, - } - const botCfg: BotConfig = { - mode: BotMode.normal, - bot_token: cfg.bot_token, - chat_id: MIC_CHAT_ID, - admin_ids: [ - MR_ANDERSON_ID, - ] - } - return { - pgPoolCfg, botCfg, - migrationCfg: { - dropDb: cfg.drop_db, - applyMigrations: cfg.apply_migrations, - migrationsPath: import.meta.dirname ? - path.join(import.meta.dirname, 'migrations') : '/app/migrations', - } + const cfg = getConfig() + const pgPoolCfg: PoolConfig = { + host: cfg.db_host || DEFAULT_DB_HOST, + user: cfg.db_user || DEFAULT_DB_USER, + database: cfg.db_name || DEFAULT_DB_NAME, + port: cfg.db_port || DEFAULT_DB_PORT, + ssl: cfg.db_tls || DEFAULT_DB_TLS, + password: cfg.db_password, + } + const botCfg: BotConfig = { + mode: BotMode.normal, + bot_token: cfg.bot_token, + chat_id: MIC_CHAT_ID, + admin_ids: [ + MR_ANDERSON_ID, + ] + } + return { + pgPoolCfg, botCfg, + migrationCfg: { + dropDb: cfg.drop_db, + applyMigrations: cfg.apply_migrations, + migrationsPath: import.meta.dirname ? + path.join(import.meta.dirname, 'migrations') : '/app/migrations', } + } } diff --git a/repo/setup_db.ts b/repo/setup_db.ts index 7cb236d..e27e5b6 100644 --- a/repo/setup_db.ts +++ b/repo/setup_db.ts @@ -10,54 +10,77 @@ 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, } export const setupDB = async (cfg: CompiledConfig): Promise => { - console.log("creating db instance: ", cfg.pgPoolCfg) - const pool = new pg.Pool(cfg.pgPoolCfg) - const dialect = new PostgresDialect({ pool }) - const db = new Kysely({ dialect }) + console.log("creating db instance: ", cfg.pgPoolCfg) + const pool = new pg.Pool(cfg.pgPoolCfg) + const dialect = new PostgresDialect({ pool }) + const db = new Kysely({ dialect }) - if (cfg.migrationCfg?.applyMigrations) { - console.log("applying db migrations", cfg.migrationCfg.migrationsPath) - const migrator = new Migrator({ - db, - provider: new FileMigrationProvider({ - fs: { - readdir(path) { - return Promise.resolve( - [...Deno.readDirSync(path)].map((file) => file.name) - ) - }, - }, - path: { - join(...path) { - return path.join('/') - }, - }, - migrationFolder: cfg.migrationCfg.migrationsPath, - }), - allowUnorderedMigrations: true - }) + if (cfg.migrationCfg?.applyMigrations) { + console.log("applying db migrations", cfg.migrationCfg.migrationsPath) + const migrator = new Migrator({ + db, + provider: new FileMigrationProvider({ + fs: { + readdir(path) { + return Promise.resolve( + [...Deno.readDirSync(path)].map((file) => file.name) + ) + }, + }, + path: { + join(...path) { + return path.join('/') + }, + }, + migrationFolder: cfg.migrationCfg.migrationsPath, + }), + allowUnorderedMigrations: true + }) - 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) - throw error - } + if (cfg.migrationCfg.dropDb) { + await dropDb(migrator, 100) } + const { error, results } = await migrator.migrateToLatest() - return { pool, db } + 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) + throw error + } + } + + return { pool, db } }