@@ -10,7 +10,7 @@ import 'package:mongo_dart/mongo_dart.dart';
1010/// `migrations_history` collection to ensure idempotency and prevent
1111/// redundant execution.
1212///
13- /// Migrations are identified by a unique version string (YYYYMMDDHHMMSS)
13+ /// Migrations are identified by their PR merge date (YYYYMMDDHHMMSS)
1414/// and are always applied in chronological order.
1515/// {@endtemplate}
1616class DatabaseMigrationService {
@@ -28,45 +28,47 @@ class DatabaseMigrationService {
2828 final List <Migration > _migrations;
2929
3030 /// The name of the MongoDB collection used to track applied migrations.
31- static const String _migrationsCollectionName = 'migrations_history' ;
31+ /// This collection stores metadata about applied Pull Request migrations.
32+ static const String _migrationsCollectionName = 'pr_migrations_history' ;
3233
3334 /// Initializes the migration service and applies any pending migrations.
3435 ///
3536 /// This method performs the following steps:
36- /// 1. Ensures the `migrations_history ` collection exists and has a unique
37- /// index on the `version ` field.
38- /// 2. Fetches all previously applied migration versions from the database.
39- /// 3. Sorts the registered migrations by their version string.
37+ /// 1. Ensures the `pr_migrations_history ` collection exists and has a unique
38+ /// index on the `prDate ` field.
39+ /// 2. Fetches all previously applied migration PR dates from the database.
40+ /// 3. Sorts the registered migrations by their `prDate` string.
4041 /// 4. Iterates through the sorted migrations, applying only those that
4142 /// have not yet been applied.
42- /// 5. Records each successfully applied migration in the `migrations_history `
43- /// collection.
43+ /// 5. Records each successfully applied migration's `prDate` and `prId `
44+ /// in the `pr_migrations_history` collection.
4445 Future <void > init () async {
4546 _log.info ('Starting database migration process...' );
4647
4748 await _ensureMigrationsCollectionAndIndex ();
4849
49- final appliedVersions = await _getAppliedMigrationVersions ();
50- _log.fine ('Applied migration versions : $appliedVersions ' );
50+ final appliedPrDates = await _getAppliedMigrationPrDates ();
51+ _log.fine ('Applied migration PR dates : $appliedPrDates ' );
5152
52- // Sort migrations by version to ensure chronological application.
53- _migrations.sort ((a, b) => a.version .compareTo (b.version ));
53+ // Sort migrations by prDate to ensure chronological application.
54+ _migrations.sort ((a, b) => a.prDate .compareTo (b.prDate ));
5455
5556 for (final migration in _migrations) {
56- if (! appliedVersions .contains (migration.version )) {
57+ if (! appliedPrDates .contains (migration.prDate )) {
5758 _log.info (
58- 'Applying migration V${migration .version }: ${migration .description }' ,
59+ 'Applying migration PR#${migration .prId } (Date: ${migration .prDate }): '
60+ '${migration .prSummary }' ,
5961 );
6062 try {
6163 await migration.up (_db, _log);
62- await _recordMigration (migration.version );
64+ await _recordMigration (migration.prDate, migration.prId );
6365 _log.info (
64- 'Successfully applied migration V ${migration .version } .' ,
66+ 'Successfully applied migration PR# ${migration .prId } (Date: ${ migration . prDate }) .' ,
6567 );
6668 } catch (e, s) {
6769 _log.severe (
68- 'Failed to apply migration V ${migration .version } : '
69- '${migration .description }' ,
70+ 'Failed to apply migration PR# ${migration .prId } (Date: ${ migration . prDate }) : '
71+ '${migration .prSummary }' ,
7072 e,
7173 s,
7274 );
@@ -75,43 +77,44 @@ class DatabaseMigrationService {
7577 }
7678 } else {
7779 _log.fine (
78- 'Migration V ${migration .version } already applied. Skipping.' ,
80+ 'Migration PR# ${migration .prId } (Date: ${ migration . prDate }) already applied. Skipping.' ,
7981 );
8082 }
8183 }
8284
8385 _log.info ('Database migration process completed.' );
8486 }
8587
86- /// Ensures the `migrations_history ` collection exists and has a unique index
87- /// on the `version ` field.
88+ /// Ensures the `pr_migrations_history ` collection exists and has a unique index
89+ /// on the `prDate ` field.
8890 Future <void > _ensureMigrationsCollectionAndIndex () async {
89- _log.fine ('Ensuring migrations_history collection and index...' );
91+ _log.fine ('Ensuring pr_migrations_history collection and index...' );
9092 final collection = _db.collection (_migrationsCollectionName);
9193 await collection.createIndex (
92- key: 'version ' ,
94+ key: 'prDate ' ,
9395 unique: true ,
94- name: 'version_unique_index ' ,
96+ name: 'prDate_unique_index ' ,
9597 );
96- _log.fine ('Migrations_history collection and index ensured.' );
98+ _log.fine ('Pr_migrations_history collection and index ensured.' );
9799 }
98100
99- /// Retrieves a set of versions of all migrations that have already been
101+ /// Retrieves a set of PR dates of all migrations that have already been
100102 /// applied to the database.
101- Future <Set <String >> _getAppliedMigrationVersions () async {
103+ Future <Set <String >> _getAppliedMigrationPrDates () async {
102104 final collection = _db.collection (_migrationsCollectionName);
103105 final documents = await collection.find ().toList ();
104106 return documents
105- .map ((doc) => doc['version ' ] as String )
107+ .map ((doc) => doc['prDate ' ] as String )
106108 .toSet ();
107109 }
108110
109- /// Records a successfully applied migration in the `migrations_history `
111+ /// Records a successfully applied migration in the `pr_migrations_history `
110112 /// collection.
111- Future <void > _recordMigration (String version ) async {
113+ Future <void > _recordMigration (String prDate, String prId ) async {
112114 final collection = _db.collection (_migrationsCollectionName);
113115 await collection.insertOne ({
114- 'version' : version,
116+ 'prDate' : prDate,
117+ 'prId' : prId,
115118 'appliedAt' : DateTime .now ().toUtc (),
116119 });
117120 }
0 commit comments