From c7bf8ff5354bdaa56b5c58becd70d3c4b11a4291 Mon Sep 17 00:00:00 2001 From: Wes Shaddix Date: Mon, 2 Feb 2015 00:21:44 -0500 Subject: [PATCH 1/2] working towards json based config --- src/SqlCi.Console/ConfigurationValues.cs | 21 +- src/SqlCi.Console/Program.cs | 195 ++++++------------- src/SqlCi.Console/Properties/AssemblyInfo.cs | 8 +- src/SqlCi.Console/SqlCi.Console.csproj | 6 + src/SqlCi.Console/config.json | 21 ++ src/SqlCi.Console/packages.config | 1 + 6 files changed, 108 insertions(+), 144 deletions(-) create mode 100644 src/SqlCi.Console/config.json diff --git a/src/SqlCi.Console/ConfigurationValues.cs b/src/SqlCi.Console/ConfigurationValues.cs index 098331c..721a106 100644 --- a/src/SqlCi.Console/ConfigurationValues.cs +++ b/src/SqlCi.Console/ConfigurationValues.cs @@ -2,19 +2,34 @@ { internal class ConfigurationValues { + public bool Deploy { get; set; } + internal string ConnectionString { 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; } + internal string RollBackToVersion { get; set; } + internal string ScriptsFolder { get; set; } + 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; } } } \ No newline at end of file diff --git a/src/SqlCi.Console/Program.cs b/src/SqlCi.Console/Program.cs index a14ba24..3b921dc 100644 --- a/src/SqlCi.Console/Program.cs +++ b/src/SqlCi.Console/Program.cs @@ -1,8 +1,8 @@ using Mono.Options; +using Newtonsoft.Json.Linq; using SqlCi.ScriptRunner; using System; using System.Collections.Generic; -using System.Configuration; using System.IO; using System.Linq; using System.Reflection; @@ -17,17 +17,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 ", 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 ", p => config.GenerateScript = p != null} }; @@ -49,106 +41,43 @@ 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(); + throw new FileNotFoundException("Couldn't open config.json file. Make sure the file exists and then try again."); } - config.ReleaseNumber = GetReleaseNumber(); - config.ScriptTable = GetScriptTable(); - config.Environment = GetEnvironment(); - return config; - } - private static string GetConnectionString() - { - var connectionStringSettings = ConfigurationManager.ConnectionStrings["Database"]; + // load the configuration file + dynamic configuration = JObject.Parse(File.ReadAllText("config.json")); - if (null == connectionStringSettings) - { - throw new ApplicationException("There is no connection string named 'Database' in the configuration file."); - } - - // grab the connection string - string connectionString = connectionStringSettings.ConnectionString; + // TODO: verify that the target environment configuration exists - if (string.IsNullOrEmpty(connectionString)) + // load the config values + if (!generateOnly) { - System.Console.WriteLine("The connectionString attribute of the 'Database' connection string in the configuration file is empty"); - return string.Empty; + config.ConnectionString = configuration[environment].connectionString.Value; + config.ResetDatabase = bool.Parse(configuration[environment].resetDatabase.Value); } - return connectionString; - } - - private static string GetEnvironment() - { - return ConfigurationManager.AppSettings["Environment"]; - } - - private static string GetReleaseNumber() - { - return ConfigurationManager.AppSettings["ReleaseVersion"]; - } + 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; - private static string GetResetConnectionString() - { - var connectionStringSettings = ConfigurationManager.ConnectionStrings["ResetDatabase"]; - - if (null == connectionStringSettings) - { - throw new ApplicationException("There is no connection string named 'ResetDatabase' in the configuration file."); - } - - // grab the connection string - string connectionString = connectionStringSettings.ConnectionString; - - 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); @@ -157,66 +86,58 @@ 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 + // 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) { - // grab the configuration values from the .config file if the user specified to - if (config.UseConfigFile) - { - GetConfigurationValues(config); - } - - // 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(); - - var executor = new Executor(); - - // write any status updates that the executor sends to the console - executor.StatusUpdate += (sender, @event) => System.Console.WriteLine(@event.Status); - - if (config.ShowHistory) - { - var runHistory = executor.GetHistory(scriptConfiguration); - ShowHistory(runHistory); - return 0; - } + 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; + } - var executionResults = executor.Execute(scriptConfiguration); + // create a script configuration + var scriptConfiguration = new ScriptConfiguration() + .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(); - // if we were successful return 0 - if (executionResults.WasSuccessful) - { - return 0; - } + var executor = new Executor(); - // otherwise return -1 to signal an error - return -1; - } - catch (Exception ex) + // write any status updates that the executor sends to the console + executor.StatusUpdate += (sender, @event) => System.Console.WriteLine(@event.Status); + + if (config.ShowHistory) { - ShowConsoleError(ex.Message); - return -1; + var runHistory = executor.GetHistory(scriptConfiguration); + ShowHistory(runHistory); return 0; } + + var executionResults = executor.Execute(scriptConfiguration); + + // if we were successful return 0 + if (executionResults.WasSuccessful) { return 0; } + + // otherwise return -1 to signal an error + return -1; } private static void ShowConsoleError(string msg) diff --git a/src/SqlCi.Console/Properties/AssemblyInfo.cs b/src/SqlCi.Console/Properties/AssemblyInfo.cs index 36a0214..cc3a4dd 100644 --- a/src/SqlCi.Console/Properties/AssemblyInfo.cs +++ b/src/SqlCi.Console/Properties/AssemblyInfo.cs @@ -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")] \ No newline at end of file +[assembly: AssemblyVersion("0.9.1")] +[assembly: AssemblyFileVersion("0.9.1")] \ No newline at end of file diff --git a/src/SqlCi.Console/SqlCi.Console.csproj b/src/SqlCi.Console/SqlCi.Console.csproj index 9a4891d..7387212 100644 --- a/src/SqlCi.Console/SqlCi.Console.csproj +++ b/src/SqlCi.Console/SqlCi.Console.csproj @@ -37,6 +37,9 @@ false + + ..\packages\Newtonsoft.Json.6.0.8\lib\net40\Newtonsoft.Json.dll + @@ -56,6 +59,9 @@ Designer + + Always + diff --git a/src/SqlCi.Console/config.json b/src/SqlCi.Console/config.json new file mode 100644 index 0000000..dd03611 --- /dev/null +++ b/src/SqlCi.Console/config.json @@ -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" + } +} diff --git a/src/SqlCi.Console/packages.config b/src/SqlCi.Console/packages.config index 0c1033c..992bfe2 100644 --- a/src/SqlCi.Console/packages.config +++ b/src/SqlCi.Console/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file From 66c202b8a7286e1b02980d695557842c3a5520db Mon Sep 17 00:00:00 2001 From: Wes Shaddix Date: Mon, 2 Feb 2015 00:46:19 -0500 Subject: [PATCH 2/2] fixes issue1 --- src/SqlCi.Console/ConfigurationValues.cs | 16 +---- src/SqlCi.Console/Program.cs | 75 ++++++++++++++---------- src/SqlCi.ScriptRunner/Executor.cs | 2 +- 3 files changed, 46 insertions(+), 47 deletions(-) diff --git a/src/SqlCi.Console/ConfigurationValues.cs b/src/SqlCi.Console/ConfigurationValues.cs index 721a106..afc6125 100644 --- a/src/SqlCi.Console/ConfigurationValues.cs +++ b/src/SqlCi.Console/ConfigurationValues.cs @@ -2,34 +2,20 @@ { internal class ConfigurationValues { - public bool Deploy { get; set; } - internal string ConnectionString { get; set; } - + internal bool Deploy { get; set; } internal string Environment { 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; } - internal string RollBackToVersion { get; set; } - internal string ScriptsFolder { get; set; } - 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; } } } \ No newline at end of file diff --git a/src/SqlCi.Console/Program.cs b/src/SqlCi.Console/Program.cs index 3b921dc..da4781f 100644 --- a/src/SqlCi.Console/Program.cs +++ b/src/SqlCi.Console/Program.cs @@ -3,6 +3,7 @@ using SqlCi.ScriptRunner; using System; using System.Collections.Generic; +using System.Configuration; using System.IO; using System.Linq; using System.Reflection; @@ -52,7 +53,11 @@ private static ConfigurationValues GetConfigurationValues(ConfigurationValues co // load the configuration file dynamic configuration = JObject.Parse(File.ReadAllText("config.json")); - // TODO: verify that the target environment configuration exists + // verify that the target environment configuration exists + if (null == configuration[environment]) + { + throw new ConfigurationErrorsException(string.Format("There are no values for the \"{0}\" environment in the config.json file.", environment)); + } // load the config values if (!generateOnly) @@ -100,44 +105,52 @@ private static int Main(string[] args) return 0; } - // 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) + try { - 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; - } + // 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); - // create a script configuration - var scriptConfiguration = new ScriptConfiguration() - .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(); + // if the user wants to generate a script file + if (config.GenerateScript) + { + 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; + } - var executor = new Executor(); + // create a script configuration + var scriptConfiguration = new ScriptConfiguration() + .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(); - // write any status updates that the executor sends to the console - executor.StatusUpdate += (sender, @event) => System.Console.WriteLine(@event.Status); + var executor = new Executor(); - if (config.ShowHistory) - { - var runHistory = executor.GetHistory(scriptConfiguration); - ShowHistory(runHistory); return 0; - } + // write any status updates that the executor sends to the console + executor.StatusUpdate += (sender, @event) => System.Console.WriteLine(@event.Status); + + if (config.ShowHistory) + { + var runHistory = executor.GetHistory(scriptConfiguration); + ShowHistory(runHistory); return 0; + } - var executionResults = executor.Execute(scriptConfiguration); + var executionResults = executor.Execute(scriptConfiguration); - // if we were successful return 0 - if (executionResults.WasSuccessful) { return 0; } + // if we were successful return 0 + if (executionResults.WasSuccessful) { return 0; } - // otherwise return -1 to signal an error - return -1; + // otherwise return -1 to signal an error + return -1; + } + catch (Exception ex) + { + ShowConsoleError(ex.GetBaseException().Message); + return -1; + } } private static void ShowConsoleError(string msg) diff --git a/src/SqlCi.ScriptRunner/Executor.cs b/src/SqlCi.ScriptRunner/Executor.cs index 1b8fded..b94ee98 100644 --- a/src/SqlCi.ScriptRunner/Executor.cs +++ b/src/SqlCi.ScriptRunner/Executor.cs @@ -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