Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Mayflower/Mayflower.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down
11 changes: 9 additions & 2 deletions Mayflower/Migrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -122,7 +129,7 @@ static IEnumerable<Migration> 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();
Expand Down
34 changes: 30 additions & 4 deletions Mayflower/Options.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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))
Expand All @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions MayflowerCLI/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<connectionStrings>
<clear />
</connectionStrings>
</configuration>
5 changes: 4 additions & 1 deletion MayflowerCLI/MayflowerCLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="App.config">
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand All @@ -74,5 +76,6 @@
<Target Name="AfterBuild">
<!-- Make a copy of the exe without the CLI part -->
<Copy SourceFiles="bin\$(Configuration)\MayflowerCLI.exe" DestinationFiles="bin\$(Configuration)\mayflower.exe" />
<Copy SourceFiles="bin\$(Configuration)\MayflowerCLI.exe.config" DestinationFiles="bin\$(Configuration)\mayflower.exe.config" />
</Target>
</Project>
11 changes: 10 additions & 1 deletion MayflowerCLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 },
Expand Down Expand Up @@ -102,10 +106,15 @@ static void ShowHelpMessage(OptionSet optionSet)
{
Console.WriteLine("Usage: mayflower [OPTIONS]+");
Console.WriteLine(" Runs all *.sql files in the directory --dir=<directory>.");
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);
}

Expand Down