mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-06 19:22:12 +00:00
fix(tests): create ephemeral LocalDB database before migration apply
This commit is contained in:
parent
2e56ec7678
commit
244a489fb2
1 changed files with 20 additions and 3 deletions
|
|
@ -25,6 +25,8 @@ public class SH221LocationAccountScopeSqlServerTests
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
await CreateDatabaseAsync(dbName);
|
||||||
|
|
||||||
await using var connection = new SqlConnection(connectionString);
|
await using var connection = new SqlConnection(connectionString);
|
||||||
await connection.OpenAsync();
|
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)
|
private static async Task DropDatabaseAsync(string dbName)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var quoted = QuoteSqlServerIdentifier(dbName);
|
||||||
await using var connection = new SqlConnection(LocalDbMaster);
|
await using var connection = new SqlConnection(LocalDbMaster);
|
||||||
await connection.OpenAsync();
|
await connection.OpenAsync();
|
||||||
await using var command = connection.CreateCommand();
|
await using var command = connection.CreateCommand();
|
||||||
command.CommandText =
|
command.CommandText =
|
||||||
$"""
|
$"""
|
||||||
IF DB_ID(N'{dbName}') IS NOT NULL
|
IF DB_ID(@dbName) IS NOT NULL
|
||||||
BEGIN
|
BEGIN
|
||||||
ALTER DATABASE [{dbName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
|
ALTER DATABASE {quoted} SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
|
||||||
DROP DATABASE [{dbName}];
|
DROP DATABASE {quoted};
|
||||||
END
|
END
|
||||||
""";
|
""";
|
||||||
|
command.Parameters.AddWithValue("@dbName", dbName);
|
||||||
await command.ExecuteNonQueryAsync();
|
await command.ExecuteNonQueryAsync();
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
|
|
@ -169,4 +183,7 @@ public class SH221LocationAccountScopeSqlServerTests
|
||||||
// Best-effort cleanup for ephemeral LocalDB databases.
|
// Best-effort cleanup for ephemeral LocalDB databases.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static string QuoteSqlServerIdentifier(string name) =>
|
||||||
|
"[" + name.Replace("]", "]]", StringComparison.Ordinal) + "]";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue