Skip to content

Commit d0d25d9

Browse files
authored
Merge pull request #27 from drwatson1/feature/cockroachdb
Add CockroachDB support
2 parents 29b2a75 + 62669ea commit d0d25d9

File tree

14 files changed

+203
-7
lines changed

14 files changed

+203
-7
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ The tool has almost all the features the DbUp has, but without a single line of
5454
* AzureSQL
5555
* PostgreSQL
5656
* MySQL
57+
* CockroachDB (by @lbguilherme)
5758

5859
## Release Notes
5960

6061
|Date|Version|Description|
6162
|-|-|-|
63+
|2022-06-11|1.7.0|Add support of CockroachDB, thanks to @lbguilherme
6264
|2022-05-10|1.6.6|Add support of .Net 6
6365
|2022-02-14|1.6.5|Support of DisableVars
6466
|2022-02-06|1.6.4|Support of drop and ensure for Azure SQL

build/PackDbUp.cmd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ILMerge.exe %BINDIR%\dbup-cli.exe /ndebug /out:dbup-cli.exe ^
88
%BINDIR%\dbup-mysql.dll ^
99
%BINDIR%\dbup-postgresql.dll ^
1010
%BINDIR%\dbup-sqlserver.dll ^
11+
%BINDIR%\dbup-cockroachdb.dll ^
1112
%BINDIR%\DotNetEnv.dll ^
1213
%BINDIR%\Microsoft.Azure.Services.AppAuthentication.dll ^
1314
%BINDIR%\Microsoft.IdentityModel.Clients.ActiveDirectory.dll ^
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using DbUp.Cli.Tests.TestInfrastructure;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System;
4+
using System.IO;
5+
using System.Reflection;
6+
using FluentAssertions;
7+
using System.Data.SqlClient;
8+
using System.Collections.Generic;
9+
using System.Threading.Tasks;
10+
using Npgsql;
11+
using System.Data.Common;
12+
13+
namespace DbUp.Cli.IntegrationTests
14+
{
15+
[TestClass]
16+
public class CocroachDbTests : DockerBasedTest
17+
{
18+
readonly CaptureLogsLogger Logger;
19+
readonly IEnvironment Env;
20+
21+
public CocroachDbTests()
22+
{
23+
Env = new CliEnvironment();
24+
Logger = new CaptureLogsLogger();
25+
26+
Environment.SetEnvironmentVariable("CONNSTR", "Host=127.0.0.1;Port=26257;SSL Mode=Disable;Database=dbup;Username=root");
27+
}
28+
29+
string GetBasePath(string subPath = "EmptyScript") =>
30+
Path.Combine(Assembly.GetExecutingAssembly().Location, $@"..\Scripts\CockroachDb\{subPath}");
31+
32+
string GetConfigPath(string name = "dbup.yml", string subPath = "EmptyScript") => new DirectoryInfo(Path.Combine(GetBasePath(subPath), name)).FullName;
33+
34+
Func<DbConnection> CreateConnection = () => new NpgsqlConnection("Host=127.0.0.1;Port=26257;SSL Mode=Disable;Database=defaultdb;Username=root");
35+
36+
[TestInitialize]
37+
public Task TestInitialize()
38+
{
39+
/*
40+
* Before the first run, download the image:
41+
* docker pull cockroachdb/cockroach:v22.1.1
42+
* */
43+
return DockerInitialize(
44+
"cockroachdb/cockroach:v22.1.1",
45+
new List<string>() { },
46+
new List<string>() { "start-single-node", "--insecure" },
47+
"26257",
48+
CreateConnection
49+
);
50+
}
51+
52+
[TestCleanup]
53+
public Task TestCleanup()
54+
{
55+
return DockerCleanup(CreateConnection, con => new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", con as NpgsqlConnection));
56+
}
57+
58+
[TestMethod]
59+
public void Ensure_CreateANewDb()
60+
{
61+
var engine = new ToolEngine(Env, Logger);
62+
63+
var result = engine.Run("upgrade", "--ensure", GetConfigPath());
64+
result.Should().Be(0);
65+
66+
using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR")))
67+
using (var command = new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", connection))
68+
{
69+
connection.Open();
70+
var count = command.ExecuteScalar();
71+
72+
count.Should().Be(1);
73+
}
74+
}
75+
76+
/*
77+
// Drop database does not supported for PostgreSQL by DbUp
78+
[TestMethod]
79+
public void Drop_DropADb()
80+
{
81+
var engine = new ToolEngine(Env, Logger);
82+
83+
engine.Run("upgrade", "--ensure", GetConfigPath());
84+
var result = engine.Run("drop", GetConfigPath());
85+
result.Should().Be(0);
86+
using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR")))
87+
using (var command = new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", connection))
88+
{
89+
Action a = () => connection.Open();
90+
a.Should().Throw<SqlException>("Database DbUp should not exist");
91+
}
92+
}
93+
*/
94+
95+
[TestMethod]
96+
public void DatabaseShouldNotExistBeforeTestRun()
97+
{
98+
using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR")))
99+
using (var command = new NpgsqlCommand("select count(*) from SchemaVersions where scriptname = '001.sql'", connection))
100+
{
101+
Action a = () => { connection.Open(); command.ExecuteScalar(); };
102+
a.Should().Throw<Exception>("Database DbUp should not exist");
103+
}
104+
}
105+
106+
[TestMethod]
107+
public void UpgradeCommand_ShouldUseConnectionTimeoutForLongrunningQueries()
108+
{
109+
var engine = new ToolEngine(Env, Logger);
110+
111+
var r = engine.Run("upgrade", "--ensure", GetConfigPath("dbup.yml", "Timeout"));
112+
r.Should().Be(1);
113+
}
114+
115+
[TestMethod]
116+
public void UpgradeCommand_ShouldUseASpecifiedJournal()
117+
{
118+
var engine = new ToolEngine(Env, Logger);
119+
120+
var result = engine.Run("upgrade", "--ensure", GetConfigPath("dbup.yml", "JournalTableScript"));
121+
result.Should().Be(0);
122+
123+
using (var connection = new NpgsqlConnection(Environment.GetEnvironmentVariable("CONNSTR")))
124+
using (var command = new NpgsqlCommand("select count(*) from public.journal where scriptname = '001.sql'", connection))
125+
{
126+
connection.Open();
127+
var count = command.ExecuteScalar();
128+
129+
count.Should().Be(1);
130+
}
131+
}
132+
133+
}
134+
}

src/dbup-cli.integration-tests/DockerBasedTest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ public class DockerBasedTest
1818
DockerClient DockerClient;
1919
string ContainerId;
2020

21-
protected async Task DockerInitialize(string imageName, List<string> environmentVariables, string port, Func<DbConnection> createConnection)
21+
protected Task DockerInitialize(string imageName, List<string> environmentVariables, string port, Func<DbConnection> createConnection)
22+
{
23+
return DockerInitialize(imageName, environmentVariables, new List<string>(), port, createConnection);
24+
}
25+
26+
protected async Task DockerInitialize(string imageName, List<string> environmentVariables, List<string> cmd, string port, Func<DbConnection> createConnection)
2227
{
2328
DockerClient = new DockerClientConfiguration(new Uri(DockerEngineUri)).CreateClient();
2429
var pars = new CreateContainerParameters(new Config()
@@ -29,7 +34,8 @@ protected async Task DockerInitialize(string imageName, List<string> environment
2934
{ port, new EmptyStruct() }
3035
},
3136
Env = environmentVariables,
32-
NetworkDisabled = false
37+
NetworkDisabled = false,
38+
Cmd = cmd
3339
});
3440

3541
pars.HostConfig = new HostConfig()
@@ -101,6 +107,7 @@ protected async Task DockerCleanup(Func<DbConnection> createConnection, Func<DbC
101107
}
102108
catch
103109
{
110+
await Task.Delay(1000);
104111
return;
105112
}
106113
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dbUp:
2+
version: 1 # should be 1
3+
provider: CockroachDb # DB provider: sqlserver
4+
connectionString: $CONNSTR$ # Connection string to DB. For example, "Data Source=(localdb)\dbup;Initial Catalog=MyDb;Integrated Security=True" for sqlserver
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dbUp:
2+
version: 1 # should be 1
3+
provider: CockroachDb # DB provider: sqlserver
4+
connectionString: $CONNSTR$ # Connection string to DB. For example, "Data Source=(localdb)\dbup;Initial Catalog=MyDb;Integrated Security=True" for sqlserver
5+
connectionTimeoutSec: 60
6+
journalTo:
7+
schema: public
8+
table: journal
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT pg_sleep(60);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dbUp:
2+
version: 1 # should be 1
3+
provider: CockroachDb # DB provider: sqlserver
4+
connectionString: $CONNSTR$ # Connection string to DB. For example, "Data Source=(localdb)\dbup;Initial Catalog=MyDb;Integrated Security=True" for sqlserver
5+
connectionTimeoutSec: 10 # Connection timeout in seconds

0 commit comments

Comments
 (0)