Skip to content

Commit

Permalink
Merge pull request #27 from drwatson1/feature/cockroachdb
Browse files Browse the repository at this point in the history
Add  CockroachDB support
  • Loading branch information
drwatson1 authored Jun 11, 2022
2 parents 29b2a75 + 62669ea commit d0d25d9
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions build/PackDbUp.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -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 ^
Expand Down
134 changes: 134 additions & 0 deletions src/dbup-cli.integration-tests/CockroachDbTests.cs
Original file line number Diff line number Diff line change
@@ -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<DbConnection> 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<string>() { },
new List<string>() { "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<SqlException>("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<Exception>("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);
}
}

}
}
11 changes: 9 additions & 2 deletions src/dbup-cli.integration-tests/DockerBasedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public class DockerBasedTest
DockerClient DockerClient;
string ContainerId;

protected async Task DockerInitialize(string imageName, List<string> environmentVariables, string port, Func<DbConnection> createConnection)
protected Task DockerInitialize(string imageName, List<string> environmentVariables, string port, Func<DbConnection> createConnection)
{
return DockerInitialize(imageName, environmentVariables, new List<string>(), port, createConnection);
}

protected async Task DockerInitialize(string imageName, List<string> environmentVariables, List<string> cmd, string port, Func<DbConnection> createConnection)
{
DockerClient = new DockerClientConfiguration(new Uri(DockerEngineUri)).CreateClient();
var pars = new CreateContainerParameters(new Config()
Expand All @@ -29,7 +34,8 @@ protected async Task DockerInitialize(string imageName, List<string> environment
{ port, new EmptyStruct() }
},
Env = environmentVariables,
NetworkDisabled = false
NetworkDisabled = false,
Cmd = cmd
});

pars.HostConfig = new HostConfig()
Expand Down Expand Up @@ -101,6 +107,7 @@ protected async Task DockerCleanup(Func<DbConnection> createConnection, Func<DbC
}
catch
{
await Task.Delay(1000);
return;
}
}
Expand Down
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,4 @@
dbUp:
version: 1 # should be 1
provider: CockroachDb # DB provider: sqlserver
connectionString: $CONNSTR$ # Connection string to DB. For example, "Data Source=(localdb)\dbup;Initial Catalog=MyDb;Integrated Security=True" for sqlserver
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,8 @@
dbUp:
version: 1 # should be 1
provider: CockroachDb # DB provider: sqlserver
connectionString: $CONNSTR$ # Connection string to DB. For example, "Data Source=(localdb)\dbup;Initial Catalog=MyDb;Integrated Security=True" for sqlserver
connectionTimeoutSec: 60
journalTo:
schema: public
table: journal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT pg_sleep(60);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dbUp:
version: 1 # should be 1
provider: CockroachDb # DB provider: sqlserver
connectionString: $CONNSTR$ # Connection string to DB. For example, "Data Source=(localdb)\dbup;Initial Catalog=MyDb;Integrated Security=True" for sqlserver
connectionTimeoutSec: 10 # Connection timeout in seconds
20 changes: 19 additions & 1 deletion src/dbup-cli.integration-tests/dbup-cli.integration-tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Docker.DotNet" Version="3.125.2" />
<PackageReference Include="Docker.DotNet" Version="3.125.5" />
<PackageReference Include="FakeItEasy" Version="5.1.0" />
<PackageReference Include="FluentAssertions" Version="5.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
Expand All @@ -30,6 +30,24 @@
<None Update="Scripts\MySql\JournalTableScript\dbup.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\CockroachDb\EmptyScript\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\CockroachDb\EmptyScript\dbup.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\CockroachDb\JournalTableScript\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\CockroachDb\JournalTableScript\dbup.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\CockroachDb\Timeout\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\CockroachDb\Timeout\dbup.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\PostgreSql\EmptyScript\001.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
3 changes: 2 additions & 1 deletion src/dbup-cli/ConfigFile/Provider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public enum Provider
SqlServer,
PostgreSQL,
MySQL,
AzureSql
AzureSql,
CockroachDB
}
}
12 changes: 12 additions & 0 deletions src/dbup-cli/ConfigurationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public static Option<UpgradeEngineBuilder, Error> SelectDbProvider(Provider prov
return DeployChanges.To.MySqlDatabase(connectionString)
.WithExecutionTimeout(timeout)
.Some<UpgradeEngineBuilder, Error>();
case Provider.CockroachDB:
return DeployChanges.To.CockroachDbDatabase(connectionString)
.WithExecutionTimeout(timeout)
.Some<UpgradeEngineBuilder, Error>();
}

return Option.None<UpgradeEngineBuilder, Error>(Error.Create(Constants.ConsoleMessages.UnsupportedProvider, provider.ToString()));
Expand Down Expand Up @@ -73,6 +77,9 @@ public static Option<bool, Error> EnsureDb(IUpgradeLog logger, Provider provider
case Provider.MySQL:
EnsureDatabase.For.MySqlDatabase(connectionString, logger, connectionTimeoutSec);
return true.Some<bool, Error>();
case Provider.CockroachDB:
EnsureDatabase.For.CockroachDbDatabase(connectionString, logger); // Cockroach provider does not support timeout...
return true.Some<bool, Error>();
}
}
catch (Exception ex)
Expand Down Expand Up @@ -106,6 +113,8 @@ public static Option<bool, Error> DropDb(IUpgradeLog logger, Provider provider,
return Option.None<bool, Error>(Error.Create("PostgreSQL database provider does not support 'drop' command for now"));
case Provider.MySQL:
return Option.None<bool, Error>(Error.Create("MySQL database provider does not support 'drop' command for now"));
case Provider.CockroachDB:
return Option.None<bool, Error>(Error.Create("CockroachDB database provider does not support 'drop' command for now"));
}
}
catch (Exception ex)
Expand Down Expand Up @@ -137,6 +146,9 @@ public static Option<UpgradeEngineBuilder, Error> SelectJournal(this Option<Upgr
case Provider.PostgreSQL:
builder.JournalToPostgresqlTable(journal.Schema, journal.Table);
break;
case Provider.CockroachDB:
builder.JournalToCockroachDbTable(journal.Schema, journal.Table);
break;
default:
return Option.None<UpgradeEngineBuilder, Error>(Error.Create($"JournalTo does not support a provider {provider}"));
}
Expand Down
7 changes: 4 additions & 3 deletions src/dbup-cli/dbup-cli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
<LangVersion>latest</LangVersion>
<Product>DbUp Command Line Interface</Product>
<Authors>Sergey Tregub</Authors>
<Version>1.6.6</Version>
<Version>1.7.0</Version>
<RepositoryUrl>https://github.com/drwatson1/dbup-cli</RepositoryUrl>
<Company />
<Copyright>Copyright (c) 2022 Sergey Tregub</Copyright>
<PackageProjectUrl>https://github.com/drwatson1/dbup-cli</PackageProjectUrl>
<RepositoryType>GitHub</RepositoryType>
<PackageTags>dbup database migration sqlserver postgresql mysql</PackageTags>
<PackageTags>dbup database migration sqlserver postgresql mysql cockroachdb</PackageTags>
<PackAsTool>true</PackAsTool>
<ToolCommandName>dbup</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
<PackageReleaseNotes>Add support of .Net 6</PackageReleaseNotes>
<PackageReleaseNotes>CockroachDB support, thanks to @lbguilherme</PackageReleaseNotes>
<Title>DbUp Command Line Interface</Title>
<Description>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.</Description>
<PackageLicenseUrl></PackageLicenseUrl>
Expand Down Expand Up @@ -62,6 +62,7 @@
<PackageReference Include="dbup-mysql" Version="4.5.0" />
<PackageReference Include="dbup-postgresql" Version="4.5.0" />
<PackageReference Include="dbup-sqlserver" Version="4.5.0" />
<PackageReference Include="dbup-cockroachdb" Version="1.0.4" />
<PackageReference Include="DotNetEnv" Version="2.1.1" />
<PackageReference Include="Npgsql" Version="3.2.7" />
<PackageReference Include="Optional" Version="4.0.0" />
Expand Down

0 comments on commit d0d25d9

Please sign in to comment.