forked from sethreno/schemazen
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved command logic from the console to the library
- Loading branch information
1 parent
015fe8c
commit 8bc6ed0
Showing
17 changed files
with
443 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.