public bool Execute(CreateTaskConfiguration configuration, GusTaskExecutionContext context) { try { var path = (configuration.Name.ToLower().EndsWith(".sql")) ? configuration.Name : configuration.Name + ".sql"; path = Path.GetFullPath(path); var name = Path.GetFileName(path); var directory = Path.GetDirectoryName(path); var timestamp = DateTime.Now.ToString("yyyyMMdd-HHmmss"); var newFileName = string.Format("{0}-{1}", timestamp, name); var newPath = Path.Combine(directory, newFileName); using (var sw = File.CreateText(newPath)) { sw.WriteLine("//"); sw.WriteLine("// Created by Defize Gus."); sw.WriteLine("//"); } } catch (Exception ex) { context.RaiseExecutionEvent(ExecutionEventType.Error, string.Format("Failed to create script file: {0}" , ex.Message)); return false; } return true; }
public bool Execute(ApplyTaskConfiguration configuration, GusTaskExecutionContext context) { var databaseHelper = new DatabaseHelper(context); var fileSystemHelper = new FileSystemHelper(context); var connection = databaseHelper.CreateAndOpenConnection(configuration.Server); if (connection == null) { return false; } var serverConnection = new ServerConnection(connection); var server = new Server(serverConnection); var database = databaseHelper.InitializeDatabase(server, configuration.Database, configuration.CreateDatabaseIfMissing, configuration.CreateDatabaseIfMissing || configuration.CreateManagementSchemaIfMissing); if (database == null) { return false; } context.RaiseExecutionEvent("Determining scripts to apply."); var scriptsToApply = GetScriptsToApply(configuration.SourcePath, database, databaseHelper, fileSystemHelper); if (scriptsToApply == null) { return false; } context.ExecutionStepCount = (uint)scriptsToApply.Count; context.RaiseExecutionEvent(string.Format("Found {0} scripts to apply.", scriptsToApply.Count)); var success = ApplyScripts(scriptsToApply, server, database, databaseHelper, fileSystemHelper, configuration.RecordOnly, context, configuration.HaltOnError); return success; }
private static bool ApplyScripts(IEnumerable<FileInfo> scriptsToApply, Server server, Database database, DatabaseHelper databaseHelper, FileSystemHelper fileSystemHelper, bool recordOnly, GusTaskExecutionContext context, bool haltOnError) { uint index = 0; var hasErrors = false; foreach (var script in scriptsToApply) { ++index; server.ConnectionContext.BeginTransaction(); try { context.RaiseExecutionEvent(ExecutionEventType.Default, string.Format("Applying script '{0}'.", script.Name), index); var hash = fileSystemHelper.GetFileHash(script); if (!recordOnly) { using (var sr = script.OpenText()) { database.ExecuteNonQuery(sr.ReadToEnd()); } } databaseHelper.RecordScript(database, script.Name, hash); server.ConnectionContext.CommitTransaction(); context.RaiseExecutionEvent(ExecutionEventType.Success, string.Format("Applied script '{0}'.", script.Name), index); } catch (FailedOperationException ex) { server.ConnectionContext.RollBackTransaction(); Exception sqlException = FindSqlException(ex); var exceptionMessage = (sqlException ?? ex).Message; context.RaiseExecutionEvent(ExecutionEventType.Error, string.Format("Error applying script '{0}': {1}", script.Name, exceptionMessage), index); if (haltOnError) { return false; } hasErrors = true; } } return !hasErrors; }
public bool Execute(StatusTaskConfiguration configuration, GusTaskExecutionContext context) { var databaseHelper = new DatabaseHelper(context); var fileSystemHelper = new FileSystemHelper(context); var connection = databaseHelper.CreateAndOpenConnection(configuration.Server); if (connection == null) { return false; } var serverConnection = new ServerConnection(connection); var server = new Server(serverConnection); var database = databaseHelper.InitializeDatabase(server, configuration.Database, false, false); if (database == null) { return false; } context.RaiseExecutionEvent("Determining scripts not yet applied."); var scriptsToApply = GetScriptsToApply(configuration.SourcePath, database, databaseHelper, fileSystemHelper); if (scriptsToApply == null) { return false; } context.ExecutionStepCount = (uint)scriptsToApply.Count; context.RaiseExecutionEvent(string.Format("Found {0} scripts not yet applied.", scriptsToApply.Count)); foreach (var script in scriptsToApply) { context.RaiseExecutionEvent(ExecutionEventType.Default, script.Name); } return true; }
public FileSystemHelper(GusTaskExecutionContext context) { _context = context; }
public DatabaseHelper(GusTaskExecutionContext context) { _context = context; }