/// <summary> /// Drops the database. /// </summary> public void DropDatabase() { var dbRun = new DbRun(ConnectionString, DbName); if (!String.IsNullOrEmpty(DbName)) { dbRun.UpdateDbName(DbName); } dbRun.DropDb(); }
public void UpdateDbVersionProd() { if (CurrentEnvironment != ClickNClaim.Setup.Environment.Prod) { return; } var dbRun = new DbRun(ConnectionString, DbName); if (dbRun.DbVersion == 0) { dbRun.SetDbVersionProd(); } }
/// <summary> /// Updates the database. /// </summary> /// <param name="fromScratch">if set to <c>true</c> [from scratch].</param> public void UpdateDatabase(bool fromScratch = false) { var directoryInfo = new DirectoryInfo(this.GetType().Assembly.Location.Replace("Agantio.Setup.exe", "Scripts\\")); var dbRun = new DbRun(ConnectionString, DbName); var files = directoryInfo.GetFileSystemInfos(); if (fromScratch == false) { int dbVersion = dbRun.DbVersion + 1; files = files.Where(f => Filter(f.Name, dbVersion)).ToArray(); } else { files = files.Where(f => Filter(f.Name, 0)).ToArray(); } var filteredFiles = files.OrderBy(f => OrderedName(f.Name)); foreach (var fileInfo in filteredFiles) { string[] actions; CheckFilename(fileInfo.FullName, out actions); if (StopScriptVersion > 0 && int.Parse(actions[0].Split('\\').Last().Replace("v", "")) > StopScriptVersion) { return; } var result = CanRunScript(fileInfo.Name); if (!result.Item1) { if (result.Item2 == ScriptAction.BadEnvironment) { continue; } if (result.Item2 == ScriptAction.FilenameError) { TracerConsole.Current.TraceError("Mauvais nom de fichier. Ne peut executer le script. Arrêt de la mise à jour de la base."); break; } } TracerConsole.Current.TraceInformation("En cours de traitement: " + fileInfo.Name); using (var file = File.OpenRead(Path.Combine(directoryInfo.FullName, fileInfo.FullName))) { using (var sr = new StreamReader(file, Encoding.Default)) { try { string script = sr.ReadToEnd(); if (!String.IsNullOrEmpty(DbName)) { script = script.Replace("AgantioDb", DbName); } if (fileInfo.Name.StartsWith("v1_")) //create table not supported by transaction { dbRun.RunCommand(script); } else { dbRun.RunCommandTransaction(script); } } catch (ExecutionFailureException e) { var sqlException = e.InnerException as SqlException; if (sqlException != null) { if (sqlException.Number == 2714) //number used for RaisError { TracerConsole.Current.TraceWarning(fileInfo.FullName + "\n" + "DbVersion ne possède pas la bonne version pour executer le script\nDbVersion:" + dbRun.DbVersion); throw e; } } TracerConsole.Current.TraceError(fileInfo.FullName); WriteAllNestedError(e); throw e; } catch (Exception e) { TracerConsole.Current.TraceError(fileInfo.FullName); WriteAllNestedError(e); throw e; } } } } }