From a707d23692ff0bb34d963025bc0d04f328cf4d0f Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 7 Mar 2016 15:13:31 +0100 Subject: [PATCH] Allows for specifying explicit product names by overriding the property in MigrationStartupHander --- .../Persistence/Migrations/MigrationRunner.cs | 4 + .../MigrationStartupHandlerTests.cs | 100 ++++++++++++++++++ src/Umbraco.Tests/Umbraco.Tests.csproj | 1 + .../Migrations/MigrationStartupHander.cs | 16 ++- 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 src/Umbraco.Tests/Persistence/Migrations/MigrationStartupHandlerTests.cs diff --git a/src/Umbraco.Core/Persistence/Migrations/MigrationRunner.cs b/src/Umbraco.Core/Persistence/Migrations/MigrationRunner.cs index 36ddb2d4ba..081865b9a1 100644 --- a/src/Umbraco.Core/Persistence/Migrations/MigrationRunner.cs +++ b/src/Umbraco.Core/Persistence/Migrations/MigrationRunner.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Linq; using System.Text; @@ -27,18 +28,21 @@ namespace Umbraco.Core.Persistence.Migrations private readonly IMigration[] _migrations; [Obsolete("Use the ctor that specifies all dependencies instead")] + [EditorBrowsable(EditorBrowsableState.Never)] public MigrationRunner(Version currentVersion, Version targetVersion, string productName) : this(LoggerResolver.Current.Logger, currentVersion, targetVersion, productName) { } [Obsolete("Use the ctor that specifies all dependencies instead")] + [EditorBrowsable(EditorBrowsableState.Never)] public MigrationRunner(ILogger logger, Version currentVersion, Version targetVersion, string productName) : this(logger, currentVersion, targetVersion, productName, null) { } [Obsolete("Use the ctor that specifies all dependencies instead")] + [EditorBrowsable(EditorBrowsableState.Never)] public MigrationRunner(ILogger logger, Version currentVersion, Version targetVersion, string productName, params IMigration[] migrations) : this(ApplicationContext.Current.Services.MigrationEntryService, logger, new SemVersion(currentVersion), new SemVersion(targetVersion), productName, migrations) { diff --git a/src/Umbraco.Tests/Persistence/Migrations/MigrationStartupHandlerTests.cs b/src/Umbraco.Tests/Persistence/Migrations/MigrationStartupHandlerTests.cs new file mode 100644 index 0000000000..dd35f9ad3c --- /dev/null +++ b/src/Umbraco.Tests/Persistence/Migrations/MigrationStartupHandlerTests.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Moq; +using NUnit.Framework; +using Semver; +using Umbraco.Core; +using Umbraco.Core.Events; +using Umbraco.Core.Logging; +using Umbraco.Core.Persistence; +using Umbraco.Core.Persistence.Migrations; +using Umbraco.Core.Profiling; +using Umbraco.Core.Services; +using Umbraco.Web.Strategies.Migrations; + +namespace Umbraco.Tests.Persistence.Migrations +{ + [TestFixture] + public class MigrationStartupHandlerTests + { + [Test] + public void Executes_For_Any_Product_Name_When_Not_Specified() + { + var changed1 = new Args { CountExecuted = 0 }; + var testHandler1 = new TestMigrationHandler(changed1); + testHandler1.OnApplicationStarting(Mock.Of(), new ApplicationContext(CacheHelper.CreateDisabledCacheHelper(), new ProfilingLogger(Mock.Of(), Mock.Of()))); + + var conn = new Mock(); + conn.Setup(x => x.BeginTransaction(It.IsAny())).Returns(Mock.Of()); + var db = new Mock(conn.Object); + + var runner1 = new MigrationRunner(Mock.Of(), Mock.Of(), new SemVersion(1), new SemVersion(2), "Test1", + new IMigration[] { Mock.Of() }); + var result1 = runner1.Execute(db.Object, DatabaseProviders.SqlServerCE, false); + Assert.AreEqual(1, changed1.CountExecuted); + } + + [Test] + public void Executes_Only_For_Specified_Product_Name() + { + var changed1 = new Args { CountExecuted = 0}; + var testHandler1 = new TestMigrationHandler("Test1", changed1); + testHandler1.OnApplicationStarting(Mock.Of(), new ApplicationContext(CacheHelper.CreateDisabledCacheHelper(), new ProfilingLogger(Mock.Of(), Mock.Of()))); + var changed2 = new Args { CountExecuted = 0 }; + var testHandler2 = new TestMigrationHandler("Test2", changed2); + testHandler2.OnApplicationStarting(Mock.Of(), new ApplicationContext(CacheHelper.CreateDisabledCacheHelper(), new ProfilingLogger(Mock.Of(), Mock.Of()))); + + var conn = new Mock(); + conn.Setup(x => x.BeginTransaction(It.IsAny())).Returns(Mock.Of()); + var db = new Mock(conn.Object); + + var runner1 = new MigrationRunner(Mock.Of(), Mock.Of(), new SemVersion(1), new SemVersion(2), "Test1", + new IMigration[] { Mock.Of()}); + var result1 = runner1.Execute(db.Object, DatabaseProviders.SqlServerCE, false); + Assert.AreEqual(1, changed1.CountExecuted); + Assert.AreEqual(0, changed2.CountExecuted); + + var runner2 = new MigrationRunner(Mock.Of(), Mock.Of(), new SemVersion(1), new SemVersion(2), "Test2", + new IMigration[] { Mock.Of() }); + var result2 = runner2.Execute(db.Object, DatabaseProviders.SqlServerCE, false); + Assert.AreEqual(1, changed1.CountExecuted); + Assert.AreEqual(1, changed2.CountExecuted); + } + + public class Args + { + public int CountExecuted { get; set; } + } + + public class TestMigrationHandler : MigrationStartupHander + { + private readonly string _prodName; + private readonly Args _changed; + + public TestMigrationHandler(Args changed) + { + _changed = changed; + } + + public TestMigrationHandler(string prodName, Args changed) + { + _prodName = prodName; + _changed = changed; + } + + protected override void AfterMigration(MigrationRunner sender, MigrationEventArgs e) + { + _changed.CountExecuted++; + } + + public override string[] TargetProductNames + { + get { return _prodName.IsNullOrWhiteSpace() ? new string[] {} : new[] {_prodName}; } + } + } + } +} diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index e553be9b49..deda4f5794 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -175,6 +175,7 @@ + diff --git a/src/Umbraco.Web/Strategies/Migrations/MigrationStartupHander.cs b/src/Umbraco.Web/Strategies/Migrations/MigrationStartupHander.cs index 6b2c92f79f..a350911580 100644 --- a/src/Umbraco.Web/Strategies/Migrations/MigrationStartupHander.cs +++ b/src/Umbraco.Web/Strategies/Migrations/MigrationStartupHander.cs @@ -1,4 +1,5 @@ -using Umbraco.Core; +using System.Linq; +using Umbraco.Core; using Umbraco.Core.Persistence.Migrations; namespace Umbraco.Web.Strategies.Migrations @@ -41,9 +42,20 @@ namespace Umbraco.Web.Strategies.Migrations private void MigrationRunner_Migrated(MigrationRunner sender, Core.Events.MigrationEventArgs e) { - AfterMigration(sender, e); + if (TargetProductNames.Length == 0 || TargetProductNames.Contains(e.ProductName)) + { + AfterMigration(sender, e); + } } protected abstract void AfterMigration(MigrationRunner sender, Core.Events.MigrationEventArgs e); + + /// + /// Override to specify explicit target product names + /// + /// + /// Leaving empty will run for all migration products + /// + public virtual string[] TargetProductNames { get { return new string[] {}; } } } } \ No newline at end of file