Skip to content

Commit

Permalink
Merge pull request #28 from Hviid/feature/ConnectionStringOptions
Browse files Browse the repository at this point in the history
Allow options
  • Loading branch information
Hviid authored Oct 28, 2021
2 parents 318d1ac + c5397e7 commit 14502ac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
8 changes: 7 additions & 1 deletion DBMigrator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public static int Main(string[] args)
"Runs command without Database validation first",
CommandOptionType.NoValue);

CommandOption optionsArg = commandLineApplication.Option(
"--options <options>",
"Extra options appended to the connection string, ie --options Encrypt=False;TrustServerCertificate=False;",
CommandOptionType.SingleValue);

IServiceCollection serviceCollection = new ServiceCollection();
Bootstrapper.ConfigureServices(serviceCollection);

Expand All @@ -95,7 +100,8 @@ public static int Main(string[] args)
var username = usernameArg.Value();
var password = passwordArg.Value();
var databasename = databasenameArg.Value();
var database = new Database(servername, databasename, username, password);
var options = optionsArg.Value();
var database = new Database(servername, databasename, username, password, options);

try
{
Expand Down
12 changes: 6 additions & 6 deletions DBMigrator.Test/DatabaseChecksumTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DatabaseChecksumTest
[TestMethod]
public void GetTablesViewsAndColumnsChecksum_doesnt_change()
{
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "");
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "", "");
var checksum = database.GetDatabaseCurrentChecksums();
var checksum2 = database.GetDatabaseCurrentChecksums();
Assert.AreEqual(BitConverter.ToString(checksum.DatabaseTablesAndViewsChecksum), BitConverter.ToString(checksum2.DatabaseTablesAndViewsChecksum));
Expand All @@ -21,7 +21,7 @@ public void GetTablesViewsAndColumnsChecksum_doesnt_change()
[TestMethod]
public void TablesChecksum_changes()
{
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "");
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "", "");
var checksum = database.GetDatabaseCurrentChecksums();
database.ExecuteSingleCommand("CREATE TABLE [dbo].[DBMigratorTable]([Test][nvarchar](max) NULL)");
var checksum2 = database.GetDatabaseCurrentChecksums();
Expand All @@ -32,7 +32,7 @@ public void TablesChecksum_changes()
[TestMethod]
public void ViewsChecksum_changes()
{
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "");
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "", "");
database.ExecuteSingleCommand("CREATE TABLE [dbo].[DBMigratorTable]([Test][nvarchar](max) NULL)");
var checksum = database.GetDatabaseCurrentChecksums();
database.ExecuteSingleCommand("CREATE VIEW [dbo].DBMigratorView AS SELECT Test FROM [dbo].[DBMigratorTable]");
Expand All @@ -45,7 +45,7 @@ public void ViewsChecksum_changes()
[TestMethod]
public void TableColumnsChecksum_changes()
{
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "");
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "", "");
database.ExecuteSingleCommand("CREATE TABLE [dbo].[DBMigratorTable]([Test][nvarchar](max) NULL)");
var checksum = database.GetDatabaseCurrentChecksums();
database.ExecuteSingleCommand("ALTER TABLE [dbo].[DBMigratorTable] ADD Test2 int");
Expand All @@ -57,7 +57,7 @@ public void TableColumnsChecksum_changes()
[TestMethod]
public void TableColumnsDataTypeChecksum_changes()
{
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "");
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "", "");
database.ExecuteSingleCommand("CREATE TABLE [dbo].[DBMigratorTable]([Test][nvarchar](max) NULL)");
var checksum = database.GetDatabaseCurrentChecksums();
database.ExecuteSingleCommand("ALTER TABLE [dbo].[DBMigratorTable] ALTER COLUMN [Test] int");
Expand All @@ -69,7 +69,7 @@ public void TableColumnsDataTypeChecksum_changes()
[TestMethod]
public void ViewColumnsChecksum_changes()
{
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "");
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "", "");
database.ExecuteSingleCommand("CREATE TABLE [dbo].[DBMigratorTable]([Test][nvarchar](max) NULL, Test2 int)");
database.ExecuteSingleCommand("CREATE VIEW [dbo].DBMigratorView AS SELECT Test FROM [dbo].[DBMigratorTable]");
var checksum = database.GetDatabaseCurrentChecksums();
Expand Down
6 changes: 3 additions & 3 deletions DBMigrator.Test/DatabaseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class DatabaseTest
[TestMethod]
public void Versions_noversions_test()
{
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "");
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "", "");
database.GetDBState();
database.ExecuteSingleCommand("DELETE FROM DBVersionScripts");
var versions = database.GetDBState();
Expand All @@ -32,7 +32,7 @@ public void Versions_one_versions_test()
script.SQL = "SELECT * FROM DBVersionScripts";


var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "");
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "", "");
database.GetDBState();
database.ExecuteSingleCommand("DELETE FROM DBVersionScripts");

Expand All @@ -54,7 +54,7 @@ public void Versions_one_versions_test()
[TestMethod]
public void Message_test()
{
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "");
var database = new Database(@"(localdb)\mssqllocaldb", "MyDatabase", "", "", "");
database.GetDBState();
database.ExecuteSingleCommand("PRINT 'Test1'");
database.ExecuteSingleCommand("RAISERROR('Test2', 0, 1, 'asd')");
Expand Down
6 changes: 3 additions & 3 deletions DBMigrator/Database/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ public class Database : IDatabase
private Regex _goRegex = new Regex(@"[\n\r]GO\b");


public Database(string servername, string database, string username, string password)
public Database(string servername, string database, string username, string password, string options)
{
var loggerFactory = Bootstrapper.GetConfiguredServiceProvider().GetRequiredService<ILoggerFactory>();
_logger = loggerFactory.CreateLogger<Database>();

string connectionString;
if (string.IsNullOrEmpty(username) && string.IsNullOrEmpty(password))
{
connectionString = $"Application Name=DbMigrator;Data Source={servername};Initial Catalog={database};Integrated Security=true;MultipleActiveResultSets=True";
connectionString = $"Application Name=DbMigrator;Data Source={servername};Initial Catalog={database};Integrated Security=true;MultipleActiveResultSets=True;" + options;
}
else
{
connectionString = $"Application Name=DbMigrator;Data Source={servername};Initial Catalog={database};Persist Security Info=True;User ID={username};Password={password};MultipleActiveResultSets=True";
connectionString = $"Application Name=DbMigrator;Data Source={servername};Initial Catalog={database};Persist Security Info=True;User ID={username};Password={password};MultipleActiveResultSets=True;" + options;
}
SetupConnAndLogger(connectionString);
}
Expand Down

0 comments on commit 14502ac

Please sign in to comment.