From 3e788e307f7490bc25f980d1b8f7adec5386886a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Stenr=C3=B8jl?= Date: Wed, 8 May 2024 22:09:49 +0200 Subject: [PATCH 1/3] Support specifying collation name --- .../Database/SqlServerDatabaseManager.cs | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/source/TestCommon/source/FunctionApp.TestCommon/Database/SqlServerDatabaseManager.cs b/source/TestCommon/source/FunctionApp.TestCommon/Database/SqlServerDatabaseManager.cs index 2299d7e8d..402b9b891 100644 --- a/source/TestCommon/source/FunctionApp.TestCommon/Database/SqlServerDatabaseManager.cs +++ b/source/TestCommon/source/FunctionApp.TestCommon/Database/SqlServerDatabaseManager.cs @@ -29,28 +29,41 @@ namespace Energinet.DataHub.Core.FunctionApp.TestCommon.Database; public abstract class SqlServerDatabaseManager where TContextImplementation : DbContext { + /// + /// Use this collation when creating a default database for traditional application usage. + /// public const string DefaultCollationName = "SQL_Latin1_General_CP1_CI_AS"; + + /// + /// Use this collation when creating a database for use with the Durable Task SQL Provider. + /// See https://microsoft.github.io/durabletask-mssql/#/quickstart?id=database-setup + /// + public const string DurableTaskCollationName = "Latin1_General_100_BIN2_UTF8"; + private readonly SqlServerConnectionStringProvider _sqlServerConnectionStringProvider; - protected SqlServerDatabaseManager(string prefixForDatabaseName) - : this(prefixForDatabaseName, new SqlServerConnectionStringProvider(RuntimeEnvironment.Default)) + protected SqlServerDatabaseManager(string prefixForDatabaseName, string collationName = DefaultCollationName) + : this(prefixForDatabaseName, new SqlServerConnectionStringProvider(RuntimeEnvironment.Default), collationName) { } protected SqlServerDatabaseManager( string prefixForDatabaseName, - SqlServerConnectionStringProvider sqlServerConnectionStringProvider) + SqlServerConnectionStringProvider sqlServerConnectionStringProvider, + string collationName = DefaultCollationName) { - if (string.IsNullOrWhiteSpace(prefixForDatabaseName)) - { - throw new ArgumentException("Value cannot be null or whitespace.", nameof(prefixForDatabaseName)); - } + ArgumentException.ThrowIfNullOrWhiteSpace(prefixForDatabaseName); + ArgumentException.ThrowIfNullOrWhiteSpace(collationName); _sqlServerConnectionStringProvider = sqlServerConnectionStringProvider; ConnectionString = sqlServerConnectionStringProvider.BuildConnectionStringForDatabaseWithPrefix(prefixForDatabaseName); + + CollationName = collationName; } public string ConnectionString { get; } + public string CollationName { get; } + /// /// IMPORTANT: Dispose the DbContext after use to return the underlying connection to the pool. /// @@ -195,12 +208,12 @@ private static string GetDatabaseName(TContextImplementation context) } /// - /// Create a clean database using SQL, similar to the PowerShell script described in the Development.md file. + /// Create a clean database using SQL. /// - private static string BuildCreateDatabaseCommandText(string databaseName) + private string BuildCreateDatabaseCommandText(string databaseName) { return - $"CREATE DATABASE [{databaseName}] COLLATE {DefaultCollationName};" + + $"CREATE DATABASE [{databaseName}] COLLATE {CollationName};" + $"ALTER DATABASE [{databaseName}] SET READ_COMMITTED_SNAPSHOT ON;"; } } From c7b49c4398a43f4e9eb5710aa705eb5ab09fed34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Stenr=C3=B8jl?= Date: Wed, 8 May 2024 22:09:56 +0200 Subject: [PATCH 2/3] Release notes and version --- source/TestCommon/documents/release-notes/release-notes.md | 6 ++++++ .../FunctionApp.TestCommon/FunctionApp.TestCommon.csproj | 2 +- source/TestCommon/source/TestCommon/TestCommon.csproj | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/source/TestCommon/documents/release-notes/release-notes.md b/source/TestCommon/documents/release-notes/release-notes.md index aaf47182e..4634e3af4 100644 --- a/source/TestCommon/documents/release-notes/release-notes.md +++ b/source/TestCommon/documents/release-notes/release-notes.md @@ -1,5 +1,11 @@ # TestCommon Release notes +## Version 5.2.0 + +- Changes to `SqlServerDatabaseManager`: + - Extended constructor of `SqlServerDatabaseManager` with parameter `collationName` to support the creation of databases with other collation names than the `DefaultCollationName`. + - Added const `DurableTaskCollationName` to allow easy access to the collation name necessary for the Durable Task SQL Provider. + ## Version 5.1.2 - No functional change diff --git a/source/TestCommon/source/FunctionApp.TestCommon/FunctionApp.TestCommon.csproj b/source/TestCommon/source/FunctionApp.TestCommon/FunctionApp.TestCommon.csproj index 2b6c0bd41..12761db87 100644 --- a/source/TestCommon/source/FunctionApp.TestCommon/FunctionApp.TestCommon.csproj +++ b/source/TestCommon/source/FunctionApp.TestCommon/FunctionApp.TestCommon.csproj @@ -31,7 +31,7 @@ limitations under the License. Energinet.DataHub.Core.FunctionApp.TestCommon - 5.1.2$(VersionSuffix) + 5.2.0$(VersionSuffix) FunctionApp TestCommon library Energinet-DataHub Energinet-DataHub diff --git a/source/TestCommon/source/TestCommon/TestCommon.csproj b/source/TestCommon/source/TestCommon/TestCommon.csproj index cd6a898d9..1a779f3d4 100644 --- a/source/TestCommon/source/TestCommon/TestCommon.csproj +++ b/source/TestCommon/source/TestCommon/TestCommon.csproj @@ -31,7 +31,7 @@ limitations under the License. Energinet.DataHub.Core.TestCommon - 5.1.2$(VersionSuffix) + 5.2.0$(VersionSuffix) TestCommon library Energinet-DataHub Energinet-DataHub From 3b47153581faab7b5ac96486791e3ff40b42cbbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Stenr=C3=B8jl?= Date: Wed, 8 May 2024 22:26:00 +0200 Subject: [PATCH 3/3] Tests --- .../Database/SqlServerDatabaseManagerTests.cs | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/source/TestCommon/source/FunctionApp.TestCommon.Tests/Integration/Database/SqlServerDatabaseManagerTests.cs b/source/TestCommon/source/FunctionApp.TestCommon.Tests/Integration/Database/SqlServerDatabaseManagerTests.cs index ae919ce9c..60eed3f37 100644 --- a/source/TestCommon/source/FunctionApp.TestCommon.Tests/Integration/Database/SqlServerDatabaseManagerTests.cs +++ b/source/TestCommon/source/FunctionApp.TestCommon.Tests/Integration/Database/SqlServerDatabaseManagerTests.cs @@ -25,11 +25,12 @@ public class SqlServerDatabaseManagerTests [Collection(nameof(SqlServerDatabaseCollectionFixture))] public class CreateDatabase { - [Fact] - public async Task When_DbContextIsConfigured_Then_DatabaseCanBeCreatedAsync() + [Theory] + [MemberData(nameof(GetCollationName), MemberType = typeof(SqlServerDatabaseManagerTests))] + public async Task When_DbContextIsConfigured_Then_DatabaseCanBeCreatedAsync(string collationName) { // Arrange - var sut = new SqlServerDatabaseManagerSut(); + var sut = new SqlServerDatabaseManagerSut(collationName); // Act var databaseCreated = await sut.CreateDatabaseAsync(); @@ -58,11 +59,12 @@ public async Task When_DatabaseExists_Then_NothingHappensAndDatabaseStillExists_ await context.Database.EnsureDeletedAsync(); } - [Fact] - public void When_DbContextIsConfigured_Then_DatabaseCanBeCreated() + [Theory] + [MemberData(nameof(GetCollationName), MemberType = typeof(SqlServerDatabaseManagerTests))] + public void When_DbContextIsConfigured_Then_DatabaseCanBeCreated(string collationName) { // Arrange - var sut = new SqlServerDatabaseManagerSut(); + var sut = new SqlServerDatabaseManagerSut(collationName); // Act var databaseCreated = sut.CreateDatabase(); @@ -182,6 +184,11 @@ public SqlServerDatabaseManagerSut() { } + public SqlServerDatabaseManagerSut(string collationName) + : base(nameof(SqlServerDatabaseManagerSut), collationName) + { + } + public override ExampleDbContext CreateDbContext() { var optionsBuilder = new DbContextOptionsBuilder(); @@ -189,4 +196,10 @@ public override ExampleDbContext CreateDbContext() return new ExampleDbContext(optionsBuilder.Options); } } + + public static IEnumerable GetCollationName() + { + yield return new object[] { SqlServerDatabaseManager.DefaultCollationName }; + yield return new object[] { SqlServerDatabaseManager.DurableTaskCollationName }; + } }