-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from drwatson1/feature/cockroachdb
Add CockroachDB support
- Loading branch information
Showing
14 changed files
with
203 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/dbup-cli.integration-tests/Scripts/CockroachDb/EmptyScript/001.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
4 changes: 4 additions & 0 deletions
4
src/dbup-cli.integration-tests/Scripts/CockroachDb/EmptyScript/dbup.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
src/dbup-cli.integration-tests/Scripts/CockroachDb/JournalTableScript/001.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
8 changes: 8 additions & 0 deletions
8
src/dbup-cli.integration-tests/Scripts/CockroachDb/JournalTableScript/dbup.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
src/dbup-cli.integration-tests/Scripts/CockroachDb/Timeout/001.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT pg_sleep(60); |
5 changes: 5 additions & 0 deletions
5
src/dbup-cli.integration-tests/Scripts/CockroachDb/Timeout/dbup.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ public enum Provider | |
SqlServer, | ||
PostgreSQL, | ||
MySQL, | ||
AzureSql | ||
AzureSql, | ||
CockroachDB | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters