Skip to content

Commit

Permalink
feat: return migration meta from up/down (#367)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal authored Oct 31, 2020
1 parent 807f02e commit 642b4e4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ umzug.json
.vscode
coverage
test/generated
*.tgz
8 changes: 6 additions & 2 deletions src/umzug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export class Umzug<Ctx> 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<void> {
async up(options: MigrateUpOptions = {}): Promise<MigrationMeta[]> {
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
Expand Down Expand Up @@ -281,13 +281,15 @@ export class Umzug<Ctx> 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<void> {
async down(options: MigrateDownOptions = {}): Promise<MigrationMeta[]> {
const eligibleMigrations = async () => {
if (options.migrations && options.rerun === RerunBehavior.ALLOW) {
const list = await this.migrations();
Expand Down Expand Up @@ -331,6 +333,8 @@ export class Umzug<Ctx> 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<RunnableMigration<Ctx>>, name: string) {
Expand Down
25 changes: 25 additions & 0 deletions test/umzug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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<Promise<Array<{ name: string; path?: string }>>>();
down.returns.toEqualTypeOf<Promise<Array<{ name: string; path?: string }>>>();
});

test('pending', () => {
Expand Down

0 comments on commit 642b4e4

Please sign in to comment.