Skip to content

Commit

Permalink
Merge pull request #15 from Zocdoc/mvs-create-schemazen-library
Browse files Browse the repository at this point in the history
Mvs create schemazen library
  • Loading branch information
scott-roepnack-zocdoc committed May 17, 2016
2 parents c760863 + bad6b2c commit a1dff4c
Show file tree
Hide file tree
Showing 47 changed files with 538 additions and 326 deletions.
10 changes: 6 additions & 4 deletions SchemaZen.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "console", "console\console.csproj", "{95219FB8-6E54-4672-943B-3D2ECAED51A2}"
# Visual Studio 14
VisualStudioVersion = 14.0.24720.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Schemazen.Console", "console\Schemazen.Console.csproj", "{95219FB8-6E54-4672-943B-3D2ECAED51A2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "test", "test\test.csproj", "{73E24852-B1C6-476B-B5D9-ADB06D5870D2}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "test\Tests.csproj", "{73E24852-B1C6-476B-B5D9-ADB06D5870D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "model", "model\model.csproj", "{1283684F-CC5F-4647-BD97-9D833D20110E}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Schemazen.Library", "model\Schemazen.Library.csproj", "{1283684F-CC5F-4647-BD97-9D833D20110E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{45A1FC03-225C-4D1C-877B-5ECF25E392C5}"
ProjectSection(SolutionItems) = preProject
Expand Down
46 changes: 46 additions & 0 deletions console/BaseCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using ManyConsole;
using NDesk.Options;

namespace SchemaZen.console
{
public abstract class BaseCommand : ConsoleCommand {
protected BaseCommand(string command, string oneLineDescription)
{
IsCommand(command, oneLineDescription);
Options = new OptionSet();
SkipsCommandSummaryBeforeRunning();

HasOption("s|server=", "server", o => Server = o);
HasOption("b|database=", "database", o => DbName = o);
HasOption("c|connectionString=", "connection string", o => ConnectionString = o);
HasOption("u|user=", "user", o => User = o);
HasOption("p|pass=", "pass", o => Pass = o);
HasRequiredOption(
"d|scriptDir=",
"Path to database script directory.",
o => ScriptDir = o);
HasOption(
"o|overwrite=",
"Overwrite existing target without prompt.",
o => Overwrite = o != null);
HasOption(
"v|verbose=",
"Enable verbose log messages.",
o => Verbose = o != null);
HasOption(
"f|databaseFilesPath=",
"Path to database data and log files.",
o => DatabaseFilesPath = o);
}

protected string Server { get; set; }
protected string DbName { get; set; }
protected string ConnectionString { get; set; }
protected string User { get; set; }
protected string Pass { get; set; }
protected string ScriptDir { get; set; }
protected bool Overwrite { get; set; }
protected bool Verbose { get; set; }
protected string DatabaseFilesPath { get; set; }
}
}
52 changes: 26 additions & 26 deletions console/Compare.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.IO;
using ManyConsole;
using NDesk.Options;
using SchemaZen.model;
using SchemaZen.Library.Command;

namespace SchemaZen.console {
internal class Compare : ConsoleCommand {
Expand All @@ -13,7 +13,7 @@ internal class Compare : ConsoleCommand {
private bool _verbose;

public Compare() {
IsCommand("Compare", "Compare two databases.");
IsCommand("Compare", "CreateDiff two databases.");
Options = new OptionSet();
SkipsCommandSummaryBeforeRunning();
HasRequiredOption(
Expand All @@ -39,30 +39,30 @@ public Compare() {
}

public override int Run(string[] remainingArguments) {
var sourceDb = new Database();
var targetDb = new Database();
sourceDb.Connection = _source;
targetDb.Connection = _target;
sourceDb.Load();
targetDb.Load();
var diff = sourceDb.Compare(targetDb);
if (diff.IsDiff) {
Console.WriteLine("Databases are different.");
Console.WriteLine(diff.SummarizeChanges(_verbose));
if (!string.IsNullOrEmpty(_outDiff)) {
Console.WriteLine();
if (!_overwrite && File.Exists(_outDiff)) {
if (!ConsoleQuestion.AskYN(string.Format("{0} already exists - do you want to replace it", _outDiff))) {
return 1;
}
}
File.WriteAllText(_outDiff, diff.Script());
Console.WriteLine("Script to make the databases identical has been created at {0}", Path.GetFullPath(_outDiff));
}
return 1;
}
Console.WriteLine("Databases are identical.");
return 0;
if (!string.IsNullOrEmpty(_outDiff))
{
Console.WriteLine();
if (!_overwrite && File.Exists(_outDiff)) {
var question = string.Format("{0} already exists - do you want to replace it", _outDiff);
if (!ConsoleQuestion.AskYN(question))
{
return 1;
}
}
}

var compareCommand = new CompareCommand {
Source = _source,
Target = _target,
Verbose = _verbose,
OutDiff = _outDiff
};

try {
return compareCommand.Execute() ? 1 : 0;
} catch (Exception ex) {
throw new ConsoleHelpAsException(ex.Message);
}
}
}
}
97 changes: 61 additions & 36 deletions console/Create.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,76 @@
using System;
using System.Diagnostics;
using System.IO;
using SchemaZen.model;
using ManyConsole;
using SchemaZen.Library;
using SchemaZen.Library.Command;
using SchemaZen.Library.Models;

namespace SchemaZen.console {
public class Create : DbCommand {
public Create()
public class Create : BaseCommand {
private Logger _logger;
public Create()
: base(
"Create", "Create the specified database from scripts.") { }

public override int Run(string[] remainingArguments) {
var db = CreateDatabase();
if (!Directory.Exists(db.Dir)) {
Log(TraceLevel.Error, string.Format("Snapshot dir {0} does not exist.", db.Dir));
return 1;
}
_logger = new Logger(Verbose);

if (!Overwrite) {
Log(TraceLevel.Verbose, "Checking if database already exists...");
if (DBHelper.DbExists(db.Connection)) {
if (!ConsoleQuestion.AskYN(string.Format("{0} {1} already exists - do you want to drop it", Server, DbName))) {
Console.WriteLine("Create command cancelled.");
return 1;
}
Overwrite = true;
}
}
if (!Directory.Exists(ScriptDir))
{
_logger.Log(TraceLevel.Error, string.Format("Snapshot dir {0} does not exist.", ScriptDir));
return 1;
}

try {
db.CreateFromDir(Overwrite, DatabaseFilesPath, Log);
Log(TraceLevel.Info, Environment.NewLine + "Database created successfully.");
} catch (BatchSqlFileException ex) {
Log(TraceLevel.Info, Environment.NewLine + "Create completed with the following errors:");
foreach (var e in ex.Exceptions)
{
Log(TraceLevel.Info, string.Format("- {0} (Line {1}):", e.FileName.Replace("/", "\\"), e.LineNumber));
Log(TraceLevel.Error, string.Format(" {0}", e.Message));
}
return -1;
} catch (SqlFileException ex) {
Log(TraceLevel.Info, Environment.NewLine + string.Format(@"An unexpected SQL error occurred while executing scripts, and the process wasn't completed.
{0} (Line {1}):", ex.FileName.Replace("/", "\\"), ex.LineNumber));
Log(TraceLevel.Error, ex.Message);
return -1;
}
if (!Overwrite)
{
_logger.Log(TraceLevel.Verbose, "Checking if database already exists...");
if (DBHelper.DbExists(ConnectionString))
{
var question = string.Format("{0} {1} already exists - do you want to drop it",
Server, DbName);
if (!ConsoleQuestion.AskYN(question))
{
Console.WriteLine("Create command cancelled.");
return 1;
}
Overwrite = true;
}
}

var createCommand = new CreateCommand
{
ConnectionString = ConnectionString,
DbName = DbName,
Pass = Pass,
ScriptDir = ScriptDir,
Server = Server,
User = User,
Logger = _logger,
Overwrite = Overwrite
};

return 0;
try {
createCommand.Execute(DatabaseFilesPath);
} catch (BatchSqlFileException ex) {
_logger.Log(TraceLevel.Info, Environment.NewLine + "Create completed with the following errors:");
foreach (var e in ex.Exceptions) {
_logger.Log(TraceLevel.Info,
string.Format("- {0} (Line {1}):", e.FileName.Replace("/", "\\"), e.LineNumber));
_logger.Log(TraceLevel.Error, string.Format(" {0}", e.Message));
}
return -1;
} catch (SqlFileException ex) {
_logger.Log(TraceLevel.Info,
Environment.NewLine +
string.Format(@"An unexpected SQL error occurred while executing scripts, and the process wasn't completed.
{0} (Line {1}):", ex.FileName.Replace("/", "\\"), ex.LineNumber));
_logger.Log(TraceLevel.Error, ex.Message);
return -1;
} catch (Exception ex) {
throw new ConsoleHelpAsException(ex.Message);
}
return 0;
}
}
}
120 changes: 0 additions & 120 deletions console/DbCommand.cs

This file was deleted.

Loading

0 comments on commit a1dff4c

Please sign in to comment.