Skip to content

Commit

Permalink
Merge pull request #312 from DbUp/bug-fileProviderSubfolders
Browse files Browse the repository at this point in the history
Added the subfolder name to script names returned from FileSystemScriptProvider
  • Loading branch information
droyad committed Feb 17, 2018
2 parents 8da8cea + a1f9f9f commit 9480615
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
27 changes: 23 additions & 4 deletions src/dbup-core/Engine/SqlScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,31 @@ public static SqlScript FromFile(string path)
/// <param name="encoding"></param>
/// <returns></returns>
public static SqlScript FromFile(string path, Encoding encoding)
=> FromFile(Path.GetDirectoryName(path), path, encoding);

/// <summary>
/// Create a SqlScript from a file using specified encoding
/// </summary>
/// <param name="basePath">Root path that was searched</param>
/// <param name="path">Path to the file</param>
/// <param name="encoding"></param>
/// <returns></returns>
public static SqlScript FromFile(string basePath, string path, Encoding encoding)
{
var fullPath = Path.GetFullPath(path);
var fullBasePath = Path.GetFullPath(basePath);

if(!fullPath.StartsWith(fullBasePath, StringComparison.OrdinalIgnoreCase))
throw new Exception("The basePath must be a parent of path");

var filename = fullPath
.Substring(fullBasePath.Length)
.Replace(Path.DirectorySeparatorChar, '.')
.Replace(Path.AltDirectorySeparatorChar, '.')
.Trim('.');

using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var fileName = new FileInfo(path).Name;
return FromStream(fileName, fileStream, encoding);
}
return FromStream(filename, fileStream, encoding);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/dbup-core/ScriptProviders/FileSystemScriptProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public IEnumerable<SqlScript> GetScripts(IConnectionManager connectionManager)
{
files = files.Where(filter);
}
return files.Select(x => SqlScript.FromFile(x, encoding))
return files.Select(x => SqlScript.FromFile(directoryPath, x, encoding))
.OrderBy(x => x.Name)
.ToList();
}
Expand Down
1 change: 1 addition & 0 deletions src/dbup-tests/ApprovalFiles/dbup-core.approved.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public class SqlScript
public string Name { get; }
public static DbUp.Engine.SqlScript FromFile(string path) { }
public static DbUp.Engine.SqlScript FromFile(string path, System.Text.Encoding encoding) { }
public static DbUp.Engine.SqlScript FromFile(string basePath, string path, System.Text.Encoding encoding) { }
public static DbUp.Engine.SqlScript FromStream(string scriptName, System.IO.Stream stream) { }
public static DbUp.Engine.SqlScript FromStream(string scriptName, System.IO.Stream stream, System.Text.Encoding encoding) { }
}
Expand Down
10 changes: 9 additions & 1 deletion src/dbup-tests/ScriptProvider/FileSystemScriptProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,16 @@ public void the_file_should_contain_content()
sqlScript.Contents.Length.ShouldBeGreaterThan(0);
}
}

[Then]
public void the_files_should_contain_the_subfolder_name()
{
filesToExecute
.Select(f => f.Name)
.ShouldContain("Folder1.dbup-tests.TestScripts.Test1__9.sql");
}

[Then(Skip = "The script names should include the folder")]
[Then()]
public void the_files_should_be_correctly_ordered_with_subdirectory_order()
{
filesToExecute.ElementAt(0).Name.ShouldEndWith("Script20110301_1_Test1.sql");
Expand Down

0 comments on commit 9480615

Please sign in to comment.