Skip to content

Commit 8e70e59

Browse files
committed
feat: add afterUpSuccess method to migrations
Allows to run something after the MongoDB transaction, only if it was successful.
1 parent d353aad commit 8e70e59

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

adonis-typings/migration.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ declare module '@ioc:Zakodium/Mongodb/Migration' {
2424
callback: (db: Db, client: ClientSession) => Promise<void>,
2525
): void;
2626
public abstract up(): void;
27+
public afterUpSuccess?(): unknown;
2728
public execUp(session: ClientSession): Promise<void>;
2829
}
2930
}

commands/MongodbMigrate.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { inject } from '@adonisjs/core/build/standalone';
22
import { ObjectId } from 'mongodb';
33

44
import { DatabaseContract } from '@ioc:Zakodium/Mongodb/Database';
5+
import Migration from '@ioc:Zakodium/Mongodb/Migration';
56

67
import MigrationCommand, {
78
migrationCollectionName,
89
migrationLockCollectionName,
910
} from './util/MigrationCommand';
10-
import Migration from '@ioc:Zakodium/Mongodb/Migration';
1111

1212
interface IMigration {
1313
_id: ObjectId | undefined;
@@ -123,6 +123,17 @@ export default class MongodbMigrate extends MigrationCommand {
123123
break;
124124
}
125125

126+
if (migration.afterUpSuccess) {
127+
try {
128+
await migration.afterUpSuccess();
129+
} catch (error) {
130+
this.logger.warning(`Migration's afterUpSuccess call failed`);
131+
// TODO: See if there can be a way in Ace commands to print error stack traces
132+
// eslint-disable-next-line no-console
133+
console.warn(error);
134+
}
135+
}
136+
126137
successfullyExecuted++;
127138
}
128139

src/Migration.ts

+1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export default function createMigration(Database: DatabaseContract): any {
173173
}
174174

175175
public abstract up(): void;
176+
public afterUpSuccess?(): void;
176177
}
177178

178179
return Migration;

0 commit comments

Comments
 (0)