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

View File

@ -12,7 +12,7 @@ namespace NzbDrone.Core.Datastore
{ {
public interface IDbFactory public interface IDbFactory
{ {
IDatabase Create(MigrationType migrationType = MigrationType.Main); IDatabase Create(MigrationType migrationType = MigrationType.Main, Action<NzbDroneMigrationBase> beforeMigration = null);
} }
public class DbFactory : IDbFactory public class DbFactory : IDbFactory
@ -43,7 +43,7 @@ namespace NzbDrone.Core.Datastore
_connectionStringFactory = connectionStringFactory; _connectionStringFactory = connectionStringFactory;
} }
public IDatabase Create(MigrationType migrationType = MigrationType.Main) public IDatabase Create(MigrationType migrationType = MigrationType.Main, Action<NzbDroneMigrationBase> beforeMigration = null)
{ {
string connectionString; string connectionString;
@ -66,7 +66,7 @@ namespace NzbDrone.Core.Datastore
} }
} }
_migrationController.MigrateToLatest(connectionString, migrationType); _migrationController.MigrateToLatest(connectionString, migrationType, beforeMigration);
var db = new Database(() => 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 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 System.Reflection;
using FluentMigrator.Runner; using FluentMigrator.Runner;
using FluentMigrator.Runner.Initialization; using FluentMigrator.Runner.Initialization;
using FluentMigrator.Runner.Processors.SQLite;
namespace NzbDrone.Core.Datastore.Migration.Framework namespace NzbDrone.Core.Datastore.Migration.Framework
{ {
public interface IMigrationController public interface IMigrationController
{ {
void MigrateToLatest(string connectionString, MigrationType migrationType); void MigrateToLatest(string connectionString, MigrationType migrationType, Action<NzbDroneMigrationBase> beforeMigration);
} }
public class MigrationController : IMigrationController public class MigrationController : IMigrationController
@ -20,7 +20,7 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
_announcer = announcer; _announcer = announcer;
} }
public void MigrateToLatest(string connectionString, MigrationType migrationType) public void MigrateToLatest(string connectionString, MigrationType migrationType, Action<NzbDroneMigrationBase> beforeMigration)
{ {
var sw = Stopwatch.StartNew(); var sw = Stopwatch.StartNew();
@ -31,10 +31,7 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
var migrationContext = new RunnerContext(_announcer) var migrationContext = new RunnerContext(_announcer)
{ {
Namespace = "NzbDrone.Core.Datastore.Migration", Namespace = "NzbDrone.Core.Datastore.Migration",
ApplicationContext = new MigrationContext ApplicationContext = new MigrationContext(migrationType, beforeMigration)
{
MigrationType = migrationType
}
}; };
var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 }; var options = new MigrationOptions { PreviewOnly = false, Timeout = 60 };
@ -45,7 +42,7 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
sw.Stop(); sw.Stop();
_announcer.ElapsedTime(sw.Elapsed); _announcer.ElapsedTime(sw.Elapsed);
} }
} }
} }

View File

@ -1,4 +1,5 @@
using System; using System;
using FluentMigrator;
using NLog; using NLog;
using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Instrumentation;
@ -7,10 +8,12 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
public abstract class NzbDroneMigrationBase : FluentMigrator.Migration public abstract class NzbDroneMigrationBase : FluentMigrator.Migration
{ {
protected readonly Logger _logger; protected readonly Logger _logger;
private readonly MigrationContext _migrationContext;
protected NzbDroneMigrationBase() protected NzbDroneMigrationBase()
{ {
_logger = NzbDroneLogger.GetLogger(this); _logger = NzbDroneLogger.GetLogger(this);
_migrationContext = (MigrationContext)ApplicationContext;
} }
protected virtual void MainDbUpgrade() 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() public override void Up()
{ {
var context = (MigrationContext)ApplicationContext;
switch (context.MigrationType) if (Context.BeforeMigration != null)
{
Context.BeforeMigration(this);
}
switch (Context.MigrationType)
{ {
case MigrationType.Main: case MigrationType.Main:
MainDbUpgrade(); MainDbUpgrade();