Skip to content

Commit

Permalink
Merge pull request #308 from DbUp/enh-scriptOrdering
Browse files Browse the repository at this point in the history
Ordered all scripts by name. Sped up the comparison of executed scripts.
  • Loading branch information
droyad committed Feb 7, 2018
2 parents 512afdc + addbff3 commit e558491
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/dbup-core/Builder/StandardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,21 @@ public static UpgradeEngineBuilder WithScriptsAndCodeEmbeddedInAssembly(this Upg
{
return WithScripts(builder, new EmbeddedScriptAndCodeProvider(assembly, filter));
}


/// <summary>
/// Adds a sorter that sorts the scripts before filtering and execution
/// </summary>
/// <param name="builder">The builder.</param>
/// <param name="sorter">The sorter.</param>
/// <returns>
/// The same builder
/// </returns>
public static UpgradeEngineBuilder WithSorter(this UpgradeEngineBuilder builder, IScriptSorter sorter)
{
builder.Configure(b => b.ScriptSorter = sorter);
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
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using DbUp.Engine;
using DbUp.Engine.Output;
using DbUp.Engine.Sorters;
using DbUp.Engine.Transactions;

namespace DbUp.Builder
Expand Down Expand Up @@ -58,6 +59,11 @@ public UpgradeConfiguration()
/// </summary>
public IScriptExecutor ScriptExecutor { get; set; }

/// <summary>
/// Gets or sets the script sorter, which defines the order the scripts execute in
/// </summary>
public IScriptSorter ScriptSorter { get; set; } = new AlphabeticalScriptSorter();

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

namespace DbUp.Engine
{
public interface IScriptSorter
{
IEnumerable<SqlScript> Sort(IEnumerable<SqlScript> filtered);
}
}
11 changes: 11 additions & 0 deletions src/dbup-core/Engine/Sorters/AlphabeticalScriptSorter.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.Sorters
{
public class AlphabeticalScriptSorter : IScriptSorter
{
public IEnumerable<SqlScript> Sort(IEnumerable<SqlScript> filtered)
=> filtered.OrderBy(s => s.Name);
}
}
6 changes: 4 additions & 2 deletions src/dbup-core/Engine/UpgradeEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ public List<SqlScript> GetScriptsToExecute()
private List<SqlScript> GetScriptsToExecuteInsideOperation()
{
var allScripts = configuration.ScriptProviders.SelectMany(scriptProvider => scriptProvider.GetScripts(configuration.ConnectionManager));
var executedScripts = configuration.Journal.GetExecutedScripts();
var executedScriptNames = new HashSet<string>(configuration.Journal.GetExecutedScripts());

return allScripts.Where(s => !executedScripts.Any(y => y == s.Name)).ToList();
var sorted = configuration.ScriptSorter.Sort(allScripts);
var filtered = sorted.Where(s => !executedScriptNames.Contains(s.Name));
return filtered.ToList();
}

public List<string> GetExecutedScripts()
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 @@ -34,6 +34,7 @@ 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 @@ -82,6 +83,7 @@ public class UpgradeConfiguration
public DbUp.Engine.IScriptExecutor ScriptExecutor { 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 AddVariables(System.Collections.Generic.IDictionary<string, string> newVariables) { }
Expand Down Expand Up @@ -127,6 +129,10 @@ 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 @@ -219,6 +225,14 @@ 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

0 comments on commit e558491

Please sign in to comment.