From 244a489fb21663d0586b262ac666d18ea7f48471 Mon Sep 17 00:00:00 2001 From: Arthur Bassi Date: Wed, 26 Aug 2026 10:20:14 -0300 Subject: [PATCH] fix(tests): create ephemeral LocalDB database before migration apply --- ...SH221LocationAccountScopeSqlServerTests.cs | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/SeaHavenIndustries.Tests/SH221LocationAccountScopeSqlServerTests.cs b/SeaHavenIndustries.Tests/SH221LocationAccountScopeSqlServerTests.cs index 444575c..917449e 100644 --- a/SeaHavenIndustries.Tests/SH221LocationAccountScopeSqlServerTests.cs +++ b/SeaHavenIndustries.Tests/SH221LocationAccountScopeSqlServerTests.cs @@ -25,6 +25,8 @@ public class SH221LocationAccountScopeSqlServerTests try { + await CreateDatabaseAsync(dbName); + await using var connection = new SqlConnection(connectionString); await connection.OpenAsync(); @@ -147,21 +149,33 @@ public class SH221LocationAccountScopeSqlServerTests } } + private static async Task CreateDatabaseAsync(string dbName) + { + var quoted = QuoteSqlServerIdentifier(dbName); + await using var connection = new SqlConnection(LocalDbMaster); + await connection.OpenAsync(); + await using var command = connection.CreateCommand(); + command.CommandText = $"CREATE DATABASE {quoted};"; + await command.ExecuteNonQueryAsync(); + } + private static async Task DropDatabaseAsync(string dbName) { try { + var quoted = QuoteSqlServerIdentifier(dbName); await using var connection = new SqlConnection(LocalDbMaster); await connection.OpenAsync(); await using var command = connection.CreateCommand(); command.CommandText = $""" - IF DB_ID(N'{dbName}') IS NOT NULL + IF DB_ID(@dbName) IS NOT NULL BEGIN - ALTER DATABASE [{dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; - DROP DATABASE [{dbName}]; + ALTER DATABASE {quoted} SET SINGLE_USER WITH ROLLBACK IMMEDIATE; + DROP DATABASE {quoted}; END """; + command.Parameters.AddWithValue("@dbName", dbName); await command.ExecuteNonQueryAsync(); } catch @@ -169,4 +183,7 @@ public class SH221LocationAccountScopeSqlServerTests // Best-effort cleanup for ephemeral LocalDB databases. } } + + private static string QuoteSqlServerIdentifier(string name) => + "[" + name.Replace("]", "]]", StringComparison.Ordinal) + "]"; }