Skip to content
This repository has been archived by the owner on Dec 13, 2021. It is now read-only.

Adds Migration Helper methods #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/Our.Umbraco.InnerContent/Helpers/MigrationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Linq;
using System.Web;
using Semver;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.Migrations;
using Umbraco.Core.Services;

namespace Our.Umbraco.InnerContent.Helpers
{
public static class MigrationHelper
{
public static void ApplyMigrations(
ApplicationContext applicationContext,
string migrationName,
SemVersion currentVersion,
SemVersion targetVersion)
{
// get the latest migration executed
var latestMigration = applicationContext.Services.MigrationEntryService.GetLatest(migrationName);

if (latestMigration != null)
currentVersion = latestMigration.Version;

if (targetVersion == currentVersion)
return;

var migrationsRunner = new MigrationRunner(
applicationContext.Services.MigrationEntryService,
applicationContext.ProfilingLogger.Logger,
currentVersion,
targetVersion,
migrationName);

try
{
migrationsRunner.Execute(applicationContext.DatabaseContext.Database);
}
catch (HttpException)
{
// because umbraco runs some other migrations after the migration runner
// is executed we get HttpException
// catch this error, but don't do anything
// fixed in 7.4.2+ see : http://issues.umbraco.org/issue/U4-8077
}
catch (Exception ex)
{
LogHelper.Error(typeof(MigrationHelper), "Error running migration.", ex);
}
}

private static IMigrationEntry GetLatest(this IMigrationEntryService service, string migrationName)
{
// TODO: Figure out if we can use the UnitOfWorkProvider here,
// but it appears to be marked as protected within Umbraco Core.

return service
.GetAll(migrationName)
.OrderByDescending(x => x.Version)
.FirstOrDefault();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
<Compile Include="Bootstrap.cs" />
<Compile Include="Converters\InnerContentValueConverter.cs" />
<Compile Include="Helpers\InnerContentHelper.cs" />
<Compile Include="Helpers\MigrationHelper.cs" />
<Compile Include="InnerContentConstants.cs" />
<Compile Include="Models\DetachedPublishedContent.cs" />
<Compile Include="Models\DetachedPublishedProperty.cs" />
Expand Down