Adds ability to have proxy migrations so there is no duplication of declaring migrations and no duplication of executing already executed migrations. Would have been a bit nicer to have

This commit is contained in:
Shannon
2014-03-12 20:36:40 +11:00
parent 3b26834484
commit 0dc00a62e2
13 changed files with 226 additions and 116 deletions

View File

@@ -6,7 +6,7 @@ namespace Umbraco.Core.Persistence.Migrations
/// Represents the Migration attribute, which is used to mark classes as
/// database migrations with Up/Down methods for pushing changes UP or pulling them DOWN.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class MigrationAttribute : Attribute
{
public MigrationAttribute(string targetVersion, int sortOrder, string product)

View File

@@ -27,26 +27,8 @@ namespace Umbraco.Core.Persistence.Migrations
/// </summary>
public IEnumerable<IMigration> Migrations
{
get { return GetSortedValues(); }
get { return Values; }
}
/// <summary>
/// Override how we determine object weight, for this resolver we use the MigrationAttribute attribute
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
protected override int GetObjectWeight(object o)
{
var type = o.GetType();
var attr = type.GetCustomAttribute<MigrationAttribute>(true);
return attr == null ? DefaultPluginWeight : attr.SortOrder;
}
protected override int DefaultPluginWeight
{
get { return 0; } //set's the default to 0
set { base.DefaultPluginWeight = value; }
}
}
}

View File

@@ -51,27 +51,15 @@ namespace Umbraco.Core.Persistence.Migrations
? OrderedUpgradeMigrations(foundMigrations).ToList()
: OrderedDowngradeMigrations(foundMigrations).ToList();
//SD: Why do we want this?
if (Migrating.IsRaisedEventCancelled(new MigrationEventArgs(migrations, _configuredVersion, _targetVersion, true), this))
return false;
//Loop through migrations to generate sql
var context = new MigrationContext(databaseProvider, database);
foreach (MigrationBase migration in migrations)
{
if (isUpgrade)
{
migration.GetUpExpressions(context);
LogHelper.Info<MigrationRunner>(string.Format("Added UPGRADE migration '{0}' to context", migration.GetType().Name));
}
else
{
migration.GetDownExpressions(context);
LogHelper.Info<MigrationRunner>(string.Format("Added DOWNGRADE migration '{0}' to context", migration.GetType().Name));
}
}
var context = ExecuteMigrations(migrations, database, databaseProvider, isUpgrade);
//Transactional execution of the sql that was generated from the found migrations
using (Transaction transaction = database.GetTransaction())
using (var transaction = database.GetTransaction())
{
int i = 1;
foreach (var expression in context.Expressions)
@@ -96,32 +84,73 @@ namespace Umbraco.Core.Persistence.Migrations
return true;
}
internal MigrationContext ExecuteMigrations(List<IMigration> migrations, Database database, DatabaseProviders databaseProvider, bool isUpgrade = true)
{
//Loop through migrations to generate sql
var context = new MigrationContext(databaseProvider, database);
foreach (var migration in migrations)
{
var baseMigration = migration as MigrationBase;
if (baseMigration != null)
{
if (isUpgrade)
{
baseMigration.GetUpExpressions(context);
LogHelper.Info<MigrationRunner>(string.Format("Added UPGRADE migration '{0}' to context", baseMigration.GetType().Name));
}
else
{
baseMigration.GetDownExpressions(context);
LogHelper.Info<MigrationRunner>(string.Format("Added DOWNGRADE migration '{0}' to context", baseMigration.GetType().Name));
}
}
else
{
//this is just a normal migration so we can only call Up/Down
if (isUpgrade)
{
migration.Up();
LogHelper.Info<MigrationRunner>(string.Format("Added UPGRADE migration '{0}' to context", migration.GetType().Name));
}
else
{
migration.Down();
LogHelper.Info<MigrationRunner>(string.Format("Added DOWNGRADE migration '{0}' to context", migration.GetType().Name));
}
}
}
return context;
}
internal IEnumerable<IMigration> OrderedUpgradeMigrations(IEnumerable<IMigration> foundMigrations)
{
var migrations = (from migration in foundMigrations
let migrationAttribute = migration.GetType().FirstAttribute<MigrationAttribute>()
where migrationAttribute != null
where
migrationAttribute.TargetVersion > _configuredVersion &&
migrationAttribute.TargetVersion <= _targetVersion &&
migrationAttribute.ProductName == _productName
orderby migrationAttribute.TargetVersion, migrationAttribute.SortOrder ascending
select migration);
let migrationAttributes = migration.GetType().GetCustomAttributes<MigrationAttribute>(false)
from migrationAttribute in migrationAttributes
where migrationAttribute != null
where
migrationAttribute.TargetVersion > _configuredVersion &&
migrationAttribute.TargetVersion <= _targetVersion &&
migrationAttribute.ProductName == _productName
orderby migrationAttribute.TargetVersion, migrationAttribute.SortOrder ascending
select migration).Distinct();
return migrations;
}
public IEnumerable<IMigration> OrderedDowngradeMigrations(IEnumerable<IMigration> foundMigrations)
{
var migrations = (from migration in foundMigrations
let migrationAttribute = migration.GetType().FirstAttribute<MigrationAttribute>()
where migrationAttribute != null
where
migrationAttribute.TargetVersion > _configuredVersion &&
migrationAttribute.TargetVersion <= _targetVersion &&
migrationAttribute.ProductName == _productName
orderby migrationAttribute.TargetVersion, migrationAttribute.SortOrder descending
select migration);
let migrationAttributes = migration.GetType().GetCustomAttributes<MigrationAttribute>(false)
from migrationAttribute in migrationAttributes
where migrationAttribute != null
where
migrationAttribute.TargetVersion > _configuredVersion &&
migrationAttribute.TargetVersion <= _targetVersion &&
migrationAttribute.ProductName == _productName
orderby migrationAttribute.TargetVersion, migrationAttribute.SortOrder descending
select migration).Distinct();
return migrations;
}