Skip to content

Commit

Permalink
Merge pull request #2 from wshaddix/feat-json-config
Browse files Browse the repository at this point in the history
Adding json based configs
  • Loading branch information
Wes Shaddix committed Feb 2, 2015
2 parents 0332159 + 66c202b commit 1578a9c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 119 deletions.
7 changes: 4 additions & 3 deletions src/SqlCi.Console/ConfigurationValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
internal class ConfigurationValues
{
internal string ConnectionString { get; set; }
internal bool Deploy { get; set; }
internal string Environment { get; set; }
internal string ReleaseNumber { get; set; }
internal bool GenerateScript { get; set; }
internal string ReleaseVersion { get; set; }
internal string ResetConnectionString { get; set; }
internal bool ResetDatabase { get; set; }
internal string ResetScriptsFolder { get; set; }
Expand All @@ -13,8 +15,7 @@ internal class ConfigurationValues
internal string ScriptTable { get; set; }
internal bool ShowHelp { get; set; }
internal bool ShowHistory { get; set; }
internal bool ShowVersionNumber { get; set; }
internal bool UseConfigFile { get; set; }
internal bool VersionNumber { get; set; }
internal bool GenerateScript { get; set; }
}
}
156 changes: 45 additions & 111 deletions src/SqlCi.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mono.Options;
using Newtonsoft.Json.Linq;
using SqlCi.ScriptRunner;
using System;
using System.Collections.Generic;
Expand All @@ -17,17 +18,9 @@ private static OptionSet DefineOptionSet(string[] args, out ConfigurationValues

var optionSet = new OptionSet()
{
{"uc|useConfig", "Determines whether to get config values from the SqlCi.Console.exe.config file or the command line arguments", p => config.UseConfigFile = p != null},
{"cs|connectionString=", "The connection string to use to access the database to run the scripts in", p => config.ConnectionString = p },
{"st|scriptTable=", "The name of the script table", p => config.ScriptTable = p },
{"rv|releaseVersion=", "The version of this release", p => config.ReleaseNumber = p},
{"sf|scriptsFolder=", "The folder that holds the sql scripts to be ran", p => config.ScriptsFolder = p},
{"ev|environment=", "The environment that the scripts are being ran in", p => config.Environment = p},
{"rd|resetDatabase", "Determines if the database should be reset", p => config.ResetDatabase = p != null},
{"rf|resetScriptsFolder=", "The folder that holds the database reset scripts to be ran if resetDatabase is specified", p => config.ResetScriptsFolder = p},
{"rc|resetConnectionString=", "The connection string to use to reset the database", p => config.ResetConnectionString = p},
{"d|deploy", "deploy the database. Usage: d <environment>", p => config.Deploy = p != null},
{"h|help", "show this message and exit", p => config.ShowHelp = p != null},
{"v|version", "show the version number", p => config.VersionNumber = p != null},
{"v|version", "show the version number", p => config.ShowVersionNumber = p != null},
{"sh|showHistory", "show the history of scripts ran against the database", p => config.ShowHistory = p != null},
{"g|generateScript", "generates a new script file. Usage: g <environment> <script_name> <script_folder>", p => config.GenerateScript = p != null}
};
Expand All @@ -49,106 +42,47 @@ private static OptionSet DefineOptionSet(string[] args, out ConfigurationValues
return optionSet;
}

private static ConfigurationValues GetConfigurationValues(ConfigurationValues config)
private static ConfigurationValues GetConfigurationValues(ConfigurationValues config, string environment, bool generateOnly)
{
config.ConnectionString = GetConnectionString();
config.ScriptsFolder = GetScriptsFolder();
config.ResetDatabase = GetResetDatabase();
if (config.ResetDatabase)
// verify that the config file exists
if (!File.Exists("config.json"))
{
config.ResetScriptsFolder = GetResetScriptsFolder();
config.ResetConnectionString = GetResetConnectionString();
}
config.ReleaseNumber = GetReleaseNumber();
config.ScriptTable = GetScriptTable();
config.Environment = GetEnvironment();
return config;
}

private static string GetConnectionString()
{
var connectionStringSettings = ConfigurationManager.ConnectionStrings["Database"];

if (null == connectionStringSettings)
{
throw new ApplicationException("There is no connection string named 'Database' in the configuration file.");
throw new FileNotFoundException("Couldn't open config.json file. Make sure the file exists and then try again.");
}

// grab the connection string
string connectionString = connectionStringSettings.ConnectionString;
// load the configuration file
dynamic configuration = JObject.Parse(File.ReadAllText("config.json"));

if (string.IsNullOrEmpty(connectionString))
// verify that the target environment configuration exists
if (null == configuration[environment])
{
System.Console.WriteLine("The connectionString attribute of the 'Database' connection string in the configuration file is empty");
return string.Empty;
throw new ConfigurationErrorsException(string.Format("There are no values for the \"{0}\" environment in the config.json file.", environment));
}

return connectionString;
}

private static string GetEnvironment()
{
return ConfigurationManager.AppSettings["Environment"];
}

private static string GetReleaseNumber()
{
return ConfigurationManager.AppSettings["ReleaseVersion"];
}

private static string GetResetConnectionString()
{
var connectionStringSettings = ConfigurationManager.ConnectionStrings["ResetDatabase"];

if (null == connectionStringSettings)
// load the config values
if (!generateOnly)
{
throw new ApplicationException("There is no connection string named 'ResetDatabase' in the configuration file.");
config.ConnectionString = configuration[environment].connectionString.Value;
config.ResetDatabase = bool.Parse(configuration[environment].resetDatabase.Value);
}

// grab the connection string
string connectionString = connectionStringSettings.ConnectionString;
config.ScriptsFolder = configuration.common.scriptsFolder.Value;
config.ResetScriptsFolder = configuration.common.resetScriptsFolder.Value;
config.ReleaseVersion = configuration.common.releaseVersion.Value;
config.ScriptTable = configuration.common.scriptTable.Value;
config.Environment = environment;

if (string.IsNullOrEmpty(connectionString))
if (!generateOnly && config.ResetDatabase)
{
System.Console.WriteLine("The connectionString attribute of the 'ResetDatabase' connection string in the configuration file is empty");
return string.Empty;
config.ResetConnectionString = configuration[environment].resetConnectionString.Value;
}

return connectionString;
}

private static bool GetResetDatabase()
{
bool resetDatabase;
bool.TryParse(ConfigurationManager.AppSettings["ResetDatabase"], out resetDatabase);
return resetDatabase;
}

private static string GetResetScriptsFolder()
{
return ConfigurationManager.AppSettings["ResetScriptsFolder"];
}

private static string GetScriptsFolder()
{
return ConfigurationManager.AppSettings["ScriptsFolder"];
}

private static string GetScriptTable()
{
return ConfigurationManager.AppSettings["ScriptTable"];
return config;
}

private static int Main(string[] args)
{
// if the 1st arg is 'g' then the user just wants to generate a new sql script file
if (args.Length == 4 && args[0].StartsWith("g"))
{
var fileName = string.Format("{0}_{1}_{2}.sql", DateTime.Now.ToString("yyyyMMddHHmmssfff"), args[1], args[2]);
File.CreateText(string.Format(@"{0}\{1}", args[3], fileName));
return 0;
}

// parse the arguments
ConfigurationValues config;
var optionSet = DefineOptionSet(args, out config);

Expand All @@ -157,37 +91,41 @@ private static int Main(string[] args)
return -1;
}

// if there are no arguments then show the help and exit
if (config.ShowHelp || args.Length == 0)
{
ShowHelp(optionSet);
return 0;
}

if (config.VersionNumber)
// if the user wants the version number
if (config.ShowVersionNumber)
{
ShowVersion();
return 0;
}

try
{
// grab the configuration values from the .config file if the user specified to
if (config.UseConfigFile)
// we need to read in the config.json file here because all other options requires
// that we know the config
GetConfigurationValues(config, args[1], config.GenerateScript);

// if the user wants to generate a script file
if (config.GenerateScript)
{
GetConfigurationValues(config);
var fileName = string.Format("{0}_{1}_{2}.sql", DateTime.Now.ToString("yyyyMMddHHmmssfff"), config.Environment, args[2]);
File.CreateText(string.Format(@"{0}\{1}", config.ScriptsFolder, fileName));
return 0;
}

// create a script configuration
var scriptConfiguration = new ScriptConfiguration()
.WithConnectionString(config.ConnectionString)
.WithScriptsFolder(config.ScriptsFolder)
.WithResetDatabase(config.ResetDatabase)
.WithResetFolder(config.ResetScriptsFolder)
.WithReleaseNumber(config.ReleaseNumber)
.WithScriptTable(config.ScriptTable)
.WithEnvironment(config.Environment)
.WithResetConnectionString(config.ResetConnectionString)
.Verify();
.WithConnectionString(config.ConnectionString)
.WithScriptsFolder(config.ScriptsFolder).WithResetDatabase(config.ResetDatabase)
.WithResetFolder(config.ResetScriptsFolder).WithReleaseNumber(config.ReleaseVersion)
.WithScriptTable(config.ScriptTable).WithEnvironment(config.Environment)
.WithResetConnectionString(config.ResetConnectionString).Verify();

var executor = new Executor();

Expand All @@ -197,24 +135,20 @@ private static int Main(string[] args)
if (config.ShowHistory)
{
var runHistory = executor.GetHistory(scriptConfiguration);
ShowHistory(runHistory);
return 0;
ShowHistory(runHistory); return 0;
}

var executionResults = executor.Execute(scriptConfiguration);

// if we were successful return 0
if (executionResults.WasSuccessful)
{
return 0;
}
if (executionResults.WasSuccessful) { return 0; }

// otherwise return -1 to signal an error
return -1;
}
catch (Exception ex)
{
ShowConsoleError(ex.Message);
ShowConsoleError(ex.GetBaseException().Message);
return -1;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/SqlCi.Console/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
[assembly: Guid("1b5d1aca-9c96-4baa-b71b-ad4f2ec579eb")]

// Version information for an assembly consists of the following four values:
//
//
// Major Version Minor Version Build Number Revision
//
//
// You can specify all the values or you can default the Build and Revision Numbers by using the '*'
// as shown below: [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("0.9.1")]
[assembly: AssemblyFileVersion("0.9.1")]
6 changes: 6 additions & 0 deletions src/SqlCi.Console/SqlCi.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
Expand All @@ -56,6 +59,9 @@
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down
21 changes: 21 additions & 0 deletions src/SqlCi.Console/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"common": {
"scriptTable": "ScriptTable",
"releaseVersion": "1.0.0",
"resetScriptsFolder": ".\\Reset",
"scriptsFolder": ".\\Scripts"
},
"workstation": {
"resetConnectionString": "server=(localdb)\\v11.0; database=master; integrated security=true;",
"connectionString": "server=(localdb)\\v11.0; database=RemoteDevJobs_Workstation; integrated security=true;",
"resetDatabase": "true"
},
"qa": {
"connectionString": "server=(localdb)\\v11.0; database=RemoteDevJobs_Qa; integrated security=true;",
"resetDatabase": "false"
},
"production": {
"connectionString": "server=(localdb)\\v11.0; database=RemoteDevJobs_Production; integrated security=true;",
"resetDatabase": "false"
}
}
1 change: 1 addition & 0 deletions src/SqlCi.Console/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Mono.Options" version="1.1" targetFramework="net40" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net40" />
</packages>
2 changes: 1 addition & 1 deletion src/SqlCi.ScriptRunner/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private void Reset()
{
// load the reset sql scripts
UpdateStatus("Loading reset script(s) from {0} ...", _scriptConfiguration.ResetFolder);
var resetScripts = LoadSqlScriptFiles(_scriptConfiguration.ResetFolder, true);
var resetScripts = LoadSqlScriptFiles(_scriptConfiguration.ResetFolder);
UpdateStatus("Loaded {0} reset script(s) from {1} ...", resetScripts.Count, _scriptConfiguration.ResetFolder);

// if there are no scripts to run, just exit
Expand Down

0 comments on commit 1578a9c

Please sign in to comment.