Skip to content

Commit

Permalink
Merge pull request #12 from drwatson1/journalTo
Browse files Browse the repository at this point in the history
Add a 'journalTo' option
  • Loading branch information
drwatson1 authored Oct 3, 2021
2 parents f822936 + 5b9fdee commit f9cb15b
Show file tree
Hide file tree
Showing 22 changed files with 206 additions and 42 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ The tool has almost all the features the DbUp has, but without a single line of
* [Variables in the Scripts](https://github.com/drwatson1/dbup-cli/wiki/Home#variables-in-the-scripts)
* [Environment Variables](https://github.com/drwatson1/dbup-cli/wiki/Home#environment-variables)
* [Using .env Files](https://github.com/drwatson1/dbup-cli/wiki/Home#using-env-files)
* [Custom journal table name](https://github.com/drwatson1/dbup-cli/wiki/Home#custom-journal-table-name)
* [Command Line Options Reference](https://github.com/drwatson1/dbup-cli/wiki/Command-Line-Options)
* [Original DbUp Documentation](https://dbup.readthedocs.io/en/latest/)

Expand All @@ -56,8 +57,9 @@ The tool has almost all the features the DbUp has, but without a single line of

|Date|Version|Description|
|-|-|-|
|2020-03-28|1.5.0|Add support of .Net Core 3.1 and .Net 5.0
|2020-03-27|1.4.0|Add script naming options<BR>Load .env.local after .env
|2021-10-03|1.6.0|Add a 'journalTo' option to dbup.yml
|2021-03-28|1.5.0|Add support of .Net Core 3.1 and .Net 5.0
|2021-03-27|1.4.0|Add script naming options<BR>Load .env.local after .env
|2020-05-30|1.3.0|Support of MySQL, improve stability of integration tests
|2020-03-20|1.2.0|Add a connectionTimeoutSec option
|2019-08-27|1.1.2|Minor fixes
Expand Down
30 changes: 25 additions & 5 deletions src/dbup-cli.integration-tests/MySqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace DbUp.Cli.IntegrationTests
{
[TestClass]
public class MySqlTests: DockerBasedTest
public class MySqlTests : DockerBasedTest
{
readonly CaptureLogsLogger Logger;
readonly IEnvironment Env;
Expand All @@ -30,12 +30,14 @@ public MySqlTests()
Environment.SetEnvironmentVariable("CONNSTR", $"Server=127.0.0.1;Database={DbName};Uid=root;Pwd={Pwd};");
}

string GetBasePath(string subPath = "EmptyScript") =>
Path.Combine(Assembly.GetExecutingAssembly().Location, $@"..\Scripts\MySQL\{subPath}");
string GetBasePath(string subPath = "EmptyScript")
=> Path.Combine(Assembly.GetExecutingAssembly().Location, $@"..\Scripts\MySQL\{subPath}");

string GetConfigPath(string name = "dbup.yml", string subPath = "EmptyScript") => new DirectoryInfo(Path.Combine(GetBasePath(subPath), name)).FullName;
string GetConfigPath(string name = "dbup.yml", string subPath = "EmptyScript")
=> new DirectoryInfo(Path.Combine(GetBasePath(subPath), name)).FullName;

Func<DbConnection> CreateConnection = () => new MySqlConnection($"Server=127.0.0.1;Uid=root;Pwd={Pwd};");
Func<DbConnection> CreateConnection = ()
=> new MySqlConnection($"Server=127.0.0.1;Uid=root;Pwd={Pwd};");

[TestInitialize]
public async Task TestInitialize()
Expand Down Expand Up @@ -118,5 +120,23 @@ public void UpgradeCommand_ShouldUseConnectionTimeoutForLongrunningQueries()
var r = engine.Run("upgrade", "--ensure", GetConfigPath("dbup.yml", "Timeout"));
r.Should().Be(1);
}

[TestMethod]
public void UpgradeCommand_ShouldUseASpecifiedJournal()
{
var engine = new ToolEngine(Env, Logger);

var result = engine.Run("upgrade", "--ensure", GetConfigPath("dbup.yml", "JournalTableScript"));
result.Should().Be(0);

using (var connection = new MySqlConnection(Environment.GetEnvironmentVariable("CONNSTR")))
using (var command = new MySqlCommand("select count(*) from DbUp.testTable where scriptname = '001.sql'", connection))
{
connection.Open();
var count = command.ExecuteScalar();

count.Should().Be(1);
}
}
}
}
19 changes: 19 additions & 0 deletions src/dbup-cli.integration-tests/PostgreSqlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,24 @@ public void UpgradeCommand_ShouldUseConnectionTimeoutForLongrunningQueries()
r.Should().Be(1);
}


[TestMethod]
public void UpgradeCommand_ShouldUseASpecifiedJournal()
{
var engine = new ToolEngine(Env, Logger);

var result = engine.Run("upgrade", "--ensure", GetConfigPath("dbup.yml", "JournalTableScript"));
result.Should().Be(0);

using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR")))
using (var command = new NpgsqlCommand("select count(*) from public.\"testTable\" where scriptname = '001.sql'", connection))
{
connection.Open();
var count = command.ExecuteScalar();

count.Should().Be(1);
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dbUp:
version: 1 # should be 1
provider: mysql # DB provider: sqlserver
connectionString: $CONNSTR$ # Connection string to DB. For example, "Data Source=(localdb)\dbup;Initial Catalog=MyDb;Integrated Security=True" for sqlserver
journalTo:
schema: "DbUp"
table: "testTable"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dbUp:
version: 1 # should be 1
provider: postgresql # DB provider: sqlserver
connectionString: $CONNSTR$ # Connection string to DB. For example, "Data Source=(localdb)\dbup;Initial Catalog=MyDb;Integrated Security=True" for sqlserver
journalTo:
schema: "public"
table: "testTable"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dbUp:
version: 1 # should be 1
provider: sqlserver # DB provider: sqlserver
connectionString: $CONNSTR$ # Connection string to DB. For example, "Data Source=(localdb)\dbup;Initial Catalog=MyDb;Integrated Security=True" for sqlserver
journalTo:
schema: "dbo"
table: "testTable"
30 changes: 25 additions & 5 deletions src/dbup-cli.integration-tests/SqlServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace DbUp.Cli.IntegrationTests
{
[TestClass]
public class SqlServerTests: DockerBasedTest
public class SqlServerTests : DockerBasedTest
{
readonly CaptureLogsLogger Logger;
readonly IEnvironment Env;
Expand All @@ -25,12 +25,14 @@ public SqlServerTests()
Environment.SetEnvironmentVariable("CONNSTR", "Data Source=127.0.0.1;Initial Catalog=DbUp;Persist Security Info=True;User ID=sa;Password=SaPwd2017");
}

string GetBasePath(string subPath = "EmptyScript") =>
Path.Combine(Assembly.GetExecutingAssembly().Location, $@"..\Scripts\SqlServer\{subPath}");
string GetBasePath(string subPath = "EmptyScript")
=> Path.Combine(Assembly.GetExecutingAssembly().Location, $@"..\Scripts\SqlServer\{subPath}");

string GetConfigPath(string name = "dbup.yml", string subPath = "EmptyScript") => new DirectoryInfo(Path.Combine(GetBasePath(subPath), name)).FullName;
string GetConfigPath(string name = "dbup.yml", string subPath = "EmptyScript")
=> new DirectoryInfo(Path.Combine(GetBasePath(subPath), name)).FullName;

Func<DbConnection> CreateConnection = () => new SqlConnection("Data Source=127.0.0.1;Persist Security Info=True;User ID=sa;Password=SaPwd2017");
Func<DbConnection> CreateConnection = ()
=> new SqlConnection("Data Source=127.0.0.1;Persist Security Info=True;User ID=sa;Password=SaPwd2017");

[TestInitialize]
public async Task TestInitialize()
Expand Down Expand Up @@ -111,5 +113,23 @@ public void UpgradeCommand_ShouldUseConnectionTimeoutForLongrunningQueries()
var r = engine.Run("upgrade", "--ensure", GetConfigPath("dbup.yml", "Timeout"));
r.Should().Be(1);
}

[TestMethod]
public void UpgradeCommand_ShouldUseASpecifiedJournal()
{
var engine = new ToolEngine(Env, Logger);

var result = engine.Run("upgrade", "--ensure", GetConfigPath("dbup.yml", "JournalTableScript"));
result.Should().Be(0);

using (var connection = new SqlConnection(Environment.GetEnvironmentVariable("CONNSTR")))
using (var command = new SqlCommand("select count(*) from dbo.testTable where scriptname = '001.sql'", connection))
{
connection.Open();
var count = command.ExecuteScalar();

count.Should().Be(1);
}
}
}
}
18 changes: 18 additions & 0 deletions src/dbup-cli.integration-tests/dbup-cli.integration-tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
</ItemGroup>

<ItemGroup>
<None Update="Scripts\MySql\JournalTableScript\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\MySql\JournalTableScript\dbup.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\PostgreSql\EmptyScript\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand All @@ -36,6 +42,12 @@
<None Update="Scripts\PostgreSql\EmptyScript\dbup.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\PostgreSql\JournalTableScript\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\PostgreSql\JournalTableScript\dbup.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\PostgreSql\Timeout\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand All @@ -60,6 +72,12 @@
<None Update="Scripts\SqlServer\EmptyScript\dbup.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\SqlServer\JournalTableScript\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\SqlServer\JournalTableScript\dbup.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\SqlServer\Timeout\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
30 changes: 27 additions & 3 deletions src/dbup-cli.tests/ConfigLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ public void LoadMigration_ShouldRespectScriptEncoding()
{
var env = A.Fake<IEnvironment>();
A.CallTo(() => env.GetCurrentDirectory()).Returns(@"c:\test");
A.CallTo(() => env.FileExists("")).WithAnyArguments().ReturnsLazily(x =>
{
A.CallTo(() => env.FileExists("")).WithAnyArguments().ReturnsLazily(x =>
{
var res = File.Exists(x.Arguments[0] as string);
return res;
return res;
});

var engine = new ToolEngine(env, Logger, (testConnectionFactory as IConnectionFactory).Some());
Expand All @@ -250,5 +250,29 @@ public void LoadMigration_ShouldSetValidNamingOptions()
x.Naming.Prefix.Should().Be("scriptpreffix");
});
}

[TestMethod]
public void LoadMigration_ShouldSetValidJournalToOptions()
{
var migration = ConfigLoader.LoadMigration(GetConfigPath("journalTo.yml").Some<string, Error>());

migration.MatchSome(x =>
{
x.JournalTo.Should().NotBeNull();
x.JournalTo.Schema.Should().Be("test-schema");
x.JournalTo.Table.Should().Be("test-table");
});
}

[TestMethod]
public void LoadMigration_ShouldSetValidJournalToNull()
{
var migration = ConfigLoader.LoadMigration(GetConfigPath("journalTo-null.yml").Some<string, Error>());

migration.MatchSome(x =>
{
x.JournalTo.Should().BeNull();
});
}
}
}
6 changes: 3 additions & 3 deletions src/dbup-cli.tests/ConfigurationHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void PerformUpgrade_ShouldUseCustomVersionsTable_IfCustomJournalIsPassed(
{
upgradeEngineBuilder.Some<UpgradeEngineBuilder, Error>()
.SelectJournal(
new Journal("test_scheme", "test_SchemaVersion").Some()
Provider.SqlServer, new Journal("test_scheme", "test_SchemaVersion")
);

upgradeEngineBuilder.Build().PerformUpgrade();
Expand All @@ -80,7 +80,7 @@ public void PerformUpgrade_ShouldUseCustomVersionsTable_IfCustomJournalIsPassed(
public void PerformUpgrade_ShouldUseDefaultVersionsTable_IfDefaultJournalIsPassed()
{
upgradeEngineBuilder.Some<UpgradeEngineBuilder, Error>()
.SelectJournal(Journal.Default.Some());
.SelectJournal(Provider.SqlServer, Journal.Default);

upgradeEngineBuilder.Build().PerformUpgrade();

Expand All @@ -91,7 +91,7 @@ public void PerformUpgrade_ShouldUseDefaultVersionsTable_IfDefaultJournalIsPasse
public void SelectJournal_ShouldSelectNullJournal_IfNoneValueIsPassed()
{
upgradeEngineBuilder.Some<UpgradeEngineBuilder, Error>()
.SelectJournal(Option.None<Journal>());
.SelectJournal(Provider.SqlServer, null);

upgradeEngineBuilder.Build().PerformUpgrade();
logger.InfoMessages.Should().NotContain(x => x.StartsWith("Creating the ", StringComparison.Ordinal));
Expand Down
5 changes: 5 additions & 0 deletions src/dbup-cli.tests/Scripts/Config/journalTo-null.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dbUp:
version: 1
provider: sqlserver
connectionString: (localdb)\dbup;Initial Catalog=DbUpTest;Integrated Security=True
journalTo: null
7 changes: 7 additions & 0 deletions src/dbup-cli.tests/Scripts/Config/journalTo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dbUp:
version: 1
provider: sqlserver
connectionString: (localdb)\dbup;Initial Catalog=DbUpTest;Integrated Security=True
journalTo:
schema: "test-schema"
table: "test-table"
6 changes: 6 additions & 0 deletions src/dbup-cli.tests/dbup-cli.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
<None Update="Scripts\Config\DotEnv-VarsOverride\CurrentFolder\.env">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\Config\journalTo-null.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\Config\journalTo.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\Config\mark-as-executed.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
9 changes: 2 additions & 7 deletions src/dbup-cli/ConfigFile/Journal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,15 @@ public class Journal
public string Table { get; private set; }

public static Journal Default => new Journal();
public static bool IsDefault(Journal journal)
{
return journal.Schema == null && journal.Table == null;
}

public static readonly Option<Journal> None = Option.None<Journal>();
public bool IsDefault => Schema == null && Table == null;

public Journal(string schema, string table)
{
Schema = schema;
Table = table;
}

private Journal()
public Journal()
{
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/dbup-cli/ConfigFile/Migration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Migration
public string ConnectionString { get; private set; }
public int ConnectionTimeoutSec { get; private set; } = 30;
public Transaction Transaction { get; private set; } = Transaction.None;
public Option<Journal> JournalTo { get; private set; } = Journal.Default.Some();
public Journal JournalTo { get; private set; } = Journal.Default;
public NamingOptions Naming { get; private set; } = NamingOptions.Default;
public List<ScriptBatch> Scripts { get; set; } = new List<ScriptBatch>();

Expand All @@ -21,7 +21,7 @@ internal void ExpandVariables()
{
ConnectionString = StringUtils.ExpandEnvironmentVariables(ConnectionString ?? "");
Scripts.ForEach(x => x.Folder = StringUtils.ExpandEnvironmentVariables(x.Folder ?? ""));

var dic = new Dictionary<string, string>();
foreach (var item in Vars)
{
Expand Down
Loading

0 comments on commit f9cb15b

Please sign in to comment.