EnVisageOnline/Main/Source/EnVisage.MongoDbMigration/Logic/MigrationsManager.cs

50 lines
1.6 KiB
C#

using EnVisage.MongoDbMigration.Data;
using EnVisage.MongoDbMigration.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
namespace EnVisage.MongoDbMigration.Logic
{
public class MigrationsManager : ManagerBase, IMigrationsManager
{
private bool _isTestMode = false;
public MigrationsManager(MongoAccessorsContainer accessorsContainer, bool isTestMode)
: base(accessorsContainer)
{
_isTestMode = isTestMode;
}
public void RunExecuters(List<IMigrationExecuterBase> executers)
{
if (executers == null)
throw new ArgumentNullException("executers");
if (executers.Count != executers.Select(x => x.Version).Distinct().Count())
throw new ArgumentException("There are few migration executers wich have the same version");
var completedMigrations = Accessors.MigrationAccessor.GetAll();
var currentMigrations = new List<Migration>();
foreach (var executer in executers.OrderBy(x => x.Version))
{
if (completedMigrations.Any(x => x.Version == executer.Version))
continue;
executer.Execute();
if (_isTestMode)
continue;
currentMigrations.Add(new Migration()
{
Version = executer.Version
});
}
if (!_isTestMode && currentMigrations.Count > 0)
Accessors.MigrationAccessor.Insert(currentMigrations);
}
}
}