Path: redb/redb.Tests.Integration/Tests/MsSql/MsSqlIndexCatalogTests.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/MsSqlIndexCatalogTests.cs
----
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using redb.Core.Data;
using redb.Core.Extensions;
using redb.Core.Models;
using redb.Core.Models.Configuration;
using redb.MSSql.Extensions;
using redb.MSSql.Sql;
using redb.Tests.Integration.Tests.Base;
namespace redb.Tests.Integration.Tests.MsSql;
/// MsSqlIndexCatalog.Base against a fresh SQL Server database of this build (see ).
public class MsSqlIndexCatalogTests : IndexCatalogTestsBase
{
internal static readonly string AdminBase =
new ConfigurationBuilder().AddJsonFile("appsettings.json").Build().GetConnectionString("MSSql")!;
protected override async Task CreateDatabaseAsync(string name)
{
await ExecAdminAsync($"CREATE DATABASE [{name}]");
return new SqlConnectionStringBuilder(AdminBase) { InitialCatalog = name }.ConnectionString;
}
protected override async Task DropDatabaseAsync(string name)
{
SqlConnection.ClearAllPools();
await ExecAdminAsync($"IF DB_ID(N'{name}') IS NOT NULL BEGIN ALTER DATABASE [{name}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{name}]; END");
}
internal static async Task ExecAdminAsync(string sql)
{
var cs = new SqlConnectionStringBuilder(AdminBase) { InitialCatalog = "master", Pooling = false }.ConnectionString;
await using var connection = new SqlConnection(cs);
await connection.OpenAsync();
await using var command = new SqlCommand(sql, connection) { CommandTimeout = 300 };
await command.ExecuteNonQueryAsync();
}
protected override void AddRedb(IServiceCollection services, string connectionString, Action configure)
=> services.AddRedb(o => o.UseMsSql(connectionString).Configure(configure));
// SQL Server keeps no statement: it is written from the catalog, keys in order with their direction, the INCLUDE list,
// the filter. The repair appends WITH (ONLINE = ON) where the edition has it.
protected override async Task> ReadCreateStatementsAsync(IRedbContext context)
{
var rows = await context.QueryAsync(
"""
SELECT i.name AS [Name],
'CREATE ' + CASE WHEN i.is_unique = 1 THEN 'UNIQUE ' ELSE '' END + 'NONCLUSTERED INDEX [' + i.name + '] ON [dbo].[' + t.name + '] ('
+ (SELECT STRING_AGG('[' + c.name + ']' + CASE WHEN ic.is_descending_key = 1 THEN ' DESC' ELSE '' END, ', ')
WITHIN GROUP (ORDER BY ic.key_ordinal)
FROM sys.index_columns ic JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
WHERE ic.object_id = i.object_id AND ic.index_id = i.index_id AND ic.is_included_column = 0 AND ic.key_ordinal > 0)
+ ')'
+ ISNULL(' INCLUDE (' + (SELECT STRING_AGG('[' + c.name + ']', ', ') WITHIN GROUP (ORDER BY ic.index_column_id)
FROM sys.index_columns ic JOIN sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
WHERE ic.object_id = i.object_id AND ic.index_id = i.index_id AND ic.is_included_column = 1) + ')', '')
+ CASE WHEN i.has_filter = 1 THEN ' WHERE ' + i.filter_definition ELSE '' END AS [Sql]
FROM sys.indexes i JOIN sys.tables t ON t.object_id = i.object_id
WHERE SCHEMA_NAME(t.schema_id) = 'dbo' AND i.type > 0 AND i.name IS NOT NULL
""");
return rows.ToDictionary(r => r.Name!, r => r.Sql!, StringComparer.OrdinalIgnoreCase);
}
protected override IReadOnlyList Catalog => MsSqlIndexCatalog.Base;
protected override (string File, string Namespace, string Class, string Member, RedbIndexOrigin Origin) Target =>
("redb.MSSql/sql/MsSqlIndexCatalog.g.cs", "redb.MSSql.Sql", "MsSqlIndexCatalog", "Base", RedbIndexOrigin.Base);
protected sealed class SqlRow
{
public string? Name { get; set; }
public string? Sql { get; set; }
}
}
/// The index check and its repair on SQL Server (ONLINE where the edition has it), see .
public class MsSqlIndexRepairTests : IndexRepairTestsBase
{
protected override async Task CreateDatabaseAsync(string name)
{
await MsSqlIndexCatalogTests.ExecAdminAsync($"CREATE DATABASE [{name}]");
return new SqlConnectionStringBuilder(MsSqlIndexCatalogTests.AdminBase) { InitialCatalog = name }.ConnectionString;
}
protected override async Task DropDatabaseAsync(string name)
{
SqlConnection.ClearAllPools();
await MsSqlIndexCatalogTests.ExecAdminAsync(
$"IF DB_ID(N'{name}') IS NOT NULL BEGIN ALTER DATABASE [{name}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{name}]; END");
}
protected override void AddRedb(IServiceCollection services, string connectionString, Action configure)
=> services.AddRedb(o => o.UseMsSql(connectionString).Configure(configure));
protected override string DropIndexSql(string table, string index) => $"DROP INDEX [{index}] ON [dbo].[{table}]";
protected override string CreateOtherIndexSql(string index) => $"CREATE INDEX [{index}] ON [dbo].[_objects] ([_date_create])";
}