Skip to content

Commit

Permalink
fix: allow logging migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Nov 16, 2023
1 parent c170280 commit 9c56c19
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/postgres/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import { logger } from '../logger';
import { PgConnectionArgs, connectPostgres, standardizedConnectionArgs } from './connection';
import { isDevEnv, isTestEnv } from '../helpers/values';

export interface MigrationOptions {
// Bypass the NODE_ENV check when performing a "down" migration which irreversibly drops data.
dangerousAllowDataLoss?: boolean;
logMigrations?: boolean;
}

/**
* Run migrations in one direction.
* @param dir - Migrations directory
* @param direction - Migration direction (`'down'` or `'up'`)
* @param connectionArgs - Postgres connection args
* @param opts - Migration options
*/
export async function runMigrations(
dir: string,
direction: MigrationDirection,
connectionArgs?: PgConnectionArgs,
opts?: {
// Bypass the NODE_ENV check when performing a "down" migration which irreversibly drops data.
dangerousAllowDataLoss?: boolean;
}
opts?: MigrationOptions
) {
if (!opts?.dangerousAllowDataLoss && direction !== 'up' && !isTestEnv && !isDevEnv) {
throw new Error(
Expand All @@ -43,7 +47,7 @@ export async function runMigrations(
},
migrationsTable: 'pgmigrations',
logger: {
info: _msg => {},
info: msg => (opts?.logMigrations === true ? logger.info(msg) : {}),
warn: msg => logger.warn(msg),
error: msg => logger.error(msg),
},
Expand All @@ -54,24 +58,23 @@ export async function runMigrations(
* Cycle migrations down and up.
* @param dir - Migrations directory
* @param connectionArgs - Postgres connection args
* @param opts - Migration options
*/
export async function cycleMigrations(
dir: string,
connectionArgs?: PgConnectionArgs,
opts?: {
// Bypass the NODE_ENV check when performing a "down" migration which irreversibly drops data.
dangerousAllowDataLoss?: boolean;
opts?: MigrationOptions & {
checkForEmptyData?: boolean;
}
) {
await runMigrations(dir, 'down', connectionArgs);
await runMigrations(dir, 'down', connectionArgs, opts);
if (
opts?.checkForEmptyData &&
(await databaseHasData(connectionArgs, { ignoreMigrationTables: true }))
) {
throw new Error('Migration down process did not completely remove DB tables');
}
await runMigrations(dir, 'up', connectionArgs);
await runMigrations(dir, 'up', connectionArgs, opts);
}

/**
Expand Down

0 comments on commit 9c56c19

Please sign in to comment.