Skip to content

Commit

Permalink
Moved command logic from the console to the library
Browse files Browse the repository at this point in the history
  • Loading branch information
marcio-santos-zocdoc committed May 10, 2016
1 parent 015fe8c commit 8bc6ed0
Show file tree
Hide file tree
Showing 17 changed files with 443 additions and 244 deletions.
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.Library.Models;
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);
}
}
}
}
95 changes: 60 additions & 35 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 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 8bc6ed0

Please sign in to comment.