Skip to content

Commit

Permalink
Merge pull request #482 from serilog-mssql/478-autocreatedb-no-except…
Browse files Browse the repository at this point in the history
…ion-if-db-exists

478 autocreatedb no exception if db exists
  • Loading branch information
ckadluba authored Aug 10, 2023
2 parents 9f28d00 + 12c265e commit 076e0e2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void Execute()
catch (Exception ex)
{
HandleException(ex);
throw;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Text;
using static System.FormattableString;

namespace Serilog.Sinks.MSSqlServer.Platform
Expand All @@ -15,6 +16,15 @@ public SqlCreateDatabaseWriter(string databaseName)
public string DatabaseName => _databaseName;

public string GetSql()
=> Invariant($"CREATE DATABASE [{_databaseName}]");
{
var sql = new StringBuilder();

sql.AppendLine(Invariant($"IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = '{_databaseName}')"));
sql.AppendLine("BEGIN");
sql.AppendLine(Invariant($"CREATE DATABASE [{_databaseName}]"));
sql.AppendLine("END");

return sql.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void HandlesExceptionFromSqlWriter()
};

// Act
_sut.Execute();
Assert.Throws<InvalidOperationException>(() => _sut.Execute());

// Assert
Assert.True(handlerCalled);
Expand All @@ -124,7 +124,7 @@ public void HandlesExceptionFromConnectionFactory()
};

// Act
_sut.Execute();
Assert.Throws<InvalidOperationException>(() => _sut.Execute());

// Assert
Assert.True(handlerCalled);
Expand All @@ -148,7 +148,7 @@ public void HandlesExceptionFromConnection()
};

// Act
_sut.Execute();
Assert.Throws<InvalidOperationException>(() => _sut.Execute());

// Assert
Assert.True(handlerCalled);
Expand All @@ -172,7 +172,7 @@ public void HandlesExceptionFromCommand()
};

// Act
_sut.Execute();
Assert.Throws<InvalidOperationException>(() => _sut.Execute());

// Assert
Assert.True(handlerCalled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void GetSqlWritesCorrectCommand()
{
// Arrange
const string databaseName = "LogDatabase";
const string expectedResult = "CREATE DATABASE [LogDatabase]";
const string expectedResult = "IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'LogDatabase')\r\nBEGIN\r\nCREATE DATABASE [LogDatabase]\r\nEND\r\n";
var sut = new SqlCreateDatabaseWriter(databaseName);

// Act
Expand All @@ -27,7 +27,7 @@ public void GetSqlWritesCorrectCommandForDatabaseNameWithSpaces()
{
// Arrange
const string databaseName = "Log Data Base";
const string expectedResult = "CREATE DATABASE [Log Data Base]";
const string expectedResult = "IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = 'Log Data Base')\r\nBEGIN\r\nCREATE DATABASE [Log Data Base]\r\nEND\r\n";
var sut = new SqlCreateDatabaseWriter(databaseName);

// Act
Expand Down

0 comments on commit 076e0e2

Please sign in to comment.