Skip to content

Commit f83840e

Browse files
authored
feat: Improves recording of processed migrations. (#11)
BREAKING CHANGE: Renames fields in `ProcessedMigration`. Renames `updateProcessedMigration` to `recordProcessedMigration` in the `RepoFacade`.
1 parent 7a36a9c commit f83840e

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

src/RepoFacade.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ProcessedMigration from './utils/types/ProcessedMigration';
44
export default interface RepoFacade {
55
readonly getMigrations: () => Promise<Migration[]>;
66
readonly getProcessedMigrations: () => Promise<ProcessedMigration[]>;
7-
readonly updateProcessedMigration: (migration: ProcessedMigration) => Promise<void>;
7+
readonly recordProcessedMigration: (migration: ProcessedMigration) => Promise<void>;
88
readonly removeProcessedMigration: (key: string) => Promise<void>;
99
readonly clearMigrations: () => Promise<void>;
1010
readonly lockMigrations: () => Promise<void>;

src/utils/getLastBatchKeys.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import FacadeConfig from '../FacadeConfig';
33

44
export default async (config: FacadeConfig) => {
55
const processedMigrations = await config.repo.getProcessedMigrations();
6-
const batchStarts = processedMigrations.map((migration) => migration.lastBatch);
6+
const batchStarts = processedMigrations.map((migration) => migration.batchStart);
77
const sortedBatchStarts = batchStarts.sort();
88
const lastBatchStart = last(sortedBatchStarts);
99

@@ -13,7 +13,7 @@ export default async (config: FacadeConfig) => {
1313

1414
const lastBatchIsoStart = lastBatchStart.toISOString();
1515
const lastBatchMigrations = processedMigrations.filter((migration) => {
16-
return migration.lastBatch.toISOString() === lastBatchIsoStart;
16+
return migration.batchStart.toISOString() === lastBatchIsoStart;
1717
});
1818
return lastBatchMigrations.map((migration) => migration.key);
1919
};

src/utils/migrateKey.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,18 @@ export default async ({ config, key, batchStart, dryRun }: Opts) => {
1515
const selectedMigration = getMigrationByKey(migrations, key);
1616
config.log(`Starting to migrate with ${key}`);
1717
if (!dryRun) {
18-
const migrationStart = new Date();
18+
const processStart = new Date();
1919
try {
2020
await selectedMigration.up();
2121
} catch (err) {
2222
throw new FailingMigrationError(key, err);
2323
}
24-
await config.repo.updateProcessedMigration({
24+
const processEnd = new Date();
25+
await config.repo.recordProcessedMigration({
26+
batchStart: defaultTo(batchStart, processStart),
2527
key,
26-
lastBatch: defaultTo(batchStart, migrationStart),
27-
lastStart: migrationStart,
28+
processEnd,
29+
processStart,
2830
});
2931
}
3032
config.log(`Completed migration with ${key}`);

src/utils/tests/testRepoFactory.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const testRepoFactory = (migrations: Migration[]): RepoFacade => {
2323
}
2424
hasLockedMigrations = true;
2525
},
26+
recordProcessedMigration: async (migration) => {
27+
processedMigrations = [...processedMigrations, migration];
28+
},
2629
removeProcessedMigration: async (key) => {
2730
processedMigrations = processedMigrations.filter((processedMigration) => {
2831
return processedMigration.key !== key;
@@ -31,12 +34,6 @@ const testRepoFactory = (migrations: Migration[]): RepoFacade => {
3134
unlockMigrations: async () => {
3235
hasLockedMigrations = false;
3336
},
34-
updateProcessedMigration: async (migration) => {
35-
const unmatchedMigrations = processedMigrations.filter((processedMigration) => {
36-
return processedMigration.key !== migration.key;
37-
});
38-
processedMigrations = [...unmatchedMigrations, migration];
39-
},
4037
};
4138
};
4239

src/utils/types/ProcessedMigration.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export default interface ProcessedMigration {
22
readonly key: string;
3-
readonly lastStart: Date;
4-
readonly lastBatch: Date;
3+
readonly processStart: Date;
4+
readonly processEnd: Date;
5+
readonly batchStart: Date;
56
}

0 commit comments

Comments
 (0)