Added before migration hook, this can be used to insert "old" data into test Db

so data migration could be tested.
This commit is contained in:
Keivan Beigi 2014-12-02 11:21:43 -08:00 committed by Mark McDowall
parent 37a1398338
commit c7ed76f6d3
5 changed files with 51 additions and 20 deletions

View File

@ -89,7 +89,6 @@ namespace NzbDrone.Core.Test.Framework
{
WithTempAsAppPath();
Mocker.SetConstant<IAnnouncer>(Mocker.Resolve<MigrationLogger>());
Mocker.SetConstant<IConnectionStringFactory>(Mocker.Resolve<ConnectionStringFactory>());
Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>());
@ -97,9 +96,9 @@ namespace NzbDrone.Core.Test.Framework
MapRepository.Instance.EnableTraceLogging = true;
var factory = Mocker.Resolve<DbFactory>();
var _database = factory.Create(MigrationType);
_db = new TestDatabase(_database);
Mocker.SetConstant(_database);
var database = factory.Create(MigrationType);
_db = new TestDatabase(database);
Mocker.SetConstant(database);
}
[SetUp]

View File

@ -12,7 +12,7 @@ namespace NzbDrone.Core.Datastore
{
public interface IDbFactory
{
IDatabase Create(MigrationType migrationType = MigrationType.Main);
IDatabase Create(MigrationType migrationType = MigrationType.Main, Action<NzbDroneMigrationBase> beforeMigration = null);
}
public class DbFactory : IDbFactory
@ -43,7 +43,7 @@ namespace NzbDrone.Core.Datastore
_connectionStringFactory = connectionStringFactory;
}
public IDatabase Create(MigrationType migrationType = MigrationType.Main)
public IDatabase Create(MigrationType migrationType = MigrationType.Main, Action<NzbDroneMigrationBase> beforeMigration = null)
{
string connectionString;
@ -66,7 +66,7 @@ namespace NzbDrone.Core.Datastore
}
}
_migrationController.MigrateToLatest(connectionString, migrationType);
_migrationController.MigrateToLatest(connectionString, migrationType, beforeMigration);
var db = new Database(() =>
{

View File

@ -1,7 +1,18 @@
namespace NzbDrone.Core.Datastore.Migration.Framework
using System;
namespace NzbDrone.Core.Datastore.Migration.Framework
{
public class MigrationContext
{
public MigrationType MigrationType { get; set; }
public MigrationType MigrationType { get; private set; }
public Action<NzbDroneMigrationBase> BeforeMigration { get; private set; }
public MigrationContext(MigrationType migrationType, Action<NzbDroneMigrationBase> beforeAction)
{
MigrationType = migrationType;
BeforeMigration = beforeAction;
}
}
}

View File

@ -1,14 +1,14 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Reflection;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Initialization;
using FluentMigrator.Runner.Processors.SQLite;
namespace NzbDrone.Core.Datastore.Migration.Framework
{
public interface IMigrationController
{
void MigrateToLatest(string connectionString, MigrationType migrationType);
void MigrateToLatest(string connectionString, MigrationType migrationType, Action<NzbDroneMigrationBase> beforeMigration);
}
public class MigrationController : IMigrationController
@ -20,7 +20,7 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
_announcer = announcer;
}
public void MigrateToLatest(string connectionString, MigrationType migrationType)
public void MigrateToLatest(string connectionString, MigrationType migrationType, Action<NzbDroneMigrationBase> beforeMigration)
{
var sw = Stopwatch.StartNew();
@ -31,10 +31,7 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
var migrationContext = new RunnerContext(_announcer)
{
Namespace = "NzbDrone.Core.Datastore.Migration",
ApplicationContext = new MigrationContext
{
MigrationType = migrationType
}
ApplicationContext = new MigrationContext(migrationType, beforeMigration)
};
var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 };
@ -45,7 +42,7 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
sw.Stop();
_announcer.ElapsedTime(sw.Elapsed);
_announcer.ElapsedTime(sw.Elapsed);
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using FluentMigrator;
using NLog;
using NzbDrone.Common.Instrumentation;
@ -7,10 +8,12 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
public abstract class NzbDroneMigrationBase : FluentMigrator.Migration
{
protected readonly Logger _logger;
private readonly MigrationContext _migrationContext;
protected NzbDroneMigrationBase()
{
_logger = NzbDroneLogger.GetLogger(this);
_migrationContext = (MigrationContext)ApplicationContext;
}
protected virtual void MainDbUpgrade()
@ -21,11 +24,32 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
{
}
public int Version
{
get
{
var migrationAttribute = (MigrationAttribute)Attribute.GetCustomAttribute(GetType(), typeof(MigrationAttribute));
return (int)migrationAttribute.Version;
}
}
public MigrationContext Context
{
get
{
return _migrationContext;
}
}
public override void Up()
{
var context = (MigrationContext)ApplicationContext;
switch (context.MigrationType)
if (Context.BeforeMigration != null)
{
Context.BeforeMigration(this);
}
switch (Context.MigrationType)
{
case MigrationType.Main:
MainDbUpgrade();