private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { try { log.Remove(0, log.Length); InstallMethod im = new InstallMethod(installer.Create); AsyncCallback cb = new AsyncCallback(InstallCallback); IAsyncResult asyncResult = im.BeginInvoke(schema, cb, im); WaitHandle[] bg = new WaitHandle[] { asyncResult.AsyncWaitHandle }; Progress prog = new Progress(StatusMessage.Running); while (prog.Status != StatusMessage.Exit) { prog = installer.GetProgress(!WaitHandle.WaitAll(bg, 100, true)); backgroundWorker1.ReportProgress(prog.Percent, prog); } } catch (Exception ex) { Progress prog = new Progress(StatusMessage.Exit, 100, ex.Message); backgroundWorker1.ReportProgress(prog.Percent, prog); } }
/// <summary> /// Console application entry point. /// </summary> /// <param name="args">An array of arguments.</param> /// <returns>Zero (0) for success and non-zero for failure.</returns> public static async Task <int> Run(string[] args) { await Task.Delay(1); int exitCode = 0; double spinCycle = Constants.MinSpinTimeout; bool forceWrite = false; Installer installer; Spinner spin = new Spinner(); try { string configPath = string.Empty; // Retain backwards compatability with using just config file as a single parameter. if (args.Length > 0 && !args[0].StartsWith("/", StringComparison.OrdinalIgnoreCase)) { configPath = args[0]; if (!File.Exists(configPath)) { forceWrite = true; } } else { configPath = Path.Combine(Directory.GetCurrentDirectory(), Constants.SQLInstallerXml); } // Allow parameter overrides and/or specifying all parameters from command line. Arguments <Parameters> parms = null; if (File.Exists(configPath)) { parms = new Arguments <Parameters>(args, Parameters.Load(configPath)); } else { parms = new Arguments <Parameters>(args, new Parameters(configPath)); } if (!parms.IsValid) { throw new ApplicationException(parms.ValidationErrors); } if (parms.Instance.WriteConfig || forceWrite) { if (!parms.Instance.IsProtected) { parms.Instance.ProtectConnectionString(); } else { parms.Instance.Write(); } } spin.Start(spinCycle); Console.WriteLine(Resources.StatusConnecting); installer = new Installer(parms.Instance); installer.Prepare(); spin.Stop(); if (installer.Exists && !parms.Instance.Options.Has(Options.Drop)) { if (installer.IsCurrent && !parms.Instance.Options.Has(Options.Retry)) { Console.WriteLine(parms.Instance.Database + Resources.StatusAlreadyUpgraded + installer.Version + Resources.StatusBy + installer.UpgradeBy); return(0); } else { if (!parms.Instance.NoPrompt && !installer.CleanInstall) { ConsoleKey key = ConsoleKey.NoName; while (key != ConsoleKey.N && key != ConsoleKey.Y) { Console.WriteLine(); Console.Write(Resources.AskUpgrade + parms.Instance.Database + Resources.AskToVersion + installer.Upgrade + Resources.AskYesNo); key = Console.ReadKey(true).Key; } Console.WriteLine(key); if (key == ConsoleKey.N) { return(0); } } } } InstallMethod im = new InstallMethod(installer.Create); AsyncCallback cb = new AsyncCallback(InstallCallback); IAsyncResult asyncResult = im.BeginInvoke(cb, im); WaitHandle[] bg = new WaitHandle[] { asyncResult.AsyncWaitHandle }; Progress prog = new Progress(StatusMessage.Running); while (prog.Status != StatusMessage.Exit) { prog = installer.GetProgress(!WaitHandle.WaitAll(bg, 100, true)); switch (prog.Status) { case StatusMessage.Start: Console.WriteLine(prog.Message + Constants.Wait); spin.Start(spinCycle); break; case StatusMessage.Detail: spin.Stop(); Console.WriteLine(prog.Message); spin.Start(spinCycle); break; case StatusMessage.Exit: spin.Stop(); Console.WriteLine(Resources.StatusCompletedWith + prog.Percent + Resources.StatusErrorCount); break; case StatusMessage.Complete: case StatusMessage.Running: case StatusMessage.Progress: default: break; } } if (parms.Instance.Options.HasFlag(Options.ExitCode) && installer.Errors > 0) { exitCode = -1; } } catch (Exception ex) { Console.WriteLine(string.Format(Resources.ErrorGeneric, ex.Message)); exitCode = -1; } finally { if (spin != null) { spin.Stop(); } } return(exitCode); }
static int Main(string[] args) { int returnCode = 0; CommandLine cl = new CommandLine(); Runtime installer; ProviderType provType = ProviderType.SqlServer; Spinner spin = new Spinner(); try { cl.Required.Add("database", "Database name (required)."); cl.Optional.Add("server", "Database server name (default last used)."); cl.Optional.Add("create", "Create database if it does not exist."); cl.Optional.Add("drop", "Drop database if it exists."); cl.Optional.Add("verbose", "Show everything."); cl.Optional.Add("path", "Path to the scripts directory (default last used)."); cl.Optional.Add("type", "Database type [SqlServer|Oracle|PostGres]."); cl.Optional.Add("user", "Database user (if not using integrated security)."); cl.Optional.Add("password", "Database user Password."); cl.Optional.Add("noprompt", "Do not prompt for upgrade."); cl.Optional.Add("retry", "Retry last upgrade (for development only)."); cl.Optional.Add("?", "Help."); cl.Parse(args); if (cl.Parameters.ContainsKey("?")) { System.Console.WriteLine(cl.ShowUsage()); System.Console.WriteLine("E.g. SqlInstaller.exe /database=MyDatabase /server=MyServer /path=\"C:\\SQL Scripts\\MyDatabase\" /verbose"); } else { string database = string.Empty; string server = string.Empty; string path = string.Empty; try { using (RegistryKey installerKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\JHOB Technologies\SQLInstaller")) { if (cl.Parameters.ContainsKey("type")) { if (string.Compare(cl.Parameters["type"], "PostGres", true) == 0) { provType = ProviderType.PostGres; } else if (string.Compare(cl.Parameters["type"], "Oracle", true) == 0) { provType = ProviderType.Oracle; } } if (cl.Parameters.ContainsKey("path")) { path = cl.Parameters["path"]; if (path.Length == 0 || string.Compare(path, "true", true) == 0) { path = "."; } } else { path = installerKey.GetValue("Scripts", ".") as string; } if (cl.Parameters.ContainsKey("server")) { server = cl.Parameters["server"]; } if (server.Length == 0 || string.Compare(server, "true", true) == 0) { server = installerKey.GetValue("Server", "localhost") as string; } if (cl.Parameters.ContainsKey("database")) { database = cl.Parameters["database"]; } installerKey.Close(); } } catch (Exception) { } if (database.Length == 0 || string.Compare(database, "true", true) == 0) { throw new ArgumentException("Missing required parameter: /database"); } RuntimeFlag flags = RuntimeFlag.None; string user = string.Empty; string password = string.Empty; if (cl.Parameters.ContainsKey("user")) { user = cl.Parameters["user"]; } if (cl.Parameters.ContainsKey("password")) { password = cl.Parameters["password"]; } if (!cl.Parameters.ContainsKey("create") || string.Compare(cl.Parameters["create"], "true", true) == 0) { flags |= RuntimeFlag.Create; } if (cl.Parameters.ContainsKey("drop") && string.Compare(cl.Parameters["drop"], "true", true) == 0) { flags |= RuntimeFlag.Drop; } if (cl.Parameters.ContainsKey("retry") && string.Compare(cl.Parameters["retry"], "true", true) == 0) { flags |= RuntimeFlag.Retry; } if (cl.Parameters.ContainsKey("verbose") && string.Compare(cl.Parameters["verbose"], "true", true) == 0) { flags |= RuntimeFlag.Verbose; } spin.Start(250); System.Console.Write("Connecting to " + server + "..."); installer = new Runtime(path, flags); Schema schema = installer.Prepare(provType, server, database, user, password); spin.Stop(); System.Console.WriteLine("Done."); try { using (RegistryKey installerKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\JHOB Technologies\SQLInstaller", true)) { DirectoryInfo di = new DirectoryInfo(path); installerKey.SetValue("Scripts", path, RegistryValueKind.String); installerKey.SetValue("Server", server, RegistryValueKind.String); installerKey.SetValue("Database", database, RegistryValueKind.String); installerKey.Close(); } } catch (Exception) { } if (schema.Exists && (flags & RuntimeFlag.Drop) != RuntimeFlag.Drop) { if (schema.IsCurrent) { System.Console.WriteLine(schema.Provider.Database + " has already been upgraded to " + schema.Version + " by " + schema.UpgradeBy); return(0); } else { if (!cl.Parameters.ContainsKey("noprompt") || cl.Parameters["noprompt"].ToLower().StartsWith("f")) { ConsoleKey key = ConsoleKey.NoName; while (key != ConsoleKey.N && key != ConsoleKey.Y) { System.Console.Write("\rUpgrade " + schema.Provider.Database + " to Version " + schema.Upgrade + " (Y/N)? "); key = System.Console.ReadKey(true).Key; } System.Console.WriteLine(key); if (key == ConsoleKey.N) { return(0); } } } } spin.Start(250); InstallMethod im = new InstallMethod(installer.Create); AsyncCallback cb = new AsyncCallback(InstallCallback); IAsyncResult asyncResult = im.BeginInvoke(schema, cb, im); WaitHandle[] bg = new WaitHandle[] { asyncResult.AsyncWaitHandle }; Progress prog = new Progress(StatusMessage.Running); while (prog.Status != StatusMessage.Exit) { prog = installer.GetProgress(!WaitHandle.WaitAll(bg, 100, true)); switch (prog.Status) { case StatusMessage.Start: System.Console.Write("\r" + prog.Message + "..."); break; case StatusMessage.Complete: case StatusMessage.Detail: System.Console.WriteLine(prog.Message); break; case StatusMessage.Exit: System.Console.WriteLine("Completed with " + prog.Percent + " errors."); returnCode = prog.Percent; break; case StatusMessage.Running: case StatusMessage.Progress: default: break; } } } } catch (Exception ex) { returnCode = -1; System.Console.WriteLine(); System.Console.WriteLine(cl.ShowUsage()); System.Console.WriteLine(ex.Message); } finally { if (spin != null) { spin.Stop(); } } return(returnCode); }