Example #1
0
        public async Task <SchemaMigrationResult> ApplyAll()
        {
            var result = new SchemaMigrationResult();

            Stopwatch sw = new Stopwatch();

            logger.LogInformation($"Beginning [ApplyAll] action for {Migrations.Count} migrations...");
            sw.Start();

            var latestVersion = new Version(0, 0, 0);

            var latestSchemaAppliedDoc = (await GetAppliedVersionsAsync()).LastOrDefault();

            if (!latestSchemaAppliedDoc.Equals(default(KeyValuePair <Version, SchemaDocument>)))
            {
                latestVersion = latestSchemaAppliedDoc.Key;
            }

            result.StartingVersion = latestVersion;
            result.MigrationsFound = Migrations.Count;

            foreach (var curVersion in Migrations)
            {
                if (curVersion.Key <= latestVersion)
                {
                    logger.LogInformation($"Migrations for version {curVersion.Key} skipped as newer version ({latestVersion}) is already applied.");
                    result.MigrationsSkipped += 1;
                    continue;
                }

                logger.LogInformation($"Applying migrations for {curVersion.Key.ToString()}");

                await curVersion.Value.Apply(db);

                var newVersion = new SchemaDocument()
                {
                    DateApplied = DateTime.UtcNow,
                    Version     = curVersion.Key.ToString()
                };
                await UpdateAppliedVersions(db, newVersion);

                result.MigrationsApplied += 1;
                result.EndingVersion      = curVersion.Key;

                logger.LogInformation($"Done applying migrations for {curVersion.Key.ToString()}");
            }

            sw.Stop();
            result.ElapsedMiliseconds = sw.ElapsedMilliseconds;
            logger.LogInformation($"Completed [ApplyAll] action in {sw.ElapsedMilliseconds}ms. Applied: {result.MigrationsApplied}, Skipped: {result.MigrationsSkipped}");

            return(result);
        }
Example #2
0
 private async Task UpdateAppliedVersions(IMongoDatabase db, SchemaDocument ver)
 {
     await db.GetCollection <SchemaDocument>(options.CollectionName).InsertOneAsync(ver, null, CancellationToken.None);
 }