internal static List <HistoryItem> GetHistory(string connectionString) { var retval = new List <HistoryItem>(); var settings = new nHydrateSetting(); settings.Load(connectionString); return(settings.History); }
/// <summary> /// Performs an install of a database /// </summary> public override void Install(System.Collections.IDictionary stateSaver) { //base.Install(stateSaver); var commandParams = stateSaver as Dictionary <string, string>; foreach (var action in GetDatabaseActions() .Select(x => Activator.CreateInstance(x) as IDatabaseAction) .OrderBy(x => x.SortOrder) .ToList()) { action.Execute(commandParams); } var paramUICount = 0; var setup = new InstallSetup(); if (commandParams.Count > 0) { if (commandParams.Any(x => PARAMKEYS_TRAN.Contains(x.Key))) { setup.UseTransaction = GetSetting(commandParams, PARAMKEYS_TRAN, true); paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_SKIPNORMALIZE)) { setup.SkipNormalize = true; paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_HASH)) { if (commandParams[PARAMKEYS_HASH].ToLower() == "true" || commandParams[PARAMKEYS_HASH].ToLower() == "1" || commandParams[PARAMKEYS_HASH].ToLower() == string.Empty) { setup.UseHash = true; } else if (commandParams[PARAMKEYS_HASH].ToLower() == "false" || commandParams[PARAMKEYS_HASH].ToLower() == "0") { setup.UseHash = false; } else { throw new Exception("The /" + PARAMKEYS_HASH + " parameter must be set to 'true or false'."); } paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_CHECKONLY)) { setup.CheckOnly = true; paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_VERSIONWARN)) { if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "all") { setup.AcceptVersionWarningsChangedScripts = true; setup.AcceptVersionWarningsNewScripts = true; } else if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "none") { setup.AcceptVersionWarningsChangedScripts = false; setup.AcceptVersionWarningsNewScripts = false; } else if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "new") { setup.AcceptVersionWarningsNewScripts = true; } else if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "changed") { setup.AcceptVersionWarningsChangedScripts = true; } else { throw new Exception("The /" + PARAMKEYS_VERSIONWARN + " parameter must be set to 'all, none, new, or changed'."); } paramUICount++; } if (GetSetting(commandParams, PARAMKEYS_HELP, false)) { ShowHelp(); return; } setup.ConnectionString = GetSetting(commandParams, PARAMKEYS_APPDB, string.Empty); //Determine if calling as a script generator if (commandParams.ContainsKey(PARAMKEYS_SCRIPT)) { var scriptAction = commandParams[PARAMKEYS_SCRIPT].ToLower(); switch (scriptAction) { case "versioned": break; case "unversioned": break; default: throw new Exception("The script action must be 'versioned' or 'unversioned'."); } if (!commandParams.ContainsKey(PARAMKEYS_SCRIPTFILE)) { throw new Exception("The '" + PARAMKEYS_SCRIPTFILE + "' parameter must be set for script generation."); } var dumpFile = commandParams[PARAMKEYS_SCRIPTFILE]; if (!IsValidFileName(dumpFile)) { throw new Exception("The '" + PARAMKEYS_SCRIPTFILE + "' parameter is not valid."); } var fileCreate = true; if (commandParams.ContainsKey(PARAMKEYS_SCRIPTFILEACTION) && (commandParams[PARAMKEYS_SCRIPTFILEACTION] + string.Empty) == "append") { fileCreate = false; } if (File.Exists(dumpFile) && fileCreate) { File.Delete(dumpFile); System.Threading.Thread.Sleep(500); } switch (scriptAction) { case "versioned": if (commandParams.ContainsKey(PARAMKEYS_DBVERSION)) { if (!GeneratedVersion.IsValid(commandParams[PARAMKEYS_DBVERSION])) { throw new Exception("The '" + PARAMKEYS_DBVERSION + "' parameter is not valid."); } setup.Version = new GeneratedVersion(commandParams[PARAMKEYS_DBVERSION]); } else { if (string.IsNullOrEmpty(setup.ConnectionString)) { throw new Exception("Generation of versioned scripts requires a '" + PARAMKEYS_DBVERSION + "' parameter or valid connection string."); } else { var s = new nHydrateSetting(); s.Load(setup.ConnectionString); setup.Version = new GeneratedVersion(s.dbVersion); } } File.AppendAllText(dumpFile, UpgradeInstaller.GetScript(setup)); break; case "unversioned": setup.Version = UpgradeInstaller._def_Version; File.AppendAllText(dumpFile, UpgradeInstaller.GetScript(setup)); break; } return; } //If we processed all parameters and they were UI then we need to show UI if (paramUICount < commandParams.Count) { Install(setup); return; } } Log.Information("Invalid configuration"); }
/// <summary> /// Performs an install of a database /// </summary> public override void Install(System.Collections.IDictionary stateSaver) { base.Install(stateSaver); var commandParams = GetCommandLineParameters(); var paramUICount = 0; var setup = new InstallSetup(); if (commandParams.Count > 0) { if (commandParams.ContainsKey(PARAMKEYS_SHOWSQL)) { if (commandParams[PARAMKEYS_SHOWSQL].ToLower() == "true" || commandParams[PARAMKEYS_SHOWSQL].ToLower() == "1" || commandParams[PARAMKEYS_SHOWSQL].ToLower() == string.Empty) { setup.ShowSql = true; } else if (commandParams[PARAMKEYS_SHOWSQL].ToLower() == "false" || commandParams[PARAMKEYS_SHOWSQL].ToLower() == "0") { setup.ShowSql = false; } else { throw new Exception("The /" + PARAMKEYS_SHOWSQL + " parameter must be set to 'true or false'."); } paramUICount++; } if (commandParams.Any(x => PARAMKEYS_TRAN.Contains(x.Key))) { setup.UseTransaction = GetSetting(commandParams, PARAMKEYS_TRAN, true); paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_SKIPNORMALIZE)) { setup.SkipNormalize = true; paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_HASH)) { if (commandParams[PARAMKEYS_HASH].ToLower() == "true" || commandParams[PARAMKEYS_HASH].ToLower() == "1" || commandParams[PARAMKEYS_HASH].ToLower() == string.Empty) { setup.UseHash = true; } else if (commandParams[PARAMKEYS_HASH].ToLower() == "false" || commandParams[PARAMKEYS_HASH].ToLower() == "0") { setup.UseHash = false; } else { throw new Exception("The /" + PARAMKEYS_HASH + " parameter must be set to 'true or false'."); } paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_CHECKONLY)) { setup.CheckOnly = true; paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_QUIET)) { setup.SuppressUI = true; paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_VERSIONWARN)) { if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "all") { setup.AcceptVersionWarningsChangedScripts = true; setup.AcceptVersionWarningsNewScripts = true; } else if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "none") { setup.AcceptVersionWarningsChangedScripts = false; setup.AcceptVersionWarningsNewScripts = false; } else if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "new") { setup.AcceptVersionWarningsNewScripts = true; } else if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "changed") { setup.AcceptVersionWarningsChangedScripts = true; } else { throw new Exception("The /" + PARAMKEYS_VERSIONWARN + " parameter must be set to 'all, none, new, or changed'."); } paramUICount++; } if (GetSetting(commandParams, PARAMKEYS_HELP, false)) { ShowHelp(); return; } //Try to drop database if (commandParams.Any(x => PARAMKEYS_DROP.Contains(x.Key))) { var masterConnectionString = GetSetting(commandParams, PARAMKEYS_MASTERDB, string.Empty); var dbname = commandParams.Where(x => PARAMKEYS_NEWNAME.Contains(x.Key)).Select(x => x.Value).FirstOrDefault(); if (commandParams.Count == 3 && !string.IsNullOrEmpty(masterConnectionString)) { if (!DropDatabase(dbname, masterConnectionString)) { throw new Exception("The database '" + dbname + "' could not dropped."); } Console.WriteLine("Database successfully dropped."); return; } throw new Exception("Invalid drop database configuration."); } setup.ConnectionString = GetSetting(commandParams, PARAMKEYS_APPDB, string.Empty); setup.MasterConnectionString = GetSetting(commandParams, PARAMKEYS_MASTERDB, string.Empty); if (GetSetting(commandParams, PARAMKEYS_UPGRADE, setup.IsUpgrade)) { setup.InstallStatus = InstallStatusConstants.Upgrade; } if (commandParams.Any(x => PARAMKEYS_CREATE.Contains(x.Key))) { setup.InstallStatus = InstallStatusConstants.Create; } if (commandParams.Any(x => PARAMKEYS_UPGRADE.Contains(x.Key)) && commandParams.Any(x => PARAMKEYS_CREATE.Contains(x.Key))) { throw new Exception("You cannot specify both the create and update action."); } if (commandParams.Count(x => PARAMKEYS_NEWNAME.Contains(x.Key)) > 1) { throw new Exception("The new database name was specified more than once."); } if (commandParams.Count(x => PARAMKEYS_MASTERDB.Contains(x.Key)) > 1) { throw new Exception("The master database connection string was specified more than once."); } if (commandParams.Count(x => PARAMKEYS_APPDB.Contains(x.Key)) > 1) { throw new Exception("The connection string was specified more than once."); } //Determine if calling as a script generator if (commandParams.ContainsKey(PARAMKEYS_SCRIPT)) { var scriptAction = commandParams[PARAMKEYS_SCRIPT].ToLower(); switch (scriptAction) { case "versioned": case "unversioned": case "create": break; default: throw new Exception("The script action must be 'create', 'versioned', or 'unversioned'."); } if (!commandParams.ContainsKey(PARAMKEYS_SCRIPTFILE)) { throw new Exception("The '" + PARAMKEYS_SCRIPTFILE + "' parameter must be set for script generation."); } var dumpFile = commandParams[PARAMKEYS_SCRIPTFILE]; if (!IsValidFileName(dumpFile)) { throw new Exception("The '" + PARAMKEYS_SCRIPTFILE + "' parameter is not valid."); } var fileCreate = true; if (commandParams.ContainsKey(PARAMKEYS_SCRIPTFILEACTION) && (commandParams[PARAMKEYS_SCRIPTFILEACTION] + string.Empty) == "append") { fileCreate = false; } if (File.Exists(dumpFile) && fileCreate) { File.Delete(dumpFile); System.Threading.Thread.Sleep(500); } switch (scriptAction) { case "versioned": if (commandParams.ContainsKey(PARAMKEYS_DBVERSION)) { if (!GeneratedVersion.IsValid(commandParams[PARAMKEYS_DBVERSION])) { throw new Exception("The '" + PARAMKEYS_DBVERSION + "' parameter is not valid."); } setup.Version = new GeneratedVersion(commandParams[PARAMKEYS_DBVERSION]); } else { if (string.IsNullOrEmpty(setup.ConnectionString)) { throw new Exception("Generation of versioned scripts requires a '" + PARAMKEYS_DBVERSION + "' parameter or valid connection string."); } else { var s = new nHydrateSetting(); s.Load(setup.ConnectionString); setup.Version = new GeneratedVersion(s.dbVersion); } } Console.WriteLine("Generate Script Started"); setup.InstallStatus = InstallStatusConstants.Upgrade; File.AppendAllText(dumpFile, UpgradeInstaller.GetScript(setup)); Console.WriteLine("Generated Create Script"); break; case "unversioned": Console.WriteLine("Generate Script Started"); setup.InstallStatus = InstallStatusConstants.Upgrade; setup.Version = UpgradeInstaller._def_Version; File.AppendAllText(dumpFile, UpgradeInstaller.GetScript(setup)); Console.WriteLine("Generated Create Script"); break; case "create": Console.WriteLine("Generate Script Started"); setup.InstallStatus = InstallStatusConstants.Create; setup.Version = new GeneratedVersion(-1, -1, -1, -1, -1); File.AppendAllText(dumpFile, UpgradeInstaller.GetScript(setup)); Console.WriteLine("Generated Create Script"); break; } return; } //If we processed all parameters and they were UI then we need to show UI if ((paramUICount < commandParams.Count) || setup.SuppressUI) { setup.NewDatabaseName = commandParams.Where(x => PARAMKEYS_NEWNAME.Contains(x.Key)).Select(x => x.Value).FirstOrDefault(); Install(setup); return; } } UIInstall(setup); }
internal static string GetDatabaseMetaProperty(string connectionString, string propertyName) { if (CanUseExtendedProperty(connectionString)) { return SelectExtendedProperty(connectionString, propertyName, string.Empty, string.Empty, string.Empty); } else { var settings = new nHydrateSetting(); settings.Load(connectionString); switch (propertyName) { case "dbVersion": return settings.dbVersion; case "LastUpdate": return settings.LastUpdate.ToString("yyyy-MM-dd HH:mm:ss"); case "ModelKey": return settings.ModelKey.ToString(); case "History": return settings.ToHistoryString(); default: throw new Exception("No property found!"); } } }
internal static void UpdateDatabaseMetaProperty(string connectionString, string propertyName, string propertyValue) { var conn = new System.Data.SqlClient.SqlConnection(); try { var settings = new nHydrateSetting(); settings.Load(connectionString); switch (propertyName) { case "dbVersion": settings.dbVersion = propertyValue; break; case "LastUpdate": settings.LastUpdate = DateTime.Parse(propertyValue); break; case "ModelKey": settings.ModelKey = new Guid(propertyValue); break; case "History": settings.LoadHistory(propertyValue); break; default: throw new Exception("No property found!"); } settings.Save(connectionString); } catch (Exception ex) { throw; } finally { if (conn != null) conn.Close(); } //We no longer use extended properties if (CanUseExtendedProperty(connectionString)) { if (ExtendedPropertyExists(connectionString, propertyName, string.Empty, string.Empty, string.Empty)) { //UpdateExtendedProperty(connectionString, propertyName, propertyValue, string.Empty, string.Empty, string.Empty); DeleteExtendedProperty(connectionString, propertyName); } else { //InsertExtendedPropery(connectionString, propertyName, propertyValue, string.Empty, string.Empty, string.Empty); } } }
internal static List<HistoryItem> GetHistory(string connectionString) { var retval = new List<HistoryItem>(); var settings = new nHydrateSetting(); if (CanUseExtendedProperty(connectionString)) { var historyRow = SqlServers.SelectExtendedProperty(connectionString, "History", string.Empty, string.Empty, string.Empty); settings.LoadHistory(historyRow); if (settings.History.Count > 0) return settings.History; } if (settings.History.Count == 0) { settings = new nHydrateSetting(); settings.Load(connectionString); } return settings.History; }
/// <summary> /// Performs an install of a database /// </summary> public override void Install(System.Collections.IDictionary stateSaver) { base.Install(stateSaver); var commandParams = GetCommandLineParameters(); var paramUICount = 0; var setup = new InstallSetup(); if (commandParams.Count > 0) { if (commandParams.ContainsKey(PARAMKEYS_SHOWSQL)) { if (commandParams[PARAMKEYS_SHOWSQL].ToLower() == "true" || commandParams[PARAMKEYS_SHOWSQL].ToLower() == "1" || commandParams[PARAMKEYS_SHOWSQL].ToLower() == string.Empty) setup.ShowSql = true; else if (commandParams[PARAMKEYS_SHOWSQL].ToLower() == "false" || commandParams[PARAMKEYS_SHOWSQL].ToLower() == "0") setup.ShowSql = false; else throw new Exception("The /" + PARAMKEYS_SHOWSQL + " parameter must be set to 'true or false'."); paramUICount++; } if (commandParams.Any(x => PARAMKEYS_TRAN.Contains(x.Key))) { setup.UseTransaction = GetSetting(commandParams, PARAMKEYS_TRAN, true); paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_SKIPNORMALIZE)) { setup.SkipNormalize = true; paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_HASH)) { if (commandParams[PARAMKEYS_HASH].ToLower() == "true" || commandParams[PARAMKEYS_HASH].ToLower() == "1" || commandParams[PARAMKEYS_HASH].ToLower() == string.Empty) setup.UseHash = true; else if (commandParams[PARAMKEYS_HASH].ToLower() == "false" || commandParams[PARAMKEYS_HASH].ToLower() == "0") setup.UseHash = false; else throw new Exception("The /" + PARAMKEYS_HASH + " parameter must be set to 'true or false'."); paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_CHECKONLY)) { setup.CheckOnly = true; paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_QUIET)) { setup.SuppressUI = true; paramUICount++; } if (commandParams.ContainsKey(PARAMKEYS_VERSIONWARN)) { if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "all") { setup.AcceptVersionWarningsChangedScripts = true; setup.AcceptVersionWarningsNewScripts = true; } else if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "none") { setup.AcceptVersionWarningsChangedScripts = false; setup.AcceptVersionWarningsNewScripts = false; } else if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "new") { setup.AcceptVersionWarningsNewScripts = true; } else if (commandParams[PARAMKEYS_VERSIONWARN].ToLower() == "changed") { setup.AcceptVersionWarningsChangedScripts = true; } else { throw new Exception("The /" + PARAMKEYS_VERSIONWARN + " parameter must be set to 'all, none, new, or changed'."); } paramUICount++; } if (GetSetting(commandParams, PARAMKEYS_HELP, false)) { ShowHelp(); return; } //Try to drop database if (commandParams.Any(x => PARAMKEYS_DROP.Contains(x.Key))) { var masterConnectionString = GetSetting(commandParams, PARAMKEYS_MASTERDB, string.Empty); var dbname = commandParams.Where(x => PARAMKEYS_NEWNAME.Contains(x.Key)).Select(x => x.Value).FirstOrDefault(); if (commandParams.Count == 3 && !string.IsNullOrEmpty(masterConnectionString)) { if (!DropDatabase(dbname, masterConnectionString)) throw new Exception("The database '" + dbname + "' could not dropped."); Console.WriteLine("Database successfully dropped."); return; } throw new Exception("Invalid drop database configuration."); } setup.ConnectionString = GetSetting(commandParams, PARAMKEYS_APPDB, string.Empty); setup.MasterConnectionString = GetSetting(commandParams, PARAMKEYS_MASTERDB, string.Empty); if (GetSetting(commandParams, PARAMKEYS_UPGRADE, setup.IsUpgrade)) setup.InstallStatus = InstallStatusConstants.Upgrade; if (commandParams.Any(x => PARAMKEYS_CREATE.Contains(x.Key))) setup.InstallStatus = InstallStatusConstants.Create; if (commandParams.Any(x => PARAMKEYS_UPGRADE.Contains(x.Key)) && commandParams.Any(x => PARAMKEYS_CREATE.Contains(x.Key))) throw new Exception("You cannot specify both the create and update action."); if (commandParams.Count(x => PARAMKEYS_NEWNAME.Contains(x.Key)) > 1) throw new Exception("The new database name was specified more than once."); if (commandParams.Count(x => PARAMKEYS_MASTERDB.Contains(x.Key)) > 1) throw new Exception("The master database connection string was specified more than once."); if (commandParams.Count(x => PARAMKEYS_APPDB.Contains(x.Key)) > 1) throw new Exception("The connection string was specified more than once."); //Determine if calling as a script generator if (commandParams.ContainsKey(PARAMKEYS_SCRIPT)) { var scriptAction = commandParams[PARAMKEYS_SCRIPT].ToLower(); switch (scriptAction) { case "versioned": case "unversioned": case "create": break; default: throw new Exception("The script action must be 'create', 'versioned', or 'unversioned'."); } if (!commandParams.ContainsKey(PARAMKEYS_SCRIPTFILE)) throw new Exception("The '" + PARAMKEYS_SCRIPTFILE + "' parameter must be set for script generation."); var dumpFile = commandParams[PARAMKEYS_SCRIPTFILE]; if (!IsValidFileName(dumpFile)) throw new Exception("The '" + PARAMKEYS_SCRIPTFILE + "' parameter is not valid."); var fileCreate = true; if (commandParams.ContainsKey(PARAMKEYS_SCRIPTFILEACTION) && (commandParams[PARAMKEYS_SCRIPTFILEACTION] + string.Empty) == "append") fileCreate = false; if (File.Exists(dumpFile) && fileCreate) { File.Delete(dumpFile); System.Threading.Thread.Sleep(500); } switch (scriptAction) { case "versioned": if (commandParams.ContainsKey(PARAMKEYS_DBVERSION)) { if (!GeneratedVersion.IsValid(commandParams[PARAMKEYS_DBVERSION])) throw new Exception("The '" + PARAMKEYS_DBVERSION + "' parameter is not valid."); setup.Version = new GeneratedVersion(commandParams[PARAMKEYS_DBVERSION]); } else { if (string.IsNullOrEmpty(setup.ConnectionString)) throw new Exception("Generation of versioned scripts requires a '" + PARAMKEYS_DBVERSION + "' parameter or valid connection string."); else { var s = new nHydrateSetting(); s.Load(setup.ConnectionString); setup.Version = new GeneratedVersion(s.dbVersion); } } Console.WriteLine("Generate Script Started"); setup.InstallStatus = InstallStatusConstants.Upgrade; File.AppendAllText(dumpFile, UpgradeInstaller.GetScript(setup)); Console.WriteLine("Generated Create Script"); break; case "unversioned": Console.WriteLine("Generate Script Started"); setup.InstallStatus = InstallStatusConstants.Upgrade; setup.Version = UpgradeInstaller._def_Version; File.AppendAllText(dumpFile, UpgradeInstaller.GetScript(setup)); Console.WriteLine("Generated Create Script"); break; case "create": Console.WriteLine("Generate Script Started"); setup.InstallStatus = InstallStatusConstants.Create; setup.Version = new GeneratedVersion(-1, -1, -1, -1, -1); File.AppendAllText(dumpFile, UpgradeInstaller.GetScript(setup)); Console.WriteLine("Generated Create Script"); break; } return; } //If we processed all parameters and they were UI then we need to show UI if ((paramUICount < commandParams.Count) || setup.SuppressUI) { setup.NewDatabaseName = commandParams.Where(x => PARAMKEYS_NEWNAME.Contains(x.Key)).Select(x => x.Value).FirstOrDefault(); Install(setup); return; } } UIInstall(setup); }