Skip to content

Commit

Permalink
intialize variables from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
max-ieremenko committed Mar 20, 2018
1 parent 1627af4 commit 46281e8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ Download the [latest release](https://github.com/max-ieremenko/SqlDatabase/relea

#### CLI
```bash
$ SqlDatabase.exe upgrade "-database=Data Source=server;Initial Catalog=MyDatabase;Integrated Security=True" -from=c:\MyDatabase\upgrade
$ SqlDatabase.exe upgrade "-database=Data Source=server;Initial Catalog=MyDatabase;Integrated Security=True" -from=c:\MyDatabase\upgrade -varVariable1=value1 -varVariable2=value2
```
|Switch|Description|
|:--:|:----------|
|:--|:----------|
|-database|set connection string to target database|
|-from|path to a folder or .zip file with migration steps|
|-transaction|set transaction mode (none, perStep). Option [none] is default, means no trasactions. Option [perStep] means to use one transaction per each migration step|
|[-var]|set a variable in format "=var[name of variable]=[value of variable]"|

Exit codes
* 0 - OK
Expand All @@ -27,7 +28,7 @@ Exit codes

#### Content example of a folder or .zip file with migration steps
|File|Description|
|:--:|:----------|
|:--|:----------|
|1.0_1.3.zip|.zip file with archived migration steps from database version 1.0 to 1.3 (inclusive).|
|1.3_2.0.sql|the database migration step from version 1.3 to 2.0|
|2.0_2.1.sql|the database migration step from version 2.0 to 2.1|
Expand Down Expand Up @@ -101,13 +102,13 @@ Any entry like *{{VariableName}}* is interpreted as variable and has to be chang
Non defined value of a variable leads to and error and stops migration execution.

The value is resolving in the following order:
1. TODO
1. check command line
2. check environment variable (Environment.GetEnvironmentVariable())

Predefined variables:

|Name|Description|
|:--:|:----------|
|:--|:----------|
|DatabaseName|the target database name|
|CurrentVersion|the database version before execution of a migration step|
|TargetVersion|the database version after execution of a migration step|
Expand Down
9 changes: 8 additions & 1 deletion Sources/SqlDatabase.Test/Configuration/CommandLineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ public void ParseUpgrade()
"upgrade",
"-database=Data Source=SQL2016DEV;Initial Catalog=test",
@"-from=c:\folder",
"-transaction=PerStep");
"-transaction=PerStep",
"-varX=1 2 3",
"-varY=value");

Assert.AreEqual(Command.Upgrade, actual.Command);
StringAssert.AreEqualIgnoringCase(@"c:\folder", actual.Scripts);

Assert.IsNotNull(actual.Connection);
StringAssert.AreEqualIgnoringCase("test", actual.Connection.InitialCatalog);
StringAssert.AreEqualIgnoringCase("SQL2016DEV", actual.Connection.DataSource);

Assert.AreEqual(TransactionMode.PerStep, actual.Transaction);

CollectionAssert.AreEquivalent(new[] { "X", "Y" }, actual.Variables.Keys);
Assert.AreEqual("1 2 3", actual.Variables["x"]);
Assert.AreEqual("value", actual.Variables["y"]);
}

[Test]
Expand Down
21 changes: 21 additions & 0 deletions Sources/SqlDatabase/Configuration/CommandLine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace SqlDatabase.Configuration
Expand All @@ -8,6 +9,7 @@ internal sealed class CommandLine
private const string ArgDatabase = "-database";
private const string ArgScripts = "-from";
private const string ArgTransaction = "-transaction";
private const string ArgVariable = "-var";

public Command Command { get; private set; }

Expand All @@ -17,6 +19,8 @@ internal sealed class CommandLine

public string Scripts { get; private set; }

public IDictionary<string, string> Variables { get; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

public static CommandLine Parse(params string[] args)
{
var result = new CommandLine();
Expand Down Expand Up @@ -92,6 +96,23 @@ private static bool ApplyArg(CommandLine cmd, string arg)
return true;
}

if (key.StartsWith(ArgVariable, StringComparison.OrdinalIgnoreCase))
{
var name = key.Substring(ArgVariable.Length).Trim();
if (name.Length == 0)
{
throw new InvalidCommandException(key, "Invalid variable name [{0}].".FormatWith(key));
}

if (cmd.Variables.ContainsKey(name))
{
throw new InvalidCommandException(key, "Variable with name [{0}] is duplicated.".FormatWith(name));
}

cmd.Variables.Add(name, value);
return true;
}

return false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/SqlDatabase/Configuration/CommandLine.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
SqlDatabase.exe upgrade "-database=Data Source=server;Initial Catalog=database;Integrated Security=True" -from=c:\
SqlDatabase.exe upgrade "-database=Data Source=server;Initial Catalog=database;Integrated Security=True" -from=c:\ -varVariable1=value1 -varVariable2=value2
[-database] - connection string to target database
[-from] - folder or zip file with upgrade scripts
[-transaction] - transaction mode. Possible values: none, perStep. Default is none.
[-var] - set a variable in format "=var[name of variable]=[value of variable]"
Exit codes:
0 - OK
1 - invalid command line
21 changes: 14 additions & 7 deletions Sources/SqlDatabase/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,23 @@ private static bool DoUpgrade(CommandLine cmd)
cmd.Connection.InitialCatalog,
cmd.Connection.DataSource));

var database = new Database
{
ConnectionString = cmd.Connection.ToString(),
Log = _logger,
Configuration = AppConfiguration.GetCurrent(),
Transaction = cmd.Transaction
};

foreach (var entry in cmd.Variables)
{
database.Variables.SetValue(entry.Key, entry.Value);
}

var upgrade = new SequentialUpgrade
{
Log = _logger,
Database = new Database
{
ConnectionString = cmd.Connection.ToString(),
Log = _logger,
Configuration = AppConfiguration.GetCurrent(),
Transaction = cmd.Transaction
},
Database = database,
ScriptSequence = new ScriptSequence
{
Root = FileSytemFactory.FolderFromPath(cmd.Scripts),
Expand Down

0 comments on commit 46281e8

Please sign in to comment.