From 642b4e47c727591a6d6bd65c79ee4db5d2126f8e Mon Sep 17 00:00:00 2001 From: Misha Kaletsky <15040698+mmkal@users.noreply.github.com> Date: Sat, 31 Oct 2020 14:41:00 -0400 Subject: [PATCH] feat: return migration meta from up/down (#367) --- .gitignore | 1 + src/umzug.ts | 8 ++++++-- test/umzug.test.ts | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 18964778..858d6a18 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ umzug.json .vscode coverage test/generated +*.tgz diff --git a/src/umzug.ts b/src/umzug.ts index 96f7b627..12f0bf36 100644 --- a/src/umzug.ts +++ b/src/umzug.ts @@ -238,7 +238,7 @@ export class Umzug extends EventEmitter { * Apply migrations. By default, runs all pending migrations. * @see MigrateUpOptions for other use cases using `to`, `migrations` and `rerun`. */ - async up(options: MigrateUpOptions = {}): Promise { + async up(options: MigrateUpOptions = {}): Promise { const eligibleMigrations = async () => { if (options.migrations && options.rerun === RerunBehavior.ALLOW) { // Allow rerun means the specified migrations should be run even if they've run before - so get all migrations, not just pending @@ -281,13 +281,15 @@ export class Umzug extends EventEmitter { this.logging(`== ${m.name}: migrated (${duration}s)\n`); this.emit('migrated', m.name, m); } + + return toBeApplied.map(m => ({ name: m.name, path: m.path })); } /** * Revert migrations. By default, the last executed migration is reverted. * @see MigrateDownOptions for other use cases using `to`, `migrations` and `rerun`. */ - async down(options: MigrateDownOptions = {}): Promise { + async down(options: MigrateDownOptions = {}): Promise { const eligibleMigrations = async () => { if (options.migrations && options.rerun === RerunBehavior.ALLOW) { const list = await this.migrations(); @@ -331,6 +333,8 @@ export class Umzug extends EventEmitter { this.logging(`== ${m.name}: reverted (${duration}s)\n`); this.emit('reverted', m.name, m); } + + return toBeReverted.map(m => ({ name: m.name, path: m.path })); } private findNameIndex(migrations: Array>, name: string) { diff --git a/test/umzug.test.ts b/test/umzug.test.ts index bfe0e700..07343b13 100644 --- a/test/umzug.test.ts +++ b/test/umzug.test.ts @@ -243,6 +243,28 @@ describe('alternate migration inputs', () => { ]); }); + test('up and down return migration meta array', async () => { + const umzug = new Umzug({ + migrations: [ + { name: 'm1', path: 'm1.sql', up: async () => {} }, + { name: 'm2', path: 'm2.sql', up: async () => {} }, + ], + logger: undefined, + }); + + const upResults = await umzug.up(); + expect(upResults).toEqual([ + { name: 'm1', path: 'm1.sql' }, + { name: 'm2', path: 'm2.sql' }, + ]); + + const downResults = await umzug.down({ to: 0 }); + expect(downResults).toEqual([ + { name: 'm2', path: 'm2.sql' }, + { name: 'm1', path: 'm1.sql' }, + ]); + }); + test('with migrations array', async () => { const spy = jest.fn(); @@ -514,6 +536,9 @@ describe('types', () => { // @ts-expect-error (`{ to: 0 }` is a special case. `{ to: 1 }` shouldn't be allowed) up.toBeCallableWith({ to: 1 }); + + up.returns.toEqualTypeOf>>(); + down.returns.toEqualTypeOf>>(); }); test('pending', () => {