Skip to content

Commit

Permalink
Merge pull request #230 from eKipod/master
Browse files Browse the repository at this point in the history
Fix issues with formatting exceptions.
  • Loading branch information
droyad committed Sep 7, 2017
2 parents e01f154 + 0507ebe commit f57f069
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/DbUp.Tests/Support/SqlServer/SqlScriptExecutorTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using DbUp.Engine;
using DbUp.Engine.Output;
using DbUp.Engine.Transactions;
Expand Down Expand Up @@ -206,5 +207,28 @@ public void logs_output_when_configured_to()
command.DidNotReceive().ExecuteNonQuery();
Assert.AreEqual("create [foo].Table", command.CommandText);
}

[Test]
public void logs_when_dbexception()
{
var dbConnection = Substitute.For<IDbConnection>();
var command = Substitute.For<IDbCommand>();
command.When(x => x.ExecuteNonQuery()).Do(x =>
{
var ex = Substitute.For<DbException>();
ex.ErrorCode.Returns(1);
ex.Message.Returns("Message with curly braces {0}");
throw ex;
});
dbConnection.CreateCommand().Returns(command);
var logger = Substitute.For<IUpgradeLog>();
logger.WhenForAnyArgs(x => x.WriteError(null, null)).Do(x => Console.WriteLine(x.Arg<string>(), x.Arg<object[]>()));

var executor = new SqlScriptExecutor(() => new TestConnectionManager(dbConnection, true), () => logger, null, () => true, null);

Assert.That(() => executor.Execute(new SqlScript("Test", "create $schema$.Table")), Throws.InstanceOf(typeof(DbException)));
command.Received().ExecuteNonQuery();
logger.ReceivedWithAnyArgs().WriteError(Arg.Any<string>(), Arg.Any<object[]>());
}
}
}
2 changes: 1 addition & 1 deletion src/DbUp.Tests/UpgradeDatabaseScenarios.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void AndErrorMessageShouldBeLogged()
"SQL logic error or missing database\r\n" +
"near \"slect\": syntax error");

log.Received().WriteError(Arg.Is<string>(s => s.StartsWith("System.Data.SQLite.SQLiteException (0x80004005): SQL logic error or missing database")));
log.Received().WriteError("{0}", Arg.Is<string>(s => s.StartsWith("System.Data.SQLite.SQLiteException (0x80004005): SQL logic error or missing database")));
log.Received().WriteError(
Arg.Is<string>(s => s.StartsWith("Upgrade failed due to an unexpected exception:")),
Arg.Is<string>(s => s.Contains("System.Data.SQLite.SQLiteException")));
Expand Down
2 changes: 1 addition & 1 deletion src/DbUp/Support/SqlServer/SqlConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public SqlConnectionManager(string connectionString)
var conn = new SqlConnection(connectionString);
if (dbManager.IsScriptOutputLogged)
conn.InfoMessage += (sender, e) => log.WriteInformation(e.Message + "\r\n");
conn.InfoMessage += (sender, e) => log.WriteInformation("{0}\r\n", e.Message);
return conn;
}))
Expand Down
8 changes: 4 additions & 4 deletions src/DbUp/Support/SqlServer/SqlScriptExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,21 @@ public virtual void Execute(SqlScript script, IDictionary<string, string> variab
catch (SqlException sqlException)
{
log().WriteInformation("SQL exception has occured in script: '{0}'", script.Name);
log().WriteError("Script block number: {0}; Block line {1}; Message: {2}", index, sqlException.LineNumber, sqlException.Procedure, sqlException.Number, sqlException.Message);
log().WriteError(sqlException.ToString());
log().WriteError("Script block number: {0}; Block line {1}; Procedure: {2}; Message Number: {3}; Message: {4}", index, sqlException.LineNumber, sqlException.Procedure, sqlException.Number, sqlException.Message);
log().WriteError("{0}", sqlException.ToString());
throw;
}
catch (DbException sqlException)
{
log().WriteInformation("DB exception has occured in script: '{0}'", script.Name);
log().WriteError("Script block number: {0}; Error code {1}; Message: {2}", index, sqlException.ErrorCode, sqlException.Message);
log().WriteError(sqlException.ToString());
log().WriteError("{0}", sqlException.ToString());
throw;
}
catch (Exception ex)
{
log().WriteInformation("Exception has occured in script: '{0}'", script.Name);
log().WriteError(ex.ToString());
log().WriteError("{0}", ex.ToString());
throw;
}
}
Expand Down

0 comments on commit f57f069

Please sign in to comment.