Skip to content

Commit

Permalink
Merge pull request #314 from DbUp/enh-scriptNameComparer
Browse files Browse the repository at this point in the history
Replace IScriptSorter with IComparer<string>
  • Loading branch information
droyad committed Feb 17, 2018
2 parents d199032 + 5ea51da commit 8da8cea
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 52 deletions.
18 changes: 11 additions & 7 deletions src/dbup-core/Builder/StandardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
using DbUp;
using DbUp.Builder;
using DbUp.Engine;
using DbUp.Engine.Filters;
using DbUp.Engine.Output;
using DbUp.Engine.Transactions;
using DbUp.ScriptProviders;
using DbUp.Support;

/// <summary>
/// Configuration extensions for the standard stuff.
Expand Down Expand Up @@ -343,23 +345,25 @@ public static UpgradeEngineBuilder WithScriptsAndCodeEmbeddedInAssembly(this Upg
}

/// <summary>
/// Adds a sorter that sorts the scripts before filtering and execution
/// Sets the comparer used to sort scripts and match script names against the log of already run scripts.
/// The default comparer is StringComparer.Ordinal.
/// By implementing your own comparer you can make make the matching and ordering case insensitive,
/// change how numbers are handled or support the renaming of scripts
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="sorter">The sorter.</param>
/// <param name="comparer">The sorter.</param>
/// <returns>
/// The same builder
/// </returns>
public static UpgradeEngineBuilder WithSorter(this UpgradeEngineBuilder builder, IScriptSorter sorter)
public static UpgradeEngineBuilder WithScriptNameComparer(this UpgradeEngineBuilder builder, IComparer<string> comparer)
{
builder.Configure(b => b.ScriptSorter = sorter);
builder.Configure(b => b.ScriptNameComparer = new ScriptNameComparer(comparer));
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
/// Sets the 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.
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="filter">The filter.</param>
Expand Down
9 changes: 6 additions & 3 deletions src/dbup-core/Builder/UpgradeConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using DbUp.Engine;
using DbUp.Engine.Filters;
using DbUp.Engine.Output;
using DbUp.Engine.Sorters;
using DbUp.Engine.Transactions;
using DbUp.Support;

namespace DbUp.Builder
{
Expand Down Expand Up @@ -75,9 +75,12 @@ public void AddLog(IUpgradeLog additionalLog)
public IScriptExecutor ScriptExecutor { get; set; }

/// <summary>
/// Gets or sets the script sorter, which defines the order the scripts execute in
/// Gets or sets the comparer used to sort scripts and match script names against the log of already run scripts.
/// The default comparer is StringComparer.Ordinal.
/// By implementing your own comparer you can make make the matching and ordering case insensitive,
/// change how numbers are handled or support the renaming of scripts
/// </summary>
public IScriptSorter ScriptSorter { get; set; } = new AlphabeticalScriptSorter();
public ScriptNameComparer ScriptNameComparer { get; set; } = new ScriptNameComparer(StringComparer.Ordinal);

/// <summary>
/// Gets or sets the script filter, which filters the scripts before execution
Expand Down
8 changes: 5 additions & 3 deletions src/dbup-core/Engine/Filters/DefaultScriptFilter.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using DbUp.Support;

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));
public IEnumerable<SqlScript> Filter(IEnumerable<SqlScript> sorted, HashSet<string> executedScriptNames, ScriptNameComparer comparer)
=> sorted.Where(s => !executedScriptNames.Contains(s.Name, comparer));
}
}
4 changes: 3 additions & 1 deletion src/dbup-core/Engine/IScriptFilter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Collections.Generic;
using DbUp.Engine.Filters;
using DbUp.Support;

namespace DbUp.Engine
{
public interface IScriptFilter
{
IEnumerable<SqlScript> Filter(IEnumerable<SqlScript> sorted, HashSet<string> executedScriptNames);
IEnumerable<SqlScript> Filter(IEnumerable<SqlScript> sorted, HashSet<string> executedScriptNames, ScriptNameComparer comparer);
}
}
9 changes: 0 additions & 9 deletions src/dbup-core/Engine/IScriptSorter.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/dbup-core/Engine/Sorters/AlphabeticalScriptSorter.cs

This file was deleted.

5 changes: 3 additions & 2 deletions src/dbup-core/Engine/UpgradeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using DbUp.Builder;
using DbUp.Engine.Filters;

namespace DbUp.Engine
{
Expand Down Expand Up @@ -102,8 +103,8 @@ private List<SqlScript> GetScriptsToExecuteInsideOperation()
var allScripts = configuration.ScriptProviders.SelectMany(scriptProvider => scriptProvider.GetScripts(configuration.ConnectionManager));
var executedScriptNames = new HashSet<string>(configuration.Journal.GetExecutedScripts());

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

Expand Down
21 changes: 21 additions & 0 deletions src/dbup-core/Support/ScriptNameComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;

namespace DbUp.Support
{
public class ScriptNameComparer : IComparer<string>, IEqualityComparer<string>
{
private readonly IComparer<string> comparer;

public ScriptNameComparer(IComparer<string> comparer)
{
this.comparer = comparer;
}

public int Compare(string x, string y) => comparer.Compare(x, y);

public bool Equals(string x, string y) => comparer.Compare(x, y) == 0;

public int GetHashCode(string obj) => obj.GetHashCode();
}
}
27 changes: 11 additions & 16 deletions src/dbup-tests/ApprovalFiles/dbup-core.approved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class StandardExtensions
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) { }
public static DbUp.Builder.UpgradeEngineBuilder WithScript(this DbUp.Builder.UpgradeEngineBuilder builder, string name, string contents) { }
public static DbUp.Builder.UpgradeEngineBuilder WithScriptNameComparer(this DbUp.Builder.UpgradeEngineBuilder builder, System.Collections.Generic.IComparer<string> comparer) { }
public static DbUp.Builder.UpgradeEngineBuilder WithScripts(this DbUp.Builder.UpgradeEngineBuilder builder, DbUp.Engine.IScriptProvider scriptProvider) { }
public static DbUp.Builder.UpgradeEngineBuilder WithScripts(this DbUp.Builder.UpgradeEngineBuilder builder, System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> scripts) { }
public static DbUp.Builder.UpgradeEngineBuilder WithScripts(this DbUp.Builder.UpgradeEngineBuilder builder, params DbUp.Engine.SqlScript[] scripts) { }
Expand All @@ -36,7 +37,6 @@ public static class StandardExtensions
public static DbUp.Builder.UpgradeEngineBuilder WithScriptsFromFileSystem(this DbUp.Builder.UpgradeEngineBuilder builder, string path, System.Text.Encoding encoding) { }
public static DbUp.Builder.UpgradeEngineBuilder WithScriptsFromFileSystem(this DbUp.Builder.UpgradeEngineBuilder builder, string path, System.Func<string, bool> filter, System.Text.Encoding encoding) { }
public static DbUp.Builder.UpgradeEngineBuilder WithScriptsFromFileSystem(this DbUp.Builder.UpgradeEngineBuilder builder, string path, DbUp.ScriptProviders.FileSystemScriptOptions options) { }
public static DbUp.Builder.UpgradeEngineBuilder WithSorter(this DbUp.Builder.UpgradeEngineBuilder builder, DbUp.Engine.IScriptSorter sorter) { }
public static DbUp.Builder.UpgradeEngineBuilder WithTransaction(this DbUp.Builder.UpgradeEngineBuilder builder) { }
public static DbUp.Builder.UpgradeEngineBuilder WithTransactionPerScript(this DbUp.Builder.UpgradeEngineBuilder builder) { }
public static DbUp.Builder.UpgradeEngineBuilder WithVariable(this DbUp.Builder.UpgradeEngineBuilder builder, string variableName, string value) { }
Expand Down Expand Up @@ -84,9 +84,9 @@ public class UpgradeConfiguration
public DbUp.Engine.Output.IUpgradeLog Log { get; set; }
public DbUp.Engine.IScriptExecutor ScriptExecutor { get; set; }
public DbUp.Engine.IScriptFilter ScriptFilter { get; set; }
public DbUp.Support.ScriptNameComparer ScriptNameComparer { 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; }
public System.Collections.Generic.Dictionary<string, string> Variables { get; }
public bool VariablesEnabled { get; set; }
public void AddLog(DbUp.Engine.Output.IUpgradeLog additionalLog) { }
Expand Down Expand Up @@ -127,7 +127,7 @@ public interface IScriptExecutor
}
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);
System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> Filter(System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> sorted, System.Collections.Generic.HashSet<string> executedScriptNames, DbUp.Support.ScriptNameComparer comparer);
}
public interface IScriptPreprocessor
{
Expand All @@ -137,10 +137,6 @@ public interface IScriptProvider
{
System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> GetScripts(DbUp.Engine.Transactions.IConnectionManager connectionManager);
}
public interface IScriptSorter
{
System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> Sort(System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> filtered);
}
public interface ISqlObjectParser
{
string QuoteIdentifier(string objectName);
Expand Down Expand Up @@ -179,7 +175,7 @@ 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) { }
public System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> Filter(System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> sorted, System.Collections.Generic.HashSet<string> executedScriptNames, DbUp.Support.ScriptNameComparer comparer) { }
}
}
namespace DbUp.Engine.Output
Expand Down Expand Up @@ -248,14 +244,6 @@ public class VariableSubstitutionSqlParser : DbUp.Support.SqlParser, System.IDis
protected virtual bool ValidVariableNameCharacter(char c) { }
}
}
namespace DbUp.Engine.Sorters
{
public class AlphabeticalScriptSorter : DbUp.Engine.IScriptSorter
{
public AlphabeticalScriptSorter() { }
public System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> Sort(System.Collections.Generic.IEnumerable<DbUp.Engine.SqlScript> filtered) { }
}
}
namespace DbUp.Engine.Transactions
{
public abstract class DatabaseConnectionManager : DbUp.Engine.Transactions.IConnectionManager
Expand Down Expand Up @@ -400,6 +388,13 @@ public abstract class ScriptExecutor : DbUp.Engine.IScriptExecutor
public void VerifySchema() { }
protected virtual void WriteReaderToLog(System.Data.IDataReader reader) { }
}
public class ScriptNameComparer : System.Collections.Generic.IComparer<string>, System.Collections.Generic.IEqualityComparer<string>
{
public ScriptNameComparer(System.Collections.Generic.IComparer<string> comparer) { }
public int Compare(string x, string y) { }
public bool Equals(string x, string y) { }
public int GetHashCode(string obj) { }
}
public class SqlCommandReader : DbUp.Support.SqlParser, System.IDisposable
{
public SqlCommandReader(string sqlText, string delimiter = "GO", bool delimiterRequiresWhitespace = true) { }
Expand Down

0 comments on commit 8da8cea

Please sign in to comment.