Skip to content

Commit

Permalink
Merge pull request #311 from DbUp/enh-scriptFiltering
Browse files Browse the repository at this point in the history
Added the ability to specify a script filter
  • Loading branch information
droyad committed Feb 7, 2018
2 parents e558491 + c2686ea commit a341fcb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/dbup-core/Builder/StandardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,22 @@ public static UpgradeEngineBuilder WithSorter(this UpgradeEngineBuilder builder,
return builder;
}

/// <summary>
/// Adds a filter that filters the sorted lists of script prior to execution. This allows
/// scripts to be excluded based on which scripts have already been run. This can be used to
/// make the filtering case-insensitive, or support re-named scripts
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="filter">The filter.</param>
/// <returns>
/// The same builder
/// </returns>
public static UpgradeEngineBuilder WithFilter(this UpgradeEngineBuilder builder, IScriptFilter filter)
{
builder.Configure(b => b.ScriptFilter = filter);
return builder;
}

/// <summary>
/// Adds a preprocessor that can replace portions of a script.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/dbup-core/Builder/UpgradeConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using DbUp.Engine;
using DbUp.Engine.Filters;
using DbUp.Engine.Output;
using DbUp.Engine.Sorters;
using DbUp.Engine.Transactions;
Expand Down Expand Up @@ -64,6 +65,11 @@ public UpgradeConfiguration()
/// </summary>
public IScriptSorter ScriptSorter { get; set; } = new AlphabeticalScriptSorter();

/// <summary>
/// Gets or sets the script filter, which filters the scripts before execution
/// </summary>
public IScriptFilter ScriptFilter { get; set; } = new DefaultScriptFilter();

/// <summary>
/// A collection of variables to be replaced in scripts before they are run
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/dbup-core/Engine/Filters/DefaultScriptFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using System.Linq;

namespace DbUp.Engine.Filters
{
public class DefaultScriptFilter : IScriptFilter
{
public IEnumerable<SqlScript> Filter(IEnumerable<SqlScript> sorted, HashSet<string> executedScriptNames)
=> sorted.Where(s => !executedScriptNames.Contains(s.Name));
}
}
9 changes: 9 additions & 0 deletions src/dbup-core/Engine/IScriptFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace DbUp.Engine
{
public interface IScriptFilter
{
IEnumerable<SqlScript> Filter(IEnumerable<SqlScript> sorted, HashSet<string> executedScriptNames);
}
}
2 changes: 1 addition & 1 deletion src/dbup-core/Engine/UpgradeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private List<SqlScript> GetScriptsToExecuteInsideOperation()
var executedScriptNames = new HashSet<string>(configuration.Journal.GetExecutedScripts());

var sorted = configuration.ScriptSorter.Sort(allScripts);
var filtered = sorted.Where(s => !executedScriptNames.Contains(s.Name));
var filtered = configuration.ScriptFilter.Filter(sorted, executedScriptNames);
return filtered.ToList();
}

Expand Down
14 changes: 14 additions & 0 deletions src/dbup-tests/ApprovalFiles/dbup-core.approved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public static class StandardExtensions
public static DbUp.Builder.UpgradeEngineBuilder LogToNowhere(this DbUp.Builder.UpgradeEngineBuilder builder) { }
public static DbUp.Builder.UpgradeEngineBuilder LogToTrace(this DbUp.Builder.UpgradeEngineBuilder builder) { }
public static DbUp.Builder.UpgradeEngineBuilder WithExecutionTimeout(this DbUp.Builder.UpgradeEngineBuilder builder, System.Nullable<System.TimeSpan> timeout) { }
public static DbUp.Builder.UpgradeEngineBuilder WithFilter(this DbUp.Builder.UpgradeEngineBuilder builder, DbUp.Engine.IScriptFilter filter) { }
public static DbUp.Builder.UpgradeEngineBuilder WithoutTransaction(this DbUp.Builder.UpgradeEngineBuilder builder) { }
public static DbUp.Builder.UpgradeEngineBuilder WithPreprocessor(this DbUp.Builder.UpgradeEngineBuilder builder, DbUp.Engine.IScriptPreprocessor preprocessor) { }
public static DbUp.Builder.UpgradeEngineBuilder WithScript(this DbUp.Builder.UpgradeEngineBuilder builder, DbUp.Engine.SqlScript script) { }
Expand Down Expand Up @@ -81,6 +82,7 @@ public class UpgradeConfiguration
public DbUp.Engine.IJournal Journal { get; set; }
public DbUp.Engine.Output.IUpgradeLog Log { get; set; }
public DbUp.Engine.IScriptExecutor ScriptExecutor { get; set; }
public DbUp.Engine.IScriptFilter ScriptFilter { get; set; }
public System.Collections.Generic.List<DbUp.Engine.IScriptPreprocessor> ScriptPreprocessors { get; }
public System.Collections.Generic.List<DbUp.Engine.IScriptProvider> ScriptProviders { get; }
public DbUp.Engine.IScriptSorter ScriptSorter { get; set; }
Expand Down Expand Up @@ -121,6 +123,10 @@ public interface IScriptExecutor
void Execute(DbUp.Engine.SqlScript script, System.Collections.Generic.IDictionary<string, string> variables);
void VerifySchema();
}
public interface IScriptFilter
{
System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> Filter(System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> sorted, System.Collections.Generic.HashSet<string> executedScriptNames);
}
public interface IScriptPreprocessor
{
string Process(string contents);
Expand Down Expand Up @@ -166,6 +172,14 @@ public class UpgradeEngine
public bool TryConnect(out string errorMessage) { }
}
}
namespace DbUp.Engine.Filters
{
public class DefaultScriptFilter : DbUp.Engine.IScriptFilter
{
public DefaultScriptFilter() { }
public System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> Filter(System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> sorted, System.Collections.Generic.HashSet<string> executedScriptNames) { }
}
}
namespace DbUp.Engine.Output
{
public class AutodetectUpgradeLog : DbUp.Engine.Output.IUpgradeLog
Expand Down

0 comments on commit a341fcb

Please sign in to comment.