Example #1
0
        public void MigrateDown(
            MongoDbContext context,
            string downVersion)
        {
            try
            {
                if (context.MigrationsAssembly == null)
                {
                    throw new NullReferenceException("Не указана сборка с миграциями");
                }

                if (downVersion.Length != 14 ||
                    !long.TryParse(downVersion, out _))
                {
                    throw new InvalidOperationException("Указана невалидная версия");
                }

                context.ExecuteTransaction(database =>
                {
                    var migrationVersions = GetMigrationsInfo(database, context.MigrationsAssembly);
                    var tempVersion       = GetTempVersion(database, out var collection);

                    var sortedMigrationVersions = migrationVersions
                                                  .OrderBy(x => long.Parse(x.Version))
                                                  .Where(x => long.Parse(x.Version) > long.Parse(downVersion))
                                                  .Where(x => long.Parse(x.Version) <= long.Parse(tempVersion));

                    var collectionExists = GetVersionCollectionExists(database);

                    if (!collectionExists)
                    {
                        database.CreateCollection(VERSION_INFO_COLLECTION);
                    }

                    foreach (var migrationInfo in sortedMigrationVersions)
                    {
                        var migration = migrationInfo.Migration;
                        migration.Down();

                        var versionInfo = new MongoVersionInfo
                        {
                            Date        = DateTime.UtcNow,
                            Description = migrationInfo.Description,
                            ToVersion   = migrationInfo.Version,
                            FromVersion = tempVersion
                        };

                        collection.InsertOne(versionInfo);
                    }
                });
            }
            catch (Exception e)
            {
                var message = $"Ошибка во время откатывания " +
                              $"миграций из сборки {context.MigrationsAssembly} " +
                              $"до версии {downVersion}";
                throw new MongoException(message, e);
            }
        }
Example #2
0
        public void MigrateUp(MongoDbContext context)
        {
            try
            {
                if (context.MigrationsAssembly == null)
                {
                    throw new NullReferenceException("Не указана сборка с миграциями");
                }

                context.ExecuteTransaction(database =>
                {
                    var migrationData = GetMigrationsInfo(database, context.MigrationsAssembly);
                    var tempVersion   = GetTempVersion(database, out var collection);

                    var sortedMigrationData = migrationData
                                              .OrderBy(x => long.Parse(x.Version))
                                              .Where(x => long.Parse(x.Version) > long.Parse(tempVersion))
                                              .ToArray();

                    var collectionExists = GetVersionCollectionExists(database);

                    if (!collectionExists)
                    {
                        database.CreateCollection(VERSION_INFO_COLLECTION);
                    }

                    foreach (var migrationInfo in sortedMigrationData)
                    {
                        var migration = migrationInfo.Migration;
                        migration.Up();
                        var versionInfo = new MongoVersionInfo
                        {
                            Date        = DateTime.UtcNow,
                            Description = migrationInfo.Description,
                            ToVersion   = migrationInfo.Version,
                            FromVersion = tempVersion
                        };

                        collection.InsertOne(versionInfo);
                    }
                });
            }
            catch (Exception e)
            {
                var message = $"Ошибка во время накатывания " +
                              $"миграций из сборки {context.MigrationsAssembly}";
                throw new MongoException(message, e);
            }
        }