Path: redb-route/tests/redb.Route.Tests.Sql/E2E/Infrastructure/SqlE2EDatabase.cs Snapshot: redb-route, 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-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/tests/redb.Route.Tests.Sql/E2E/Infrastructure/SqlE2EDatabase.cs ---- using System.Data.Common; using System.Net.Sockets; using redb.Route.Sql.Connection; namespace redb.Route.Tests.Sql.E2E.Infrastructure; /// /// A uniquely named table on one provider for the lifetime of a test. The three target frameworks run the /// suites in parallel against the same servers, so tests share nothing but the server itself. The table is /// dropped on dispose; a failing drop is not swallowed. /// public sealed class SqlE2EDatabase : IAsyncDisposable { private bool _created; private SqlE2EDatabase(SqlE2EProvider provider) { Provider = provider; Table = "rsql_" + Guid.NewGuid().ToString("N")[..12]; } /// The provider this table lives on. public SqlE2EProvider Provider { get; } /// Unique table name. public string Table { get; } /// Creates the table with the provider's default DDL, or with . public static async Task CreateAsync(SqlE2EProvider provider, Func? createTableSql = null) { var database = new SqlE2EDatabase(provider); await database.ExecuteAsync((createTableSql ?? provider.CreateTableSql)(database.Table)); database._created = true; return database; } /// Opens a connection with the provider's session settings applied. /// The server is not reachable; the message says how to start it. public async Task OpenAsync(CancellationToken ct = default) { var connection = Provider.Factory.CreateConnection() ?? throw new InvalidOperationException($"{Provider.Factory.GetType().Name}.CreateConnection() returned null."); var ready = false; try { connection.ConnectionString = Provider.ConnectionString; try { await connection.OpenAsync(ct); } // Inside an ambient transaction the open also enlists, and an enlistment failure (a promotion to a // distributed transaction above all) is the outcome a test looks at, not an unreachable server. catch (Exception ex) when ((ex is DbException or SocketException or TimeoutException or InvalidOperationException) && System.Transactions.Transaction.Current is null) { throw new InvalidOperationException( $"SQL E2E: provider '{Provider.Name}' is not reachable. {Provider.StartHint}", ex); } if (Provider.SessionInitSql is { } init) await ExecuteAsync(connection, null, init, ct); ready = true; return connection; } finally { if (!ready) await connection.DisposeAsync(); } } /// Connection factory for the connector: every connection it hands out is opened by . public RecordingConnectionFactory CreateConnectionFactory() => new(this); /// Executes a non-query on an open connection. public static async Task ExecuteAsync(DbConnection connection, DbTransaction? transaction, string sql, CancellationToken ct = default) { await using var command = connection.CreateCommand(); command.CommandText = sql; command.Transaction = transaction; return await command.ExecuteNonQueryAsync(ct); } /// Executes a non-query on a fresh connection. public async Task ExecuteAsync(string sql) { await using var connection = await OpenAsync(); await ExecuteAsync(connection, null, sql); } /// Ids in the table, ascending, read on a fresh connection — what is actually persisted. public async Task ReadIdsAsync() { var rows = await QueryAsync($"SELECT id FROM {Table} ORDER BY id"); return rows.Select(r => Convert.ToInt32(r[0])).ToArray(); } /// All rows of a query on a fresh connection; becomes null. public async Task> QueryAsync(string sql) { await using var connection = await OpenAsync(); await using var command = connection.CreateCommand(); command.CommandText = sql; var rows = new List(); await using var reader = await command.ExecuteReaderAsync(); while (await reader.ReadAsync()) { var row = new object?[reader.FieldCount]; for (var i = 0; i < row.Length; i++) row[i] = reader.IsDBNull(i) ? null : reader.GetValue(i); rows.Add(row); } return rows; } /// public async ValueTask DisposeAsync() { if (_created) await ExecuteAsync($"DROP TABLE {Table}"); } }