|
| 1 | +import { Migrator } from '../../..' |
| 2 | + |
| 3 | +// eslint-disable-next-line @typescript-eslint/consistent-type-definitions |
| 4 | +type State = { readonly items: readonly number[] } |
| 5 | + |
| 6 | +const migrator = new Migrator<State>([ |
| 7 | + ['b4dac8cd-af18-4e7d-a723-27f61d368228', (previous) => ({ |
| 8 | + ...previous, |
| 9 | + items: [...(previous['items'] as readonly number[]), 1] |
| 10 | + })], |
| 11 | + ['1b69c28f-454e-4511-aa05-596fe5ae23a8', (previous) => ({ |
| 12 | + ...previous, |
| 13 | + items: [...(previous['items'] as readonly number[]), 2] |
| 14 | + })], |
| 15 | + ['b07bc75d-1ba2-4bf7-b510-51a93d554a56', (previous) => ({ |
| 16 | + ...previous, |
| 17 | + items: [...(previous['items'] as readonly number[]), 3], |
| 18 | + executedMigrationUuids: 'overwritten' |
| 19 | + })] |
| 20 | +]) |
| 21 | + |
| 22 | +test('execution is required when migrations do not exist', () => { |
| 23 | + expect(migrator.executionRequired({ |
| 24 | + items: [] |
| 25 | + })).toBeTruthy() |
| 26 | +}) |
| 27 | + |
| 28 | +test('execution is not required when all migrations have executed', () => { |
| 29 | + expect(migrator.executionRequired({ |
| 30 | + executedMigrationUuids: ['1b69c28f-454e-4511-aa05-596fe5ae23a8', 'b4dac8cd-af18-4e7d-a723-27f61d368228', 'b07bc75d-1ba2-4bf7-b510-51a93d554a56'], |
| 31 | + items: [] |
| 32 | + })).toBeFalsy() |
| 33 | +}) |
| 34 | + |
| 35 | +test('execution is required when at least one migration has not been executed', () => { |
| 36 | + expect(migrator.executionRequired({ |
| 37 | + executedMigrationUuids: ['1b69c28f-454e-4511-aa05-596fe5ae23a8'], |
| 38 | + items: [] |
| 39 | + })).toBeTruthy() |
| 40 | +}) |
| 41 | + |
| 42 | +test('all unexecuted migrations are ran in order', () => { |
| 43 | + expect(migrator.execute({ |
| 44 | + executedMigrationUuids: ['1b69c28f-454e-4511-aa05-596fe5ae23a8'], |
| 45 | + items: [] |
| 46 | + })).toEqual({ |
| 47 | + executedMigrationUuids: ['1b69c28f-454e-4511-aa05-596fe5ae23a8', 'b4dac8cd-af18-4e7d-a723-27f61d368228', 'b07bc75d-1ba2-4bf7-b510-51a93d554a56'], |
| 48 | + items: [1, 3] |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +test('all migrations are ran in order', () => { |
| 53 | + expect(migrator.execute({ |
| 54 | + items: [] |
| 55 | + })).toEqual({ |
| 56 | + executedMigrationUuids: ['b4dac8cd-af18-4e7d-a723-27f61d368228', '1b69c28f-454e-4511-aa05-596fe5ae23a8', 'b07bc75d-1ba2-4bf7-b510-51a93d554a56'], |
| 57 | + items: [1, 2, 3] |
| 58 | + }) |
| 59 | +}) |
0 commit comments