Path: redb/redb.Tests.Integration/Tests/MsSql/MsSqlExportRenameHistoryTests.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Tests.Integration/Tests/MsSql/MsSqlExportRenameHistoryTests.cs ---- using Microsoft.Data.SqlClient; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using redb.Core.Extensions; using redb.MSSql.Extensions; using redb.Tests.Integration.Tests.Base; namespace redb.Tests.Integration.Tests.MsSql; /// on SQL Server, scratch databases dropped afterwards. public class MsSqlExportRenameHistoryTests : ExportRenameHistoryTestsBase, IAsyncLifetime { private string _server = null!; private readonly List _databases = []; private readonly List _files = []; protected override string ProviderName => "mssql"; public Task InitializeAsync() { var config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); _server = config.GetConnectionString("MSSql")!; return Task.CompletedTask; } public async Task DisposeAsync() { SqlConnection.ClearAllPools(); foreach (var database in _databases) await ExecuteAsync(Master(), $"IF DB_ID('{database}') IS NOT NULL BEGIN ALTER DATABASE [{database}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{database}] END"); foreach (var file in _files) File.Delete(file); } protected override async Task NewDatabaseAsync(string suffix) { var name = $"redb_export_ren_{Guid.NewGuid().ToString("N")[..12]}_{suffix}"; _databases.Add(name); await ExecuteAsync(Master(), $"CREATE DATABASE [{name}]"); return new SqlConnectionStringBuilder(_server) { InitialCatalog = name }.ConnectionString; } protected override ServiceProvider Build(string cs) { var services = new ServiceCollection(); services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning)); services.AddRedb(options => options .UseMsSql(cs) .Configure(c => c.CacheDomain = $"export-ren-{Guid.NewGuid():N}")); return services.BuildServiceProvider(); } protected override string NewFile() { var file = Path.Combine(Path.GetTempPath(), $"redb-export-ren-{Guid.NewGuid():N}.redb"); _files.Add(file); return file; } protected override async Task ScalarAsync(string cs, string sql) { await using var connection = new SqlConnection(cs); await connection.OpenAsync(); await using var command = new SqlCommand(sql, connection); return AsText(await command.ExecuteScalarAsync()); } protected override async Task> PairsAsync(string cs, string sql) { await using var connection = new SqlConnection(cs); await connection.OpenAsync(); await using var command = new SqlCommand(sql, connection); await using var reader = await command.ExecuteReaderAsync(); var rows = new List<(string?, string?)>(); while (await reader.ReadAsync()) rows.Add((AsText(reader.GetValue(0)), AsText(reader.GetValue(1)))); return rows; } protected override Task ExecuteSqlAsync(string cs, string sql) => ExecuteAsync(cs, sql); private string Master() => new SqlConnectionStringBuilder(_server) { InitialCatalog = "master" }.ConnectionString; private static async Task ExecuteAsync(string cs, string sql) { await using var connection = new SqlConnection(cs); await connection.OpenAsync(); await using var command = new SqlCommand(sql, connection); await command.ExecuteNonQueryAsync(); } }