From c7ed76f6d3f926c009022f27aa2dcbfc6c6c2e23 Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Tue, 2 Dec 2014 11:21:43 -0800 Subject: [PATCH] Added before migration hook, this can be used to insert "old" data into test Db so data migration could be tested. --- src/NzbDrone.Core.Test/Framework/DbTest.cs | 7 ++--- src/NzbDrone.Core/Datastore/DbFactory.cs | 6 ++-- .../Migration/Framework/MigrationContext.cs | 15 ++++++++-- .../Framework/MigrationController.cs | 15 ++++------ .../Framework/NzbDroneMigrationBase.cs | 28 +++++++++++++++++-- 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/src/NzbDrone.Core.Test/Framework/DbTest.cs b/src/NzbDrone.Core.Test/Framework/DbTest.cs index be9ffed86..436368db3 100644 --- a/src/NzbDrone.Core.Test/Framework/DbTest.cs +++ b/src/NzbDrone.Core.Test/Framework/DbTest.cs @@ -89,7 +89,6 @@ namespace NzbDrone.Core.Test.Framework { WithTempAsAppPath(); - Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); Mocker.SetConstant(Mocker.Resolve()); @@ -97,9 +96,9 @@ namespace NzbDrone.Core.Test.Framework MapRepository.Instance.EnableTraceLogging = true; var factory = Mocker.Resolve(); - 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] diff --git a/src/NzbDrone.Core/Datastore/DbFactory.cs b/src/NzbDrone.Core/Datastore/DbFactory.cs index df7889d83..e4fffed2f 100644 --- a/src/NzbDrone.Core/Datastore/DbFactory.cs +++ b/src/NzbDrone.Core/Datastore/DbFactory.cs @@ -12,7 +12,7 @@ namespace NzbDrone.Core.Datastore { public interface IDbFactory { - IDatabase Create(MigrationType migrationType = MigrationType.Main); + IDatabase Create(MigrationType migrationType = MigrationType.Main, Action 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 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(() => { diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationContext.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationContext.cs index e1bb47bcf..0d49450af 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationContext.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationContext.cs @@ -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 BeforeMigration { get; private set; } + + public MigrationContext(MigrationType migrationType, Action beforeAction) + { + MigrationType = migrationType; + + BeforeMigration = beforeAction; + } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs index 4c55762b8..a505078fe 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs @@ -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 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 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); } } } diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs index c2fca697a..2838adac4 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneMigrationBase.cs @@ -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();