|
| 1 | +// tslint:disable:max-file-line-count |
1 | 2 | import * as assert from 'assert';
|
2 | 3 | import * as assertRejects from 'assert-rejects';
|
3 | 4 | import 'mocha'; // tslint:disable-line:no-import-side-effect
|
4 | 5 | import factory from '../factory';
|
| 6 | +import DuplicateKeyError from '../utils/errors/DuplicateKeyError'; |
5 | 7 | import FailingMigrationError from '../utils/errors/FailingMigrationError';
|
6 | 8 | import MissingMigrationError from '../utils/errors/MissingMigrationError';
|
7 | 9 | import assertLocked from '../utils/tests/assertLocked';
|
8 | 10 | import createMigrationProcess from '../utils/tests/createMigrationProcess';
|
9 | 11 | import createTestDownMigration from '../utils/tests/createTestDownMigration';
|
10 | 12 | import TestFactory from '../utils/tests/TestFactory';
|
11 |
| -import MigrationDictionary from '../utils/types/MigrationDictionary'; |
| 13 | +import Migration from '../utils/types/Migration'; |
12 | 14 |
|
13 | 15 | const testRollback: TestFactory = (repoFactory) => {
|
14 |
| - const successfulMigration = createTestDownMigration(); |
15 |
| - const failingMigration = createTestDownMigration(() => { throw new Error(); }); |
| 16 | + const successfulMigration = createTestDownMigration(undefined, 'successfulMigration'); |
| 17 | + const failingMigration = createTestDownMigration(() => { |
| 18 | + throw new Error(); |
| 19 | + }, 'failingMigration'); |
| 20 | + const unskippableKey = 'unskippableMigration'; |
| 21 | + const unskippableMigration = createTestDownMigration(undefined, unskippableKey); |
16 | 22 |
|
17 |
| - const createService = (migrations: MigrationDictionary) => { |
| 23 | + const createService = (migrations: Migration[] = []) => { |
18 | 24 | const log = () => null;
|
19 | 25 | return factory({ log, repo: repoFactory(migrations) });
|
20 | 26 | };
|
21 | 27 |
|
22 | 28 | describe('rollback', () => {
|
23 | 29 | it('should not error when there are no migrations', async () => {
|
24 |
| - const service = createService({}); |
| 30 | + const service = createService(); |
25 | 31 | await service.rollback();
|
26 | 32 | });
|
27 | 33 |
|
| 34 | + it('should error when there are duplicate keys', async () => { |
| 35 | + await createService([createTestDownMigration()]).migrate(); |
| 36 | + const service = createService([createTestDownMigration(), createTestDownMigration()]); |
| 37 | + const promise = service.rollback(); |
| 38 | + await assertRejects(promise, DuplicateKeyError); |
| 39 | + }); |
| 40 | + |
28 | 41 | it('should error when a processed migration is missing', async () => {
|
29 |
| - await createService({ successfulMigration }).migrate(); |
30 |
| - const promise = createService({}).rollback(); |
| 42 | + await createService([successfulMigration]).migrate(); |
| 43 | + const promise = createService().rollback(); |
31 | 44 | await assertRejects(promise, MissingMigrationError);
|
32 | 45 | });
|
33 | 46 |
|
34 | 47 | it('should error when the first migration errors', async () => {
|
35 |
| - const service = createService({ failingMigration, successfulMigration }); |
| 48 | + const service = createService([failingMigration, successfulMigration]); |
36 | 49 | await service.migrate();
|
37 | 50 | const promise = service.rollback();
|
38 | 51 | await assertRejects(promise, FailingMigrationError);
|
39 | 52 | });
|
40 | 53 |
|
41 | 54 | it('should error when the second migration errors', async () => {
|
42 |
| - const service = createService({ successfulMigration, failingMigration }); |
| 55 | + const service = createService([successfulMigration, failingMigration]); |
43 | 56 | await service.migrate();
|
44 | 57 | const promise = service.rollback();
|
45 | 58 | await assertRejects(promise, FailingMigrationError);
|
46 | 59 | });
|
47 | 60 |
|
48 | 61 | it('should process rollback for a processed migration', async () => {
|
49 | 62 | const { process, getProcessed } = createMigrationProcess();
|
50 |
| - const service = createService({ testMigration: createTestDownMigration(process) }); |
| 63 | + const service = createService([createTestDownMigration(process)]); |
51 | 64 | await service.migrate();
|
52 | 65 | await service.rollback();
|
53 | 66 | assert.equal(getProcessed(), true);
|
54 | 67 | });
|
55 | 68 |
|
56 | 69 | it('should not process rollback for a unprocessed migration', async () => {
|
57 | 70 | const { process, getProcessed } = createMigrationProcess();
|
58 |
| - const service = createService({ testMigration: createTestDownMigration(process) }); |
| 71 | + const service = createService([createTestDownMigration(process)]); |
59 | 72 | await service.rollback();
|
60 | 73 | assert.equal(getProcessed(), false);
|
61 | 74 | });
|
62 | 75 |
|
63 |
| - it('should skip processed migrations after unprocessed migrations', async () => { |
64 |
| - const skippedMigration = createMigrationProcess(); |
65 |
| - const unskippedMigration = createMigrationProcess(); |
66 |
| - await createService({ migrationToProcess: createTestDownMigration() }).migrate(); |
67 |
| - await createService({ |
68 |
| - migrationToProcess: createTestDownMigration(unskippedMigration.process), |
69 |
| - migrationToSkip: createTestDownMigration(skippedMigration.process), |
70 |
| - }).rollback(); |
71 |
| - assert.equal(skippedMigration.getProcessed(), false); |
72 |
| - assert.equal(unskippedMigration.getProcessed(), true); |
| 76 | + it('should skip unprocessed migrations after processed migrations', async () => { |
| 77 | + const skippedProcess = createMigrationProcess(); |
| 78 | + const unskippedProcess = createMigrationProcess(); |
| 79 | + await createService([unskippableMigration]).migrate(); |
| 80 | + await createService([ |
| 81 | + createTestDownMigration(unskippedProcess.process, unskippableKey), |
| 82 | + createTestDownMigration(skippedProcess.process), |
| 83 | + ]).rollback(); |
| 84 | + assert.equal(skippedProcess.getProcessed(), false); |
| 85 | + assert.equal(unskippedProcess.getProcessed(), true); |
73 | 86 | });
|
74 | 87 |
|
75 |
| - it('should skip processed migrations before unprocessed migrations', async () => { |
76 |
| - const skippedMigration = createMigrationProcess(); |
77 |
| - const unskippedMigration = createMigrationProcess(); |
78 |
| - await createService({ migrationToProcess: createTestDownMigration() }).migrate(); |
79 |
| - await createService({ |
80 |
| - migrationToSkip: createTestDownMigration(skippedMigration.process), |
81 |
| - // tslint:disable-next-line:object-literal-sort-keys |
82 |
| - migrationToProcess: createTestDownMigration(unskippedMigration.process), |
83 |
| - }).rollback(); |
84 |
| - assert.equal(skippedMigration.getProcessed(), false); |
85 |
| - assert.equal(unskippedMigration.getProcessed(), true); |
| 88 | + it('should skip unprocessed migrations before processed migrations', async () => { |
| 89 | + const skippedProcess = createMigrationProcess(); |
| 90 | + const unskippedProcess = createMigrationProcess(); |
| 91 | + await createService([unskippableMigration]).migrate(); |
| 92 | + await createService([ |
| 93 | + createTestDownMigration(skippedProcess.process), |
| 94 | + createTestDownMigration(unskippedProcess.process, unskippableKey), |
| 95 | + ]).rollback(); |
| 96 | + assert.equal(skippedProcess.getProcessed(), false); |
| 97 | + assert.equal(unskippedProcess.getProcessed(), true); |
86 | 98 | });
|
87 | 99 |
|
88 | 100 | it('should error when migrations are locked', async () => {
|
89 |
| - const service = createService({}); |
| 101 | + const service = createService(); |
90 | 102 | await assertLocked([service.rollback(), service.rollback()]);
|
91 | 103 | });
|
92 | 104 | });
|
|
0 commit comments