Skip to content

Commit

Permalink
feat: expose enableReverseMode from migrator (#1346)
Browse files Browse the repository at this point in the history
Co-authored-by: Shinigami <[email protected]>
  • Loading branch information
lorefnon and Shinigami92 authored Feb 21, 2025
1 parent 10dcd34 commit cfe1d96
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,12 @@ export interface MigrationBuilder {
*/
noTransaction: () => void;

/**
* Run the reverse of the migration. Useful for creating a new migration that
* reverts a previous migration.
*/
enableReverseMode(): this;

/**
* The `db` client instance.
*
Expand Down
6 changes: 6 additions & 0 deletions test/1739900132875_names_reversed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { up as originalUp } from './1414549381268_names_promise.js';

export const up = (pgm) => {
pgm.enableReverseMode();
return originalUp(pgm);
};
22 changes: 22 additions & 0 deletions test/migration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import type { Logger, RunnerOption } from '../src/types';

const callbackMigration = '1414549381268_names.js';
const promiseMigration = '1414549381268_names_promise.js';
const reverseMigration = '1739900132875_names_reversed.js';
const migrationsTable = 'pgmigrations';

const actionsCallback = await import(`./${callbackMigration}`);
const actionsPromise = await import(`./${promiseMigration}`);
const reversePromise = await import(`./${reverseMigration}`);

describe('migration', () => {
const dbMock = {} as DBConnection;
Expand Down Expand Up @@ -154,6 +156,26 @@ describe('migration', () => {
expect(queryMock).toHaveBeenCalled();
});

it('should perform the inferred reverse operation when reverseMode is enabled', async () => {
const migration = new Migration(
dbMock,
reverseMigration,
reversePromise,
options,
{},
logger
);

await migration.apply('up');

expect(queryMock).toHaveBeenCalledTimes(4);
expect(queryMock).toHaveBeenNthCalledWith(1, 'BEGIN;');
expect(queryMock).toHaveBeenNthCalledWith(
2,
expect.stringMatching('DROP TABLE')
);
});

it('should not call db.query on --dry-run', async () => {
const migration = new Migration(
dbMock,
Expand Down

0 comments on commit cfe1d96

Please sign in to comment.