Replies: 1 comment
-
@jzabroski - So this is what I came up with, hope it make sense. The using System;
namespace FluentMigrator.Runner.Constraints
{
/// <summary>
/// Can be used to apply conditions when migrations will be run.
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class SqlConstraintAttribute : Attribute
{
/// <summary>
/// Validate SQL script
/// </summary>
public string ValidationScript { get; }
/// <inheritdoc />
public SqlConstraintAttribute(string validationScript)
{
ValidationScript = validationScript;
}
}
}
// in MigrationRunner.cs code, we'll have the following new code
namespace FluentMigrator.Runner
{
/// <summary>
/// The default <see cref="IMigrationRunner"/> implementation
/// </summary>
public class MigrationRunner : IMigrationRunner
{
private bool CheckMigrationConstraints(IMigrationInfo migrationInfo, bool useAutomaticTransactionManagement)
{
bool IsMigrationConstraintValid(Type migrationType)
{
var constraints = migrationType.GetCustomAttributes().OfType<SqlConstraintAttribute>().ToArray();
return constraints.All(c => string.IsNullOrWhiteSpace(c.ValidationScript))
|| constraints.All(constraint => VersionLoader.Runner.Processor.Exists(constraint.ValidationScript));
}
return IsMigrationConstraintValid(migrationInfo.Migration.GetType());
}
/// <summary>
/// Apply migrations up to the given <paramref name="targetVersion"/>
/// </summary>
/// <param name="targetVersion">The target migration version</param>
/// <param name="useAutomaticTransactionManagement"><c>true</c> if automatic transaction management should be used</param>
public void MigrateUp(long targetVersion, bool useAutomaticTransactionManagement)
{
var migrationInfos = GetUpMigrationsToApply(targetVersion);
using (IMigrationScope scope = _migrationScopeManager.CreateOrWrapMigrationScope(useAutomaticTransactionManagement && TransactionPerSession))
{
try
{
ApplyMaintenance(MigrationStage.BeforeAll, useAutomaticTransactionManagement);
foreach (var migrationInfo in migrationInfos)
{
// new code to determine whether to proceed with this particular migration or not
if (!CheckMigrationConstraints(migrationInfo, useAutomaticTransactionManagement))
{
continue;
}
ApplyMaintenance(MigrationStage.BeforeEach, useAutomaticTransactionManagement);
ApplyMigrationUp(migrationInfo, useAutomaticTransactionManagement && migrationInfo.TransactionBehavior == TransactionBehavior.Default);
ApplyMaintenance(MigrationStage.AfterEach, useAutomaticTransactionManagement);
}
ApplyMaintenance(MigrationStage.BeforeProfiles, useAutomaticTransactionManagement);
ApplyProfiles();
ApplyMaintenance(MigrationStage.AfterAll, useAutomaticTransactionManagement);
scope.Complete();
}
catch
{
if (scope.IsActive)
{
scope.Cancel(); // SQLAnywhere needs explicit call to rollback transaction
}
throw;
}
}
VersionLoader.LoadVersionInfo();
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
@jzabroski Further to closed issue
Thanks for pointing out gist but yeah I'd like to see if this get run on every migration.
I'll draft some code up and we'll discuss further
Beta Was this translation helpful? Give feedback.
All reactions