private bool UserConfirmed(DbOptions options) { bool dropConfirmed = options.IsForce; if (!dropConfirmed) { ConsoleLog.Write(ConsoleColor.White, $"{options.DatabaseInfo.DataSource}\\{options.DatabaseInfo.InitialCatalog} database is going to be deleted, are you sure to drop the database"); ConsoleLog.Write(ConsoleColor.Yellow, " [Y/N]? "); var answer = Console.ReadLine(); Console.WriteLine(); if (answer != null && answer.Trim().ToUpper() == "Y") { dropConfirmed = true; } } return(dropConfirmed); }
public static bool TryParse(string[] args, out DbOptions options) { options = null; try { options = Parse(args); return(true); } catch (FormatException formatExp) { ConsoleLog.WriteInfo(formatExp.Message); return(false); } catch (Exception e) { ConsoleLog.WriteError($"An unexpected error happened: {e.Message}"); return(false); } }
private bool Upgrade(DbOptions options) { UpgradeEngine upgradeBuilder = new UpgradeEngineBuilder() .WithDatabase(options.DatabaseInfo) .WithScriptsFromFileSystem(options.UpgradeScriptsPath) .WithPreprocessor(new DefaultScriptPreProcessor(null)) .JournalToTable() .WithDefaultSchema("dbo") .ShowSql(options.ShowSql) .LogScriptOutput(options.IsDebugMode) .WithExecutionTimeout(null) .WithTransaction() .LogToConsole() .Build(_dbProviderFactory); UpgradeResult result = upgradeBuilder.PerformUpgrade(); if (result.Error != null) { Log.Error("Database upgrade failed", result.Error); } return(result.Successful); }
private static int Run(string[] args) { DbOptions options; if (!DbOptions.TryParse(args, out options)) { Log.Debug($"Arguments {args} are not valid"); return(1); } if (!options.ShowHelp) { Log.Debug($"Arguments {args} are valid"); if (!options.IsForce) { ConsoleLog.WriteInfo(options.ToString()); ConsoleLog.Write(ConsoleColor.White, "Would you like to continue with the above parameters "); ConsoleLog.Write(ConsoleColor.Yellow, " [Y/N]? "); var c = Console.ReadLine(); Console.WriteLine(); if (!"Y".Equals(c, StringComparison.OrdinalIgnoreCase)) { ConsoleLog.WriteWarn("DbUp exited as you didn't confirm to continue"); Log.Warn("DbUp didn't run, since user didn't confirm to continue"); return(1); } } return(ResolveDbUpRunner(options.ShowSql).Run(options)); } else { return(1); } }
public static DbOptions Parse(string[] args) { bool showHelp = false, showSql = false, isForce = false, isDebugMode = false; DbProviders providerName = DbProviders.SqlServer; string dataSource = "", initialCatalog = "", username = "", password = ""; string exportfilename = "", upgradepath = ""; var command = DbOptionCommands.None; showHelp = false; var optionSet = new OptionSet { { "d", "Drop existing database", _ => command |= DbOptionCommands.Drop }, { "c", "Create database schema", _ => command |= DbOptionCommands.Create }, { "e", "Export database schema", _ => command |= DbOptionCommands.ExportSchema }, { "u", "Upgrade database", _ => command |= DbOptionCommands.Upgrade }, { "upd=", "Upgrade database with SQL update files, {directory} containing SQL update files", dir => upgradepath = dir }, { "exf=", "Export database schema {file path}", exf => exportfilename = exf }, { "p=", "The database {provider} [sqlserver | sqlite. If not specified the default is sqlserver.", p => { if (p == null) { providerName = DbProviders.SqlServer; } else { Enum.TryParse <DbProviders>(p, out providerName); } } }, { "source=", "The database {source} or server host", s => dataSource = s }, { "catalog=", "The database {catalog} name", d => initialCatalog = d }, { "user="******"Database {user} identity", u => username = u }, { "password="******"Database user {password}", p => password = p }, { "f", "Forces command to run without getting confirmation from user", _ => isForce = true }, { "debug", "Execute DbTool in debug mode", _ => isDebugMode = true }, { "showsql", "Show executed sql commands during dbtool execution", _ => showSql = true }, { "?|h|help", "Show this message and exit", v => showHelp = v != null }, }; optionSet.Parse(args); if (args.Length == 0) { showHelp = true; } DbOptions o = new DbOptions( new DatabaseInfo(providerName, dataSource, initialCatalog, username, password, false), command, exportfilename, upgradepath) { ShowHelp = showHelp, ShowSql = showSql, IsForce = isForce, IsDebugMode = isDebugMode }; if (showHelp) { ConsoleLog.WriteSuccess("Roham DbTool Console Application Usage\r\n"); ConsoleLog.Write(ConsoleColor.White, "DbTool.exe [options]"); ConsoleLog.WriteInfo(""); optionSet.WriteOptionDescriptions(Console.Out); ConsoleLog.Write(ConsoleColor.White, "\r\nExamples:"); ConsoleLog.WriteInfo(@" DbTool -c --source=localhost --catalog=roham-dev --user=sa --password=test Creates a new sqlserver database named roham-dev in localhost server DbTool -c --source=localhost --catalog=roham-dev -e --exf=c:\database\schema.sql Creates a new sqlserver database named roham-dev and export database schema script into c:\database\schema.sql DbTool -u --upd=c:\upgrade\scripts --source=localhost --catalog=roham Runs upgrade scripts located at c:\upgrade\scripts in roham sqlserver database at localhost (via IntegratedSecurity) DbTool -d -c -u --upd:c:\upgrade\scripts --source=localhost --catalog=roham --user=sa --password=test Drops roham sqlserver database from localhost server and creates a new one with same name and then upgrade database with scripts located at c:\upgrade\scripts DbTool -p=sqlite -c -u --upd=c:\upgrade\scripts --source=c:\database\roham.sqlite --debug --showsql Creates a new sqlite database at c:\database\roham.sqlite and then runs upgrade scripts located at c:\upgrade\scripts in debug mode and show executed sql commands "); return(o); } Validate(o); return(o); }