From 7328317fc81b7ed9e88d95da22bcbca3a93b6b1b Mon Sep 17 00:00:00 2001 From: dia0369 Date: Wed, 4 Feb 2015 16:35:59 +0000 Subject: [PATCH] Added support for selecting encoding when using scripts embedded in an assembly or scripts from file system. --- src/DbUp/Builder/StandardExtensions.cs | 88 +++++++++-- src/DbUp/Engine/SqlScript.cs | 146 +++++++++--------- .../ScriptProviders/EmbeddedScriptProvider.cs | 26 +++- .../FileSystemScriptProvider.cs | 41 ++++- 4 files changed, 203 insertions(+), 98 deletions(-) diff --git a/src/DbUp/Builder/StandardExtensions.cs b/src/DbUp/Builder/StandardExtensions.cs index 378cedfa..259279ef 100644 --- a/src/DbUp/Builder/StandardExtensions.cs +++ b/src/DbUp/Builder/StandardExtensions.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; -using System.Reflection; +using System.Reflection; +using System.Text; +using DbUp; using DbUp.Builder; using DbUp.Engine; using DbUp.Engine.Output; @@ -175,6 +177,35 @@ public static UpgradeEngineBuilder WithScriptsFromFileSystem(this UpgradeEngineB public static UpgradeEngineBuilder WithScriptsFromFileSystem(this UpgradeEngineBuilder builder, string path, Func filter) { return WithScripts(builder, new FileSystemScriptProvider(path, filter)); + } + + /// + /// Adds all scripts from a folder on the file system, with custom encoding. + /// + /// The builder. + /// The directory path. + /// The encoding. + /// + /// The same builder + /// + public static UpgradeEngineBuilder WithScriptsFromFileSystem(this UpgradeEngineBuilder builder, string path, Encoding encoding) + { + return WithScripts(builder, new FileSystemScriptProvider(path, encoding)); + } + + /// + /// Adds all scripts from a folder on the file system, with a custom filter and custom encoding. + /// + /// The builder. + /// The directory path. + /// The filter. Use the static class to get some pre-defined filters. + /// The encoding. + /// + /// The same builder + /// + public static UpgradeEngineBuilder WithScriptsFromFileSystem(this UpgradeEngineBuilder builder, string path, Func filter, Encoding encoding) + { + return WithScripts(builder, new FileSystemScriptProvider(path, filter, encoding)); } /// @@ -188,20 +219,49 @@ public static UpgradeEngineBuilder WithScriptsFromFileSystem(this UpgradeEngineB public static UpgradeEngineBuilder WithScriptsEmbeddedInAssembly(this UpgradeEngineBuilder builder, Assembly assembly) { return WithScripts(builder, new EmbeddedScriptProvider(assembly, s => s.EndsWith(".sql", StringComparison.InvariantCultureIgnoreCase))); - } - - /// - /// Adds all scripts found as embedded resources in the given assembly, with a custom filter (you'll need to exclude non- .SQL files yourself). - /// - /// The builder. - /// The assembly. - /// The filter. Don't forget to ignore any non- .SQL files. - /// - /// The same builder - /// - public static UpgradeEngineBuilder WithScriptsEmbeddedInAssembly(this UpgradeEngineBuilder builder, Assembly assembly, Func filter) + } + + /// + /// Adds all scripts found as embedded resources in the given assembly, with custom encoding. + /// + /// The builder. + /// The assembly. + /// The encoding. + /// + /// The same builder + /// + public static UpgradeEngineBuilder WithScriptsEmbeddedInAssembly(this UpgradeEngineBuilder builder, Assembly assembly, Encoding encoding) + { + return WithScripts(builder, new EmbeddedScriptProvider(assembly, s => s.EndsWith(".sql", StringComparison.InvariantCultureIgnoreCase), encoding)); + } + + /// + /// Adds all scripts found as embedded resources in the given assembly, with custom encoding and with a custom filter (you'll need to exclude non- .SQL files yourself). + /// + /// The builder. + /// The assembly. + /// The filter. Don't forget to ignore any non- .SQL files. + /// The encoding. + /// + /// The same builder + /// + public static UpgradeEngineBuilder WithScriptsEmbeddedInAssembly(this UpgradeEngineBuilder builder, Assembly assembly, Func filter, Encoding encoding) { - return WithScripts(builder, new EmbeddedScriptProvider(assembly, filter)); + return WithScripts(builder, new EmbeddedScriptProvider(assembly, filter, encoding)); + } + + /// + /// Adds all scripts found as embedded resources in the given assembly, with a custom filter (you'll need to exclude non- .SQL files yourself). + /// + /// The builder. + /// The assembly. + /// The filter. Don't forget to ignore any non- .SQL files. + /// + /// The same builder + /// + public static UpgradeEngineBuilder WithScriptsEmbeddedInAssembly(this UpgradeEngineBuilder builder, Assembly assembly, Func filter) + { + return WithScripts(builder, new EmbeddedScriptProvider(assembly, filter)); } /// diff --git a/src/DbUp/Engine/SqlScript.cs b/src/DbUp/Engine/SqlScript.cs index 4b7b10a1..94167536 100644 --- a/src/DbUp/Engine/SqlScript.cs +++ b/src/DbUp/Engine/SqlScript.cs @@ -1,73 +1,75 @@ - -using System.IO; -using System.Text; - -namespace DbUp.Engine -{ - /// - /// Represents a SQL Server script that comes from an embedded resource in an assembly. - /// - public class SqlScript - { - private readonly string contents; - private readonly string name; - - /// - /// Initializes a new instance of the class. - /// - /// The name. - /// The contents. - public SqlScript(string name, string contents) - { - this.name = name; - this.contents = contents; - } - - /// - /// Gets the contents of the script. - /// - /// - public virtual string Contents - { - get { return contents; } - } - - /// - /// Gets the name of the script. - /// - /// - public string Name - { - get { return name; } - } - - /// - /// - /// - /// - /// - public static SqlScript FromFile(string path) - { - using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) - { - var fileName = new FileInfo(path).Name; - return FromStream(fileName, fileStream); - } - } - - /// - /// - /// - /// - /// - /// - public static SqlScript FromStream(string scriptName, Stream stream) - { - using (var resourceStreamReader = new StreamReader(stream, Encoding.Default, true)) - { - string c = resourceStreamReader.ReadToEnd(); - return new SqlScript(scriptName, c); - } - } - } + +using System.IO; +using System.Text; + +namespace DbUp.Engine +{ + /// + /// Represents a SQL Server script that comes from an embedded resource in an assembly. + /// + public class SqlScript + { + private readonly string contents; + private readonly string name; + + /// + /// Initializes a new instance of the class. + /// + /// The name. + /// The contents. + public SqlScript(string name, string contents) + { + this.name = name; + this.contents = contents; + } + + /// + /// Gets the contents of the script. + /// + /// + public virtual string Contents + { + get { return contents; } + } + + /// + /// Gets the name of the script. + /// + /// + public string Name + { + get { return name; } + } + + /// + /// + /// + /// + /// + /// + public static SqlScript FromFile(string path, Encoding encoding) + { + using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) + { + var fileName = new FileInfo(path).Name; + return FromStream(fileName, fileStream, encoding); + } + } + + /// + /// + /// + /// + /// + /// + /// + public static SqlScript FromStream(string scriptName, Stream stream, Encoding encoding) + { + using (var resourceStreamReader = new StreamReader(stream, encoding, true)) + { + string c = resourceStreamReader.ReadToEnd(); + return new SqlScript(scriptName, c); + } + } + } } \ No newline at end of file diff --git a/src/DbUp/ScriptProviders/EmbeddedScriptProvider.cs b/src/DbUp/ScriptProviders/EmbeddedScriptProvider.cs index 3f408881..b6aa01cc 100644 --- a/src/DbUp/ScriptProviders/EmbeddedScriptProvider.cs +++ b/src/DbUp/ScriptProviders/EmbeddedScriptProvider.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; +using System.Reflection; +using System.Text; using DbUp.Engine; using DbUp.Engine.Transactions; @@ -13,8 +14,9 @@ namespace DbUp.ScriptProviders public class EmbeddedScriptProvider : IScriptProvider { private readonly Assembly assembly; - private readonly Func filter; - + private readonly Func filter; + private Encoding encoding; + /// /// Initializes a new instance of the class. /// @@ -23,7 +25,21 @@ public class EmbeddedScriptProvider : IScriptProvider public EmbeddedScriptProvider(Assembly assembly, Func filter) { this.assembly = assembly; - this.filter = filter; + this.filter = filter; + this.encoding = Encoding.Default; + } + + /// + /// Initializes a new instance of the class. + /// + /// The assembly. + /// The filter. + /// The encoding. + public EmbeddedScriptProvider(Assembly assembly, Func filter, Encoding encoding) + { + this.assembly = assembly; + this.filter = filter; + this.encoding = encoding; } /// @@ -36,7 +52,7 @@ public IEnumerable GetScripts(IConnectionManager connectionManager) .GetManifestResourceNames() .Where(filter) .OrderBy(x => x) - .Select(s => SqlScript.FromStream(s, assembly.GetManifestResourceStream(s))) + .Select(s => SqlScript.FromStream(s, assembly.GetManifestResourceStream(s), encoding)) .ToList(); } diff --git a/src/DbUp/ScriptProviders/FileSystemScriptProvider.cs b/src/DbUp/ScriptProviders/FileSystemScriptProvider.cs index 28c2549b..07c40a2c 100644 --- a/src/DbUp/ScriptProviders/FileSystemScriptProvider.cs +++ b/src/DbUp/ScriptProviders/FileSystemScriptProvider.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; +using System.Linq; +using System.Text; using DbUp.Engine; using DbUp.Engine.Transactions; @@ -13,7 +14,8 @@ namespace DbUp.ScriptProviders public class FileSystemScriptProvider : IScriptProvider { private readonly string directoryPath; - private readonly Func filter; + private readonly Func filter; + private readonly Encoding encoding; /// /// @@ -21,7 +23,8 @@ public class FileSystemScriptProvider : IScriptProvider public FileSystemScriptProvider(string directoryPath) { this.directoryPath = directoryPath; - this.filter = null; + this.filter = null; + this.encoding = Encoding.Default; } /// @@ -31,20 +34,44 @@ public FileSystemScriptProvider(string directoryPath) public FileSystemScriptProvider(string directoryPath, Func filter) { this.directoryPath = directoryPath; - this.filter = filter; + this.filter = filter; + this.encoding = Encoding.Default; + } + + /// + /// + ///Path to SQL upgrade scripts + ///The encoding. + public FileSystemScriptProvider(string directoryPath, Encoding encoding) + { + this.directoryPath = directoryPath; + this.filter = null; + this.encoding = encoding; + } + + /// + /// + ///Path to SQL upgrade scripts + ///The filter. + ///The encoding. + public FileSystemScriptProvider(string directoryPath, Func filter, Encoding encoding) + { + this.directoryPath = directoryPath; + this.filter = filter; + this.encoding = encoding; } /// /// Gets all scripts that should be executed. /// - public IEnumerable GetScripts(IConnectionManager connectionManager) - { + public IEnumerable GetScripts(IConnectionManager connectionManager) + { var files = Directory.GetFiles(directoryPath, "*.sql").AsEnumerable(); if (this.filter != null) { files = files.Where(filter); } - return files.Select(SqlScript.FromFile).ToList(); + return files.Select(x => SqlScript.FromFile(x, encoding)).ToList(); }