protected override void DoExecute() { string executedScriptName = null; try { List <DbScriptToRun> scriptPathsToRunList = _scriptPathsToRunEnumerable.ToList(); if (scriptPathsToRunList.Count <= 0) { PostDiagnosticMessage("There are no scripts to run.", DiagnosticMessageType.Info); } else { PostDiagnosticMessage( string.Format("Will run '{0}' scripts.", scriptPathsToRunList.Count), DiagnosticMessageType.Info); foreach (DbScriptToRun scriptPathToRun in scriptPathsToRunList) { executedScriptName = Path.GetFileName(scriptPathToRun.ScriptPath); using (var sr = new StreamReader(scriptPathToRun.ScriptPath)) { string script = sr.ReadToEnd(); if (ShouldBeVersionInsertPresent(executedScriptName) && !DbScriptToRun.IsVersionInsertPresent(scriptPathToRun.DbVersion, script)) { throw new DeploymentTaskException(string.Format("Script {0} that should be run does not have necessary version insert.", scriptPathToRun.DbVersion)); } _dbScriptRunner.Execute(script); PostDiagnosticMessage("Script executed successfully: " + executedScriptName, DiagnosticMessageType.Info); } } } } catch (DbScriptRunnerException exc) { string message = string.Format("Script execution failed, script name: '{0}'. {1}.", executedScriptName, exc.Message); Exception eLocal = exc.InnerException; while (eLocal != null) { PostDiagnosticMessage("Inner exception: " + eLocal.Message, DiagnosticMessageType.Error); eLocal = eLocal.InnerException; } throw new DeploymentTaskException(message, exc); } }
public void build_number_is_checked_positive() { const string scriptWithVersionInsert = "INSERT \n\r INTO \n\r [VERSION] VALUES('1.4.1.2')"; Assert.IsTrue(DbScriptToRun.IsVersionInsertPresent(new DbVersion(1, 4, 1, 2), scriptWithVersionInsert)); }
public void revision_number_is_checked() { const string scriptWithVersionInsert = "INSERT \n\r INTO \n\r [VERSION] VALUES('1.4')"; Assert.IsFalse(DbScriptToRun.IsVersionInsertPresent(new DbVersion(1, 4, 1), scriptWithVersionInsert)); }
public void correct_version_insert_split_into_lines_succeeds() { const string scriptWithVersionInsert = "INSERT \n\r INTO \n\r [VERSION] VALUES('1.5.0.0')"; Assert.IsTrue(DbScriptToRun.IsVersionInsertPresent(new DbVersion(1, 5), scriptWithVersionInsert)); }
public void wrong_version_insert_fails() { const string scriptWithWrongVersionInsert = "INSERT INTO VERSION VALUES('1.5.0.0')"; Assert.IsFalse(DbScriptToRun.IsVersionInsertPresent(new DbVersion(1, 4), scriptWithWrongVersionInsert)); }
public void missing_version_insert_fails() { const string scriptWithMissingVersionInsert = "USE Rahl DROP Stuff"; Assert.IsFalse(DbScriptToRun.IsVersionInsertPresent(new DbVersion(1, 0), scriptWithMissingVersionInsert)); }