Skip to content

Commit b63dd7f

Browse files
committed
refactor(migrator): migrator inherits from umzug, add test cases
closes #4
1 parent 59146dc commit b63dd7f

File tree

6 files changed

+41
-27
lines changed

6 files changed

+41
-27
lines changed

package-lock.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"eslint": "^6.8.0",
6060
"husky": "^4.2.5",
6161
"jest": "^25.1.0",
62+
"mockdate": "^2.0.5",
6263
"semantic-release": "^17.0.7",
6364
"ts-jest": "^25.5.0",
6465
"typescript": "^3.8.3"

src/commands/migrate-undo-all.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BaseCliOptions, baseHandler, baseOptions } from '../core/yargs';
44
import logger from '../helpers/logger';
55

66
interface CliOptions extends BaseCliOptions {
7-
to?: string | number;
7+
to: string | 0;
88
}
99

1010
export default {

src/commands/migrate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { BaseCliOptions, baseHandler, baseOptions } from '../core/yargs';
55
import logger from '../helpers/logger';
66

77
interface CliOptions extends BaseCliOptions {
8-
to?: string;
9-
from?: string;
8+
to: string;
9+
from: string;
1010
}
1111

1212
export default {

src/core/migrator.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DocumentClient } from 'aws-sdk/clients/dynamodb';
2-
import nodePlop from 'node-plop/lib/node-plop';
2+
import nodePlop from 'node-plop';
33
import path from 'path';
44
import { pick } from 'ramda';
55
import Umzug from 'umzug';
@@ -19,8 +19,11 @@ export interface MigratorOptions {
1919
attributeName?: string;
2020
}
2121

22-
export class Migrator {
23-
private umzug: Umzug.Umzug;
22+
interface Generator {
23+
generate(migrationName: string): Promise<void>;
24+
}
25+
26+
export class Migrator extends Umzug implements Generator {
2427
private generator;
2528

2629
/**
@@ -38,7 +41,7 @@ export class Migrator {
3841
tableName = tableName || 'migrations';
3942
attributeName = attributeName || 'name';
4043

41-
this.umzug = new Umzug({
44+
super({
4245
storage: new DynamoDBStorage({ dynamodb, tableName, attributeName }),
4346
migrations: {
4447
params: [dynamodb],
@@ -53,26 +56,6 @@ export class Migrator {
5356
async generate(migrationName: string) {
5457
await this.generator.runActions({timestamp: getCurrentYYYYMMDDHHmms(), name: migrationName});
5558
}
56-
57-
execute(options?: Umzug.ExecuteOptions) {
58-
return this.umzug.execute(options);
59-
}
60-
61-
pending() {
62-
return this.umzug.pending();
63-
}
64-
65-
executed() {
66-
return this.umzug.executed();
67-
}
68-
69-
up(options?) {
70-
return this.umzug.up(options);
71-
}
72-
73-
down(options?) {
74-
return this.umzug.down(options);
75-
}
7659
}
7760

7861
/**

tests/core/migrator.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,40 @@
1+
const runActions = jest.fn();
2+
const getGenerator = jest.fn().mockReturnValue({ runActions });
3+
const nodePlop = jest.fn().mockReturnValue({ getGenerator });
4+
jest.mock('node-plop', () => ({ __esModule: true, default: nodePlop }));
5+
16
import * as AWS from 'aws-sdk';
27
import * as AWSMock from 'aws-sdk-mock';
8+
import mockdate from 'mockdate';
39

410
import { Migrator } from '../../src/core/migrator';
511

612
describe('Migrator tests', () => {
713
beforeAll((done) => {
814
AWSMock.setSDKInstance(AWS);
15+
mockdate.set(Date.UTC(2020, 4, 11));
916
done();
1017
});
1118

1219
it('Should not migrator factory be thrown', async () => {
1320
const factory = () => new Migrator();
1421
expect(factory).not.toThrow();
1522
});
23+
24+
it('Should run generator action', async () => {
25+
const migrator = new Migrator();
26+
27+
await migrator.generate('test');
28+
29+
expect(nodePlop).toBeCalled();
30+
expect(getGenerator).toBeCalled();
31+
expect(getGenerator).toBeCalledWith('migration');
32+
expect(runActions).toBeCalledTimes(1);
33+
expect(runActions).toBeCalledWith({timestamp: '20200511000000', name: 'test'});
34+
});
35+
36+
afterAll((done) => {
37+
mockdate.reset();
38+
done();
39+
});
1640
});

0 commit comments

Comments
 (0)