Skip to content

Commit

Permalink
Feature #150: Support equally named scripts in subfolders (#151)
Browse files Browse the repository at this point in the history
* Rewrote a bit to make sure files are sorted on folder name if subfolders are used in up, sprocs, etc folders
  • Loading branch information
erikbra authored Dec 17, 2021
1 parent da1b8c8 commit 9a9d329
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 60 deletions.
57 changes: 57 additions & 0 deletions grate.unittests/Basic/Infrastructure/FileSystem_.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.IO;
using System.Linq;
using FluentAssertions;
using grate.Configuration;
using grate.Migration;
using grate.unittests.TestInfrastructure;
using NUnit.Framework;

namespace grate.unittests.Basic.Infrastructure;

[TestFixture]
[Category("Basic")]
public class FileSystem_
{
[Test]
public void Sorts_enumerated_files_on_filename_when_no_subfolders()
{
var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory());

var path = knownFolders.Up!.Path;

var folder1 = new DirectoryInfo(path.ToString());

string filename1 = "01_any_filename.sql";
string filename2 = "02_any_filename.sql";

TestConfig.WriteContent(folder1, filename1, "Whatever");
TestConfig.WriteContent(folder1, filename2, "Whatever");

var files = FileSystem.GetFiles(path, "*.sql").ToList();

files.First().FullName.Should().Be(Path.Combine(folder1.ToString(), filename1));
files.Last().FullName.Should().Be(Path.Combine(folder1.ToString(), filename2));
}

[Test]
public void Sorts_enumerated_files_on_sub_path_when_subfolders_are_used()
{
var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory());

var path = knownFolders.Up!.Path;

var folder1 = new DirectoryInfo(Path.Combine(path.ToString(), "01_sub", "folder", "long", "way"));
var folder2 = new DirectoryInfo(Path.Combine(path.ToString(), "02_sub", "folder", "long", "way"));

string filename1 = "01_any_filename.sql";
string filename2 = "02_any_filename.sql";

TestConfig.WriteContent(folder1, filename2, "Whatever");
TestConfig.WriteContent(folder2, filename1, "Whatever");

var files = FileSystem.GetFiles(path, "*.sql").ToList();

files.First().FullName.Should().Be(Path.Combine(folder1.ToString(), filename2));
files.Last().FullName.Should().Be(Path.Combine(folder2.ToString(), filename1));
}
}
10 changes: 6 additions & 4 deletions grate.unittests/Generic/GenericMigrationTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public async Task Inserts_version_in_version_table()
versions.Should().HaveCount(1);
versions.FirstOrDefault().Should().Be("a.b.c.d");
}

private static void CreateInvalidSql(MigrationsFolder? folder)
{
var dummySql = "SELECT TOP";
Expand All @@ -135,15 +135,17 @@ private static void WriteSql(DirectoryInfo path, string filename, string? sql)
}

private static DirectoryInfo MakeSurePathExists(MigrationsFolder? folder)
=> MakeSurePathExists(folder?.Path);

protected static DirectoryInfo MakeSurePathExists(DirectoryInfo? path)
{
var path = folder?.Path ?? throw new ArgumentException(nameof(folder.Path));

ArgumentNullException.ThrowIfNull(path);
if (!path.Exists)
{
path.Create();
}

return path;
}


}
Original file line number Diff line number Diff line change
@@ -1,59 +1,35 @@
using System;
using System.IO;
using System.IO;
using grate.Configuration;
using grate.unittests.TestInfrastructure;

namespace grate.unittests.Generic.Running_MigrationScripts;

public abstract class MigrationsScriptsBase
{
protected static DirectoryInfo CreateRandomTempDirectory()
{

// I keep seeing the same temp di name in repeat test runs, and the dir has leftover scripts sitting in it.
// Trying to get a clean folder each time we ask for it

var dummyFile = Path.GetRandomFileName();
File.Delete(dummyFile);

var scriptsDir = Directory.CreateDirectory(dummyFile);
return scriptsDir;
}
protected static DirectoryInfo CreateRandomTempDirectory() => TestConfig.CreateRandomTempDirectory();

protected void CreateDummySql(MigrationsFolder? folder, string filename = "1_jalla.sql")
=> CreateDummySql(folder?.Path, filename);

protected void WriteSomeOtherSql(MigrationsFolder? folder, string filename = "1_jalla.sql")
=> WriteSomeOtherSql(folder?.Path, filename);

protected void CreateDummySql(DirectoryInfo? path, string filename = "1_jalla.sql")
{
var dummySql = Context.Sql.SelectVersion;
var path = MakeSurePathExists(folder);
WriteSql(path, filename, dummySql);
}

protected void WriteSomeOtherSql(MigrationsFolder? folder, string filename = "1_jalla.sql")
protected void WriteSomeOtherSql(DirectoryInfo? path, string filename = "1_jalla.sql")
{
var dummySql = Context.Syntax.CurrentDatabase;
var path = MakeSurePathExists(folder);
WriteSql(path, filename, dummySql);
}

protected static void WriteSql(DirectoryInfo path, string filename, string? sql)
{
if (!path.Exists)
{
path.Create();
}
File.WriteAllText(Path.Combine(path.ToString(), filename), sql);
}
protected static void WriteSql(DirectoryInfo? path, string filename, string? sql) =>
TestConfig.WriteContent(path, filename, sql);

protected static DirectoryInfo MakeSurePathExists(MigrationsFolder? folder)
{
var path = folder?.Path ?? throw new ArgumentException(nameof(folder.Path));

if (!path.Exists)
{
path.Create();
}

return path;
}
protected static DirectoryInfo MakeSurePathExists(MigrationsFolder? folder) => TestConfig.MakeSurePathExists(folder?.Path);

protected abstract IGrateTestContext Context { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
Expand Down
126 changes: 126 additions & 0 deletions grate.unittests/Generic/Running_MigrationScripts/ScriptsRun_Table.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
using FluentAssertions;
using grate.Configuration;
using grate.Migration;
using grate.unittests.TestInfrastructure;
using NUnit.Framework;

namespace grate.unittests.Generic.Running_MigrationScripts;

[TestFixture]
public abstract class ScriptsRun_Table : MigrationsScriptsBase
{
[Test()]
public async Task Includes_the_folder_name_in_the_script_name_if_subfolders()
{
var db = TestConfig.RandomDatabase();

var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory());
GrateMigrator? migrator;

var folder = new DirectoryInfo(Path.Combine(knownFolders.Up!.Path.ToString(), "sub", "folder", "long", "way"));

string filename = "any_filename.sql";

CreateDummySql(folder, filename);
await using (migrator = Context.GetMigrator(db, knownFolders))
{
await migrator.Migrate();
}

string[] scripts;
string sql = $"SELECT script_name FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}";

await using (var conn = Context.CreateDbConnection(db))
{
scripts = (await conn.QueryAsync<string>(sql)).ToArray();
}

var expectedName = $"sub/folder/long/way/{filename}";
scripts.First().Should().Be(expectedName);
}

[Test()]
public async Task Does_not_include_the_folder_name_in_the_script_name_if_no_subfolders()
{
var db = TestConfig.RandomDatabase();

var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory());
GrateMigrator? migrator;

string filename = "any_filename.sql";

CreateDummySql(knownFolders.Up, filename);

await using (migrator = Context.GetMigrator(db, knownFolders))
{
await migrator.Migrate();
}

string[] scripts;
string sql = $"SELECT script_name FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}";

await using (var conn = Context.CreateDbConnection(db))
{
scripts = (await conn.QueryAsync<string>(sql)).ToArray();
}

scripts.First().Should().Be(filename);
}

// ReSharper disable InconsistentNaming
// ReSharper disable once ClassNeverInstantiated.Local
record Result(string script_name, string text_of_script);
// ReSharper restore InconsistentNaming

[Test()]
public async Task Does_not_overwrite_scripts_from_different_folders_with_last_content()
{
var db = TestConfig.RandomDatabase();

var knownFolders = KnownFolders.In(TestConfig.CreateRandomTempDirectory());
GrateMigrator? migrator;

string filename = "any_filename.sql";
var folder1 = new DirectoryInfo(Path.Combine(knownFolders.Up!.Path.ToString(), "dub", "folder", "long", "way"));
var folder2 = new DirectoryInfo(Path.Combine(knownFolders.Up!.Path.ToString(), "sub", "dolder", "gong", "way"));

CreateDummySql(folder1, filename);
WriteSomeOtherSql(folder2, filename);

await using (migrator = Context.GetMigrator(db, knownFolders))
{
await migrator.Migrate();
}


Result[] scripts;
string sql = $"SELECT script_name, text_of_script FROM {Context.Syntax.TableWithSchema("grate", "ScriptsRun")}";

await using (var conn = Context.CreateDbConnection(db))
{
scripts = (await conn.QueryAsync<Result>(sql)).ToArray();
}

Assert.Multiple(() =>
{
scripts.Should().HaveCount(2);
var first = scripts.First();
var second = scripts.Last();
first.script_name.Should().Be($"dub/folder/long/way/{filename}");
first.text_of_script.Should().Be(Context.Sql.SelectVersion);
second.script_name.Should().Be($"sub/dolder/gong/way/{filename}");
second.text_of_script.Should().Be(Context.Syntax.CurrentDatabase);
});



}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using grate.unittests.TestInfrastructure;

namespace grate.unittests.MariaDB.Running_MigrationScripts;

public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table
{
protected override IGrateTestContext Context => GrateTestContext.MariaDB;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using grate.unittests.TestInfrastructure;

namespace grate.unittests.Oracle.Running_MigrationScripts;

public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table
{
protected override IGrateTestContext Context => GrateTestContext.Oracle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using grate.unittests.TestInfrastructure;

namespace grate.unittests.PostgreSQL.Running_MigrationScripts;

public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table
{
protected override IGrateTestContext Context => GrateTestContext.PostgreSql;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using grate.unittests.TestInfrastructure;

namespace grate.unittests.SqLite.Running_MigrationScripts;

public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table
{
protected override IGrateTestContext Context => GrateTestContext.Sqlite;
}
25 changes: 23 additions & 2 deletions grate.unittests/SqLite/SetupTestEnvironment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using grate.unittests.TestInfrastructure;
using Microsoft.Data.Sqlite;
using NUnit.Framework;
Expand All @@ -22,8 +24,27 @@ public void RunBeforeAnyTests()
Logger.LogDebug($"Before tests. Deleting old DB files.");
foreach (var dbFile in dbFiles)
{
Logger.LogDebug("File: {DbFile}", dbFile);
File.Delete(dbFile);
TryDeletingFile(dbFile);
}
}

private static void TryDeletingFile(string dbFile)
{
var i = 0;
var sleepTime = 300;
const int maxTries = 5;
while (i++ < maxTries)
{
try
{
Logger.LogDebug("File: {DbFile}", dbFile);
File.Delete(dbFile);
return;
}
catch (IOException) when (i <= maxTries)
{
Thread.Sleep(sleepTime);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using grate.unittests.TestInfrastructure;

namespace grate.unittests.SqlServer.Running_MigrationScripts;

public class ScriptsRun_Table: Generic.Running_MigrationScripts.ScriptsRun_Table
{
protected override IGrateTestContext Context => GrateTestContext.SqlServer;
}
Loading

0 comments on commit 9a9d329

Please sign in to comment.