From 439df3edfa04c5f5a10a5be33c51ad5e602c7248 Mon Sep 17 00:00:00 2001 From: Matt Burstein Date: Mon, 27 Jun 2022 23:07:29 +0100 Subject: [PATCH] add dryRun to migrate --- README.md | 26 ++++ .../dry-run-false/1_dry_run_false.sql | 3 + .../1_dry_run_no_migrations.sql | 3 + .../dry-run-true/1_dry_run_true_first.sql | 3 + .../dry-run-true/2_dry_run_true_second.js | 3 + src/__tests__/migrate.ts | 122 ++++++++++++++++++ src/migrate.ts | 36 +++++- src/types.ts | 1 + 8 files changed, 192 insertions(+), 5 deletions(-) create mode 100644 src/__tests__/fixtures/dry-run-false/1_dry_run_false.sql create mode 100644 src/__tests__/fixtures/dry-run-no-migrations/1_dry_run_no_migrations.sql create mode 100644 src/__tests__/fixtures/dry-run-true/1_dry_run_true_first.sql create mode 100644 src/__tests__/fixtures/dry-run-true/2_dry_run_true_second.js diff --git a/README.md b/README.md index 5247883..0214b58 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,32 @@ async function() { } ``` +### Config + +#### `logger` + +This will be used for all logging from `postgres-migrations`. The function will be called passing a +string argument with each call. + +#### `dryRun` + +Setting `dryRun` to `true` will log the filenames and SQL of all transactions to be run, without running them. It validates that the file names are the appropriate format, but it does not perform any validation of the migrations themselves so they may still fail after running. + +The logs look like this: + +``` +Migrations to run: + 1_first_migration.sql + 2_second_migration.js + +CREATE TABLE first_migration_table ( + id integer +); + +ALTER TABLE first_migration_table + ADD second_migration_column integer; +``` + ### Validating migration files Occasionally, if two people are working on the same codebase independently, they might both create a migration at the same time. For example, `5_add-table.sql` and `5_add-column.sql`. If these both get pushed, there will be a conflict. diff --git a/src/__tests__/fixtures/dry-run-false/1_dry_run_false.sql b/src/__tests__/fixtures/dry-run-false/1_dry_run_false.sql new file mode 100644 index 0000000..23ef39d --- /dev/null +++ b/src/__tests__/fixtures/dry-run-false/1_dry_run_false.sql @@ -0,0 +1,3 @@ +CREATE TABLE dry_run_false ( + id integer +); diff --git a/src/__tests__/fixtures/dry-run-no-migrations/1_dry_run_no_migrations.sql b/src/__tests__/fixtures/dry-run-no-migrations/1_dry_run_no_migrations.sql new file mode 100644 index 0000000..23c6bc4 --- /dev/null +++ b/src/__tests__/fixtures/dry-run-no-migrations/1_dry_run_no_migrations.sql @@ -0,0 +1,3 @@ +CREATE TABLE dry_run_no_migrations ( + id integer +); diff --git a/src/__tests__/fixtures/dry-run-true/1_dry_run_true_first.sql b/src/__tests__/fixtures/dry-run-true/1_dry_run_true_first.sql new file mode 100644 index 0000000..4b1d8f1 --- /dev/null +++ b/src/__tests__/fixtures/dry-run-true/1_dry_run_true_first.sql @@ -0,0 +1,3 @@ +CREATE TABLE dry_run_true ( + id integer +); diff --git a/src/__tests__/fixtures/dry-run-true/2_dry_run_true_second.js b/src/__tests__/fixtures/dry-run-true/2_dry_run_true_second.js new file mode 100644 index 0000000..c184262 --- /dev/null +++ b/src/__tests__/fixtures/dry-run-true/2_dry_run_true_second.js @@ -0,0 +1,3 @@ +module.exports.generateSql = () =>` +ALTER TABLE dry_run_true + ADD new_column integer;` diff --git a/src/__tests__/migrate.ts b/src/__tests__/migrate.ts index 0503395..2083972 100644 --- a/src/__tests__/migrate.ts +++ b/src/__tests__/migrate.ts @@ -4,6 +4,8 @@ import * as pg from "pg" import SQL from "sql-template-strings" import {createDb, migrate, MigrateDBConfig} from "../" import {PASSWORD, startPostgres, stopPostgres} from "./fixtures/docker-postgres" +import * as sinon from "sinon" +import {SinonSpy} from "sinon" const CONTAINER_NAME = "pg-migrations-test-migrate" @@ -284,6 +286,119 @@ test("successful complex js migration", (t) => { }) }) +test("with dryRun true", (t) => { + const logSpy = sinon.spy() + const databaseName = "migration-with-dryRun-true-test" + const dbConfig = { + database: databaseName, + user: "postgres", + password: PASSWORD, + host: "localhost", + port, + } + + const expectedLog = ` +Migrations to run: + 1_dry_run_true_first.sql + 2_dry_run_true_second.js + +CREATE TABLE dry_run_true ( + id integer +); + +ALTER TABLE dry_run_true + ADD new_column integer; +` + + return createDb(databaseName, dbConfig) + .then(() => migrate(dbConfig, "src/__tests__/fixtures/empty")) + .then(() => + migrate(dbConfig, "src/__tests__/fixtures/dry-run-true", { + dryRun: true, + logger: logSpy, + }), + ) + .then(() => doesTableExist(dbConfig, "dry_run_true")) + .then((exists) => { + t.falsy(exists) + t.assert( + logSpy.calledWith(expectedLog), + `expected logger to have been called with ${expectedLog} but was called with ${allArgsForCalls( + logSpy, + )}`, + ) + }) +}) + +test("with dryRun true but no migrations to run", (t) => { + const logSpy = sinon.spy() + const databaseName = "migration-with-dryRun-no-migrations-test" + const dbConfig = { + database: databaseName, + user: "postgres", + password: PASSWORD, + host: "localhost", + port, + } + + const expectedLog = "\nNo new migrations to run\n" + + return createDb(databaseName, dbConfig) + .then(() => + migrate(dbConfig, "src/__tests__/fixtures/dry-run-no-migrations"), + ) + .then(() => doesTableExist(dbConfig, "dry_run_no_migrations")) + .then((exists) => { + t.truthy(exists) + }) + .then(() => + migrate(dbConfig, "src/__tests__/fixtures/dry-run-no-migrations", { + dryRun: true, + logger: logSpy, + }), + ) + .then(() => { + t.assert( + logSpy.calledWith(expectedLog), + `expected logger to have been called with ${expectedLog} but was called with ${allArgsForCalls( + logSpy, + )}`, + ) + }) +}) + +test("with dryRun false", (t) => { + const logSpy = sinon.spy() + const databaseName = "migration-with-dryRun-false-test" + const dbConfig = { + database: databaseName, + user: "postgres", + password: PASSWORD, + host: "localhost", + port, + } + + const unexpectedLog = "CREATE TABLE dry_run_false" + + return createDb(databaseName, dbConfig) + .then(() => + migrate(dbConfig, "src/__tests__/fixtures/dry-run-false", { + dryRun: false, + logger: logSpy, + }), + ) + .then(() => doesTableExist(dbConfig, "dry_run_false")) + .then((exists) => { + t.truthy(exists) + t.assert( + logSpy.neverCalledWithMatch(unexpectedLog), + `expected logger not to be called with string matching ${unexpectedLog} but was called with ${allArgsForCalls( + logSpy, + )}`, + ) + }) +}) + test("bad arguments - no db config", (t) => { // tslint:disable-next-line no-any return t.throwsAsync((migrate as any)()).then((err) => { @@ -720,3 +835,10 @@ function doesTableExist(dbConfig: pg.ClientConfig, tableName: string) { } }) } + +function allArgsForCalls(spy: SinonSpy) { + return spy + .getCalls() + .map((call) => call.args) + .join(" | ") +} diff --git a/src/migrate.ts b/src/migrate.ts index e00807b..794c2e1 100644 --- a/src/migrate.ts +++ b/src/migrate.ts @@ -38,6 +38,8 @@ export async function migrate( // } + const dryRun = config.dryRun === undefined ? false : config.dryRun + if (dbConfig == null) { throw new Error("No config object") } @@ -51,7 +53,7 @@ export async function migrate( // we have been given a client to use, it should already be connected return withAdvisoryLock( log, - runMigrations(intendedMigrations, log), + runMigrations(intendedMigrations, log, dryRun), )(dbConfig.client) } @@ -99,15 +101,19 @@ export async function migrate( const runWith = withConnection( log, - withAdvisoryLock(log, runMigrations(intendedMigrations, log)), + withAdvisoryLock(log, runMigrations(intendedMigrations, log, dryRun)), ) return runWith(client) } } -function runMigrations(intendedMigrations: Array, log: Logger) { - return async (client: BasicPgClient) => { +function runMigrations( + intendedMigrations: Array, + log: Logger, + dryRun: boolean, +) { + return async (client: BasicPgClient): Promise> => { try { const migrationTableName = "migrations" @@ -125,7 +131,12 @@ function runMigrations(intendedMigrations: Array, log: Logger) { intendedMigrations, appliedMigrations, ) - const completedMigrations = [] + const completedMigrations: Array = [] + + if (dryRun) { + logDryRun(migrationsToRun, log) + return completedMigrations + } for (const migration of migrationsToRun) { log(`Starting migration: ${migration.id} ${migration.name}`) @@ -211,3 +222,18 @@ async function doesTableExist(client: BasicPgClient, tableName: string) { return result.rows.length > 0 && result.rows[0].exists } + +function logDryRun(migrations: Array, log: Logger) { + if (migrations.length === 0) { + log("\nNo new migrations to run\n") + } else { + const logString = + "\nMigrations to run:\n" + + migrations.map((m) => ` ${m.fileName}`).join("\n") + + "\n\n" + + migrations.map((m) => m.sql.trim()).join("\n\n") + + "\n" + + log(logString) + } +} diff --git a/src/types.ts b/src/types.ts index 5e16f9a..8c6658a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -59,6 +59,7 @@ export type Config = Partial export interface FullConfig { readonly logger: Logger + readonly dryRun: boolean } export class MigrationError extends Error {