From 77dfdecc681e878c19b714f7ac6f127690a8c55d Mon Sep 17 00:00:00 2001 From: Guilherme Bernal Date: Mon, 6 Jun 2022 17:20:46 -0300 Subject: [PATCH 1/6] feat: add support for CockroachDB --- src/dbup-cli/ConfigFile/Provider.cs | 3 ++- src/dbup-cli/ConfigurationHelper.cs | 12 ++++++++++++ src/dbup-cli/dbup-cli.csproj | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/dbup-cli/ConfigFile/Provider.cs b/src/dbup-cli/ConfigFile/Provider.cs index 3ad9c5c..fae01dd 100644 --- a/src/dbup-cli/ConfigFile/Provider.cs +++ b/src/dbup-cli/ConfigFile/Provider.cs @@ -6,6 +6,7 @@ public enum Provider SqlServer, PostgreSQL, MySQL, - AzureSql + AzureSql, + CockroachDB } } diff --git a/src/dbup-cli/ConfigurationHelper.cs b/src/dbup-cli/ConfigurationHelper.cs index a4afcbc..a041e7c 100644 --- a/src/dbup-cli/ConfigurationHelper.cs +++ b/src/dbup-cli/ConfigurationHelper.cs @@ -43,6 +43,10 @@ public static Option SelectDbProvider(Provider prov return DeployChanges.To.MySqlDatabase(connectionString) .WithExecutionTimeout(timeout) .Some(); + case Provider.CockroachDB: + return DeployChanges.To.CockroachDbDatabase(connectionString) + .WithExecutionTimeout(timeout) + .Some(); } return Option.None(Error.Create(Constants.ConsoleMessages.UnsupportedProvider, provider.ToString())); @@ -73,6 +77,9 @@ public static Option EnsureDb(IUpgradeLog logger, Provider provider case Provider.MySQL: EnsureDatabase.For.MySqlDatabase(connectionString, logger, connectionTimeoutSec); return true.Some(); + case Provider.CockroachDB: + EnsureDatabase.For.CockroachDbDatabase(connectionString, logger); // Cockroach provider does not support timeout... + return true.Some(); } } catch (Exception ex) @@ -106,6 +113,8 @@ public static Option DropDb(IUpgradeLog logger, Provider provider, return Option.None(Error.Create("PostgreSQL database provider does not support 'drop' command for now")); case Provider.MySQL: return Option.None(Error.Create("MySQL database provider does not support 'drop' command for now")); + case Provider.CockroachDB: + return Option.None(Error.Create("CockroachDB database provider does not support 'drop' command for now")); } } catch (Exception ex) @@ -137,6 +146,9 @@ public static Option SelectJournal(this Option(Error.Create($"JournalTo does not support a provider {provider}")); } diff --git a/src/dbup-cli/dbup-cli.csproj b/src/dbup-cli/dbup-cli.csproj index e3eabd5..f4263f8 100644 --- a/src/dbup-cli/dbup-cli.csproj +++ b/src/dbup-cli/dbup-cli.csproj @@ -62,6 +62,7 @@ + From e09f60cce86d5b19c16afa281fcfc7392070490c Mon Sep 17 00:00:00 2001 From: Guilherme Bernal Date: Tue, 7 Jun 2022 09:33:53 -0300 Subject: [PATCH 2/6] fix: use JournalToCockroachDbTable --- src/dbup-cli/ConfigurationHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dbup-cli/ConfigurationHelper.cs b/src/dbup-cli/ConfigurationHelper.cs index a041e7c..34e312f 100644 --- a/src/dbup-cli/ConfigurationHelper.cs +++ b/src/dbup-cli/ConfigurationHelper.cs @@ -147,7 +147,7 @@ public static Option SelectJournal(this Option(Error.Create($"JournalTo does not support a provider {provider}")); From bb071e1a95ad1d7bfd17ca06d11795a362af213b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A2=D1=80=D0=B5?= =?UTF-8?q?=D0=B3=D1=83=D0=B1?= Date: Sat, 11 Jun 2022 17:11:10 +0300 Subject: [PATCH 3/6] Add CockroachDb integration tests --- .../CockroachDbTests.cs | 134 ++++++++++++++++++ .../DockerBasedTest.cs | 11 +- .../Scripts/CockroachDb/EmptyScript/001.sql | 1 + .../Scripts/CockroachDb/EmptyScript/dbup.yml | 4 + .../CockroachDb/JournalTableScript/001.sql | 1 + .../CockroachDb/JournalTableScript/dbup.yml | 8 ++ .../Scripts/CockroachDb/Timeout/001.sql | 1 + .../Scripts/CockroachDb/Timeout/dbup.yml | 5 + .../dbup-cli.integration-tests.csproj | 20 ++- 9 files changed, 182 insertions(+), 3 deletions(-) create mode 100644 src/dbup-cli.integration-tests/CockroachDbTests.cs create mode 100644 src/dbup-cli.integration-tests/Scripts/CockroachDb/EmptyScript/001.sql create mode 100644 src/dbup-cli.integration-tests/Scripts/CockroachDb/EmptyScript/dbup.yml create mode 100644 src/dbup-cli.integration-tests/Scripts/CockroachDb/JournalTableScript/001.sql create mode 100644 src/dbup-cli.integration-tests/Scripts/CockroachDb/JournalTableScript/dbup.yml create mode 100644 src/dbup-cli.integration-tests/Scripts/CockroachDb/Timeout/001.sql create mode 100644 src/dbup-cli.integration-tests/Scripts/CockroachDb/Timeout/dbup.yml diff --git a/src/dbup-cli.integration-tests/CockroachDbTests.cs b/src/dbup-cli.integration-tests/CockroachDbTests.cs new file mode 100644 index 0000000..1f2123d --- /dev/null +++ b/src/dbup-cli.integration-tests/CockroachDbTests.cs @@ -0,0 +1,134 @@ +using DbUp.Cli.Tests.TestInfrastructure; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.IO; +using System.Reflection; +using FluentAssertions; +using System.Data.SqlClient; +using System.Collections.Generic; +using System.Threading.Tasks; +using Npgsql; +using System.Data.Common; + +namespace DbUp.Cli.IntegrationTests +{ + [TestClass] + public class CocroachDbTests : DockerBasedTest + { + readonly CaptureLogsLogger Logger; + readonly IEnvironment Env; + + public CocroachDbTests() + { + Env = new CliEnvironment(); + Logger = new CaptureLogsLogger(); + + Environment.SetEnvironmentVariable("CONNSTR", "Host=127.0.0.1;Port=26257;SSL Mode=Disable;Database=dbup;Username=root"); + } + + string GetBasePath(string subPath = "EmptyScript") => + Path.Combine(Assembly.GetExecutingAssembly().Location, $@"..\Scripts\CockroachDb\{subPath}"); + + string GetConfigPath(string name = "dbup.yml", string subPath = "EmptyScript") => new DirectoryInfo(Path.Combine(GetBasePath(subPath), name)).FullName; + + Func CreateConnection = () => new NpgsqlConnection("Host=127.0.0.1;Port=26257;SSL Mode=Disable;Database=defaultdb;Username=root"); + + [TestInitialize] + public Task TestInitialize() + { + /* + * Before the first run, download the image: + * docker pull cockroachdb/cockroach:v22.1.1 + * */ + return DockerInitialize( + "cockroachdb/cockroach:v22.1.1", + new List() { }, + new List() { "start-single-node", "--insecure" }, + "26257", + CreateConnection + ); + } + + [TestCleanup] + public Task TestCleanup() + { + return DockerCleanup(CreateConnection, con => new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", con as NpgsqlConnection)); + } + + [TestMethod] + public void Ensure_CreateANewDb() + { + var engine = new ToolEngine(Env, Logger); + + var result = engine.Run("upgrade", "--ensure", GetConfigPath()); + result.Should().Be(0); + + using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR"))) + using (var command = new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", connection)) + { + connection.Open(); + var count = command.ExecuteScalar(); + + count.Should().Be(1); + } + } + + /* + // Drop database does not supported for PostgreSQL by DbUp + [TestMethod] + public void Drop_DropADb() + { + var engine = new ToolEngine(Env, Logger); + + engine.Run("upgrade", "--ensure", GetConfigPath()); + var result = engine.Run("drop", GetConfigPath()); + result.Should().Be(0); + using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR"))) + using (var command = new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", connection)) + { + Action a = () => connection.Open(); + a.Should().Throw("Database DbUp should not exist"); + } + } + */ + + [TestMethod] + public void DatabaseShouldNotExistBeforeTestRun() + { + using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR"))) + using (var command = new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", connection)) + { + Action a = () => { connection.Open(); command.ExecuteScalar(); }; + a.Should().Throw("Database DbUp should not exist"); + } + } + + [TestMethod] + public void UpgradeCommand_ShouldUseConnectionTimeoutForLongrunningQueries() + { + var engine = new ToolEngine(Env, Logger); + + 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 NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR"))) + using (var command = new NpgsqlCommand("select count(*) from public.journal where scriptname = '001.sql'", connection)) + { + connection.Open(); + var count = command.ExecuteScalar(); + + count.Should().Be(1); + } + } + + } +} diff --git a/src/dbup-cli.integration-tests/DockerBasedTest.cs b/src/dbup-cli.integration-tests/DockerBasedTest.cs index 501669f..e01f440 100644 --- a/src/dbup-cli.integration-tests/DockerBasedTest.cs +++ b/src/dbup-cli.integration-tests/DockerBasedTest.cs @@ -18,7 +18,12 @@ public class DockerBasedTest DockerClient DockerClient; string ContainerId; - protected async Task DockerInitialize(string imageName, List environmentVariables, string port, Func createConnection) + protected Task DockerInitialize(string imageName, List environmentVariables, string port, Func createConnection) + { + return DockerInitialize(imageName, environmentVariables, new List(), port, createConnection); + } + + protected async Task DockerInitialize(string imageName, List environmentVariables, List cmd, string port, Func createConnection) { DockerClient = new DockerClientConfiguration(new Uri(DockerEngineUri)).CreateClient(); var pars = new CreateContainerParameters(new Config() @@ -29,7 +34,8 @@ protected async Task DockerInitialize(string imageName, List environment { port, new EmptyStruct() } }, Env = environmentVariables, - NetworkDisabled = false + NetworkDisabled = false, + Cmd = cmd }); pars.HostConfig = new HostConfig() @@ -101,6 +107,7 @@ protected async Task DockerCleanup(Func createConnection, Func - + @@ -30,6 +30,24 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + PreserveNewest From dac7d6342f0d46f7661a88f0eb1fa7666d50425f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A2=D1=80=D0=B5?= =?UTF-8?q?=D0=B3=D1=83=D0=B1?= Date: Sat, 11 Jun 2022 17:33:16 +0300 Subject: [PATCH 4/6] Bump version --- src/dbup-cli/dbup-cli.csproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dbup-cli/dbup-cli.csproj b/src/dbup-cli/dbup-cli.csproj index f4263f8..803d410 100644 --- a/src/dbup-cli/dbup-cli.csproj +++ b/src/dbup-cli/dbup-cli.csproj @@ -18,17 +18,17 @@ latest DbUp Command Line Interface Sergey Tregub - 1.6.6 + 1.7.0 https://github.com/drwatson1/dbup-cli Copyright (c) 2022 Sergey Tregub https://github.com/drwatson1/dbup-cli GitHub - dbup database migration sqlserver postgresql mysql + dbup database migration sqlserver postgresql mysql cockroachdb true dbup ./nupkg - Add support of .Net 6 + CockroachDB support, thanks to @lbguilherme DbUp Command Line Interface Command line tool, that can be installed as a .Net global tool, that helps you to deploy changes to databases. It tracks which SQL scripts have been run already, and runs the change scripts that are needed to get your database up to date. From 9475c3a66c5f4227bbf3635e02faea05d1b3ca89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A2=D1=80=D0=B5?= =?UTF-8?q?=D0=B3=D1=83=D0=B1?= Date: Sat, 11 Jun 2022 17:33:49 +0300 Subject: [PATCH 5/6] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8a86cd6..68d953d 100644 --- a/README.md +++ b/README.md @@ -54,11 +54,13 @@ The tool has almost all the features the DbUp has, but without a single line of * AzureSQL * PostgreSQL * MySQL +* CockroachDB (by @lbguilherme) ## Release Notes |Date|Version|Description| |-|-|-| +|2022-06-11|1.7.0|Add support of CockroachDB, thanks to @lbguilherme |2022-05-10|1.6.6|Add support of .Net 6 |2022-02-14|1.6.5|Support of DisableVars |2022-02-06|1.6.4|Support of drop and ensure for Azure SQL From 62669ea7828e8796b4fdc2115c9ac08fe06f8fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D1=80=D0=B3=D0=B5=D0=B9=20=D0=A2=D1=80=D0=B5?= =?UTF-8?q?=D0=B3=D1=83=D0=B1?= Date: Sat, 11 Jun 2022 17:34:15 +0300 Subject: [PATCH 6/6] Add a new dll to ILMerge --- build/PackDbUp.cmd | 1 + 1 file changed, 1 insertion(+) diff --git a/build/PackDbUp.cmd b/build/PackDbUp.cmd index 3c5f175..33e1583 100644 --- a/build/PackDbUp.cmd +++ b/build/PackDbUp.cmd @@ -8,6 +8,7 @@ ILMerge.exe %BINDIR%\dbup-cli.exe /ndebug /out:dbup-cli.exe ^ %BINDIR%\dbup-mysql.dll ^ %BINDIR%\dbup-postgresql.dll ^ %BINDIR%\dbup-sqlserver.dll ^ +%BINDIR%\dbup-cockroachdb.dll ^ %BINDIR%\DotNetEnv.dll ^ %BINDIR%\Microsoft.Azure.Services.AppAuthentication.dll ^ %BINDIR%\Microsoft.IdentityModel.Clients.ActiveDirectory.dll ^