From 82791859b4b4677be534e9c7152f947925672432 Mon Sep 17 00:00:00 2001 From: Attila Szasz Date: Tue, 25 Apr 2017 17:13:44 +0300 Subject: [PATCH] Added option to use conncetion string from App.config with default to first connection string. --- Mayflower/Mayflower.csproj | 1 + Mayflower/Migrator.cs | 11 +++++++++-- Mayflower/Options.cs | 34 ++++++++++++++++++++++++++++---- MayflowerCLI/App.config | 3 +++ MayflowerCLI/MayflowerCLI.csproj | 5 ++++- MayflowerCLI/Program.cs | 11 ++++++++++- 6 files changed, 57 insertions(+), 8 deletions(-) diff --git a/Mayflower/Mayflower.csproj b/Mayflower/Mayflower.csproj index 9f8b250..9943752 100644 --- a/Mayflower/Mayflower.csproj +++ b/Mayflower/Mayflower.csproj @@ -31,6 +31,7 @@ + diff --git a/Mayflower/Migrator.cs b/Mayflower/Migrator.cs index 46d1638..781c233 100644 --- a/Mayflower/Migrator.cs +++ b/Mayflower/Migrator.cs @@ -49,9 +49,16 @@ public class MigrationResult _db = new ConnectionContext(options); + Log(" Database: " + _db.Database); + Migrations = GetAllMigrations(dir, _db.CommandSplitter).ToList(); - Log(" Database: " + _db.Database); + if (!Migrations.Any()) + { + Log("No migrations found."); + return; + } + _db.Open(); Log(" Transaction Mode: " + (_useGlobalTransaction ? "Global" : "Individual")); @@ -122,7 +129,7 @@ static IEnumerable GetAllMigrations(string directory, Regex commandSp MigrationResult RunOutstandingMigrations() { - Log("Running migrations" + (_isPreview ? " (preview mode)" : "")); + if (Migrations.Any()) Log("Running migrations" + (_isPreview ? " (preview mode)" : "")); Log(); var result = new MigrationResult(); diff --git a/Mayflower/Options.cs b/Mayflower/Options.cs index 13cabf7..222616e 100644 --- a/Mayflower/Options.cs +++ b/Mayflower/Options.cs @@ -1,12 +1,14 @@ using System; using System.IO; using System.Text.RegularExpressions; +using System.Configuration; namespace Mayflower { public class Options { public string ConnectionString { get; set; } + public string ConnectionStringName { get; set; } public string Database { get; set; } public string Server { get; set; } public int CommandTimeout { get; set; } = 30; @@ -20,9 +22,20 @@ public class Options public void AssertValid() { - if (string.IsNullOrEmpty(ConnectionString) == string.IsNullOrEmpty(Database)) + if (string.IsNullOrWhiteSpace(ConnectionString) && string.IsNullOrWhiteSpace(Database) && string.IsNullOrWhiteSpace(ConnectionStringName)) { - throw new Exception("Either a connection string or a database must be specified."); + var count = ConfigurationManager.ConnectionStrings.Count; + if (count == 0 || + (count > 0 && string.IsNullOrWhiteSpace(ConfigurationManager.ConnectionStrings[0].ConnectionString))) + { + throw new Exception( + "There is no connection string in the config file. Either a connection string or a database must be specified."); + } + } + + if (!string.IsNullOrWhiteSpace(ConnectionStringName) && string.IsNullOrWhiteSpace(ConfigurationManager.ConnectionStrings[ConnectionStringName]?.ConnectionString)) + { + throw new Exception("When a connection string name is specified, it must exist in the .config file."); } if (!string.IsNullOrEmpty(MigrationsTable)) @@ -40,8 +53,21 @@ internal string GetConnectionString(DatabaseProvider provider) if (!string.IsNullOrEmpty(ConnectionString)) return ConnectionString; - var server = string.IsNullOrEmpty(Server) ? "localhost" : Server; - return $"Persist Security Info=False;Integrated Security=true;Initial Catalog={Database};server={server}"; + if (!string.IsNullOrEmpty(ConnectionStringName)) + return ConfigurationManager.ConnectionStrings[ConnectionStringName]?.ConnectionString; + + if (!string.IsNullOrEmpty(Database)) + { + var server = string.IsNullOrEmpty(Server) ? "localhost" : Server; + return $"Persist Security Info=False;Integrated Security=true;Initial Catalog={Database};server={server}"; + } + + if (ConfigurationManager.ConnectionStrings.Count > 0 && !string.IsNullOrWhiteSpace(ConfigurationManager.ConnectionStrings[0].ConnectionString)) + { + return ConfigurationManager.ConnectionStrings[0].ConnectionString; + } + + throw new Exception("There is no connection string in the config file. Either a connection string or a database must be specified."); } internal string GetMigrationsTable() diff --git a/MayflowerCLI/App.config b/MayflowerCLI/App.config index 88fa402..058f348 100644 --- a/MayflowerCLI/App.config +++ b/MayflowerCLI/App.config @@ -3,4 +3,7 @@ + + + \ No newline at end of file diff --git a/MayflowerCLI/MayflowerCLI.csproj b/MayflowerCLI/MayflowerCLI.csproj index 9b6105f..679ae98 100644 --- a/MayflowerCLI/MayflowerCLI.csproj +++ b/MayflowerCLI/MayflowerCLI.csproj @@ -51,7 +51,9 @@ - + + Designer + @@ -74,5 +76,6 @@ + \ No newline at end of file diff --git a/MayflowerCLI/Program.cs b/MayflowerCLI/Program.cs index 6591eeb..c5798ad 100644 --- a/MayflowerCLI/Program.cs +++ b/MayflowerCLI/Program.cs @@ -39,6 +39,9 @@ static void Run(string[] args) Console.WriteLine(count + " outstanding migrations"); Console.WriteLine(); break; + case Command.None: + Environment.Exit(1); + break; } Environment.Exit(0); @@ -55,6 +58,7 @@ static Command TryParseArgs(string[] args, out Options options) { { "h|help", "Shows this help message.", v => showHelp= v != null }, {"c|connection=", "A SQL Server connection string. For integrated auth, you can use --database and --server instead.", v => optionsTmp.ConnectionString = v }, + {"n|name=", "A conections string from the config file.", v => optionsTmp.ConnectionStringName = v }, {"d|database=", "Generates an integrated auth connection string for the specified database.", v => optionsTmp.Database = v }, {"s|server=", "Generates an integrated auth connection string with the specified server (default: localhost).", v=> optionsTmp.Server = v }, {"f|folder=", "The folder containing your .sql migration files (defaults to current working directory).", v => optionsTmp.MigrationsFolder = v }, @@ -102,10 +106,15 @@ static void ShowHelpMessage(OptionSet optionSet) { Console.WriteLine("Usage: mayflower [OPTIONS]+"); Console.WriteLine(" Runs all *.sql files in the directory --dir=."); - Console.WriteLine(" The databse connection can be specified using a full connection string with --connection,"); + Console.WriteLine(" The database connection can be specified using a full connection string with --connection,"); + Console.WriteLine(" a connection string name from the config file can be specified with --name,"); Console.WriteLine(" or Mayflower can generate an integrated auth connection string using the --database and"); Console.WriteLine(" optional --server arguments."); Console.WriteLine(); + Console.WriteLine(" If no connection was specified, but there is a connection string in the config file, it will"); + Console.WriteLine(" be used as default. If there is more then one connection string, the first one will be used."); + Console.WriteLine(); + optionSet.WriteOptionDescriptions(Console.Out); }