diff --git a/Respawn/IDbAdapter.cs b/Respawn/IDbAdapter.cs index 5f3c7e3..579cade 100644 --- a/Respawn/IDbAdapter.cs +++ b/Respawn/IDbAdapter.cs @@ -14,9 +14,6 @@ public interface IDbAdapter string BuildReseedSql(IEnumerable tablesToDelete); string BuildTurnOffSystemVersioningCommandText(IEnumerable tablesToTurnOffSystemVersioning); string BuildTurnOnSystemVersioningCommandText(IEnumerable tablesToTurnOnSystemVersioning); - Task CheckSupportsTemporalTables(DbConnection connection) - { - return Task.FromResult(false); - } + Task CheckSupportsTemporalTables(DbConnection connection); } } \ No newline at end of file diff --git a/Respawn/Respawn.csproj b/Respawn/Respawn.csproj index 66de5a5..716cb99 100644 --- a/Respawn/Respawn.csproj +++ b/Respawn/Respawn.csproj @@ -4,7 +4,7 @@ Intelligent resetting for database tests Copyright Jimmy Bogard Jimmy Bogard - netstandard2.1 + netstandard2.0;net6.0 Respawn Respawn tests;integration tests;database tests @@ -26,9 +26,10 @@ + - + diff --git a/Respawn/Respawner.cs b/Respawn/Respawner.cs index 8c35fd3..4e8d790 100644 --- a/Respawn/Respawner.cs +++ b/Respawn/Respawner.cs @@ -36,9 +36,15 @@ public static async Task CreateAsync(string nameOrConnectionString, R throw new ArgumentException("This overload only supports the SqlDataAdapter. To use an alternative adapter, use the overload that supplies a DbConnection.", nameof(options.DbAdapter)); } +#if NETSTANDARD2_0 + using var connection = new SqlConnection(nameOrConnectionString); + + connection.Open(); +#else await using var connection = new SqlConnection(nameOrConnectionString); await connection.OpenAsync(); +#endif var respawner = new Respawner(options); @@ -48,7 +54,7 @@ public static async Task CreateAsync(string nameOrConnectionString, R } /// - /// Creates a based on the supplied connection and options. + /// Creates a based on the supplied connection and options. /// /// Connection object for your target database /// Options @@ -67,9 +73,15 @@ public static async Task CreateAsync(DbConnection connection, Respawn public virtual async Task ResetAsync(string nameOrConnectionString) { +#if NETSTANDARD2_0 + using var connection = new SqlConnection(nameOrConnectionString); + + connection.Open(); +#else await using var connection = new SqlConnection(nameOrConnectionString); await connection.OpenAsync(); +#endif await ResetAsync(connection); } @@ -98,8 +110,13 @@ public virtual async Task ResetAsync(DbConnection connection) private async Task ExecuteAlterSystemVersioningAsync(DbConnection connection, string commandText) { +#if NETSTANDARD2_0 + using var tx = connection.BeginTransaction(); + using var cmd = connection.CreateCommand(); +#else await using var tx = await connection.BeginTransactionAsync(); await using var cmd = connection.CreateCommand(); +#endif cmd.CommandTimeout = Options.CommandTimeout ?? cmd.CommandTimeout; cmd.CommandText = commandText; @@ -107,13 +124,22 @@ private async Task ExecuteAlterSystemVersioningAsync(DbConnection connection, st await cmd.ExecuteNonQueryAsync(); +#if NETSTANDARD2_0 + tx.Commit(); +#else await tx.CommitAsync(); +#endif } private async Task ExecuteDeleteSqlAsync(DbConnection connection) { +#if NETSTANDARD2_0 + using var tx = connection.BeginTransaction(); + using var cmd = connection.CreateCommand(); +#else await using var tx = await connection.BeginTransactionAsync(); await using var cmd = connection.CreateCommand(); +#endif cmd.CommandTimeout = Options.CommandTimeout ?? cmd.CommandTimeout; cmd.CommandText = DeleteSql; @@ -127,7 +153,11 @@ private async Task ExecuteDeleteSqlAsync(DbConnection connection) await cmd.ExecuteNonQueryAsync(); } +#if NETSTANDARD2_0 + tx.Commit(); +#else await tx.CommitAsync(); +#endif } private async Task BuildDeleteTables(DbConnection connection) @@ -158,11 +188,19 @@ private async Task> GetRelationships(DbConnection connecti var relationships = new HashSet(); var commandText = Options.DbAdapter.BuildRelationshipCommandText(Options); +#if NETSTANDARD2_0 + using var cmd = connection.CreateCommand(); + + cmd.CommandText = commandText; + + using var reader = cmd.ExecuteReader(); +#else await using var cmd = connection.CreateCommand(); cmd.CommandText = commandText; await using var reader = await cmd.ExecuteReaderAsync(); +#endif while (await reader.ReadAsync()) { @@ -181,11 +219,19 @@ private async Task> GetAllTables(DbConnection connection) var commandText = Options.DbAdapter.BuildTableCommandText(Options); +#if NETSTANDARD2_0 + using var cmd = connection.CreateCommand(); + + cmd.CommandText = commandText; + + using var reader = cmd.ExecuteReader(); +#else await using var cmd = connection.CreateCommand(); cmd.CommandText = commandText; await using var reader = await cmd.ExecuteReaderAsync(); +#endif while (await reader.ReadAsync()) { @@ -201,11 +247,19 @@ private async Task> GetAllTemporalTables(DbConnection conne var commandText = Options.DbAdapter.BuildTemporalTableCommandText(Options); +#if NETSTANDARD2_0 + using var cmd = connection.CreateCommand(); + + cmd.CommandText = commandText; + + using var reader = cmd.ExecuteReader(); +#else await using var cmd = connection.CreateCommand(); cmd.CommandText = commandText; await using var reader = await cmd.ExecuteReaderAsync(); +#endif while (await reader.ReadAsync()) { diff --git a/Respawn/SqlServerDbAdapter.cs b/Respawn/SqlServerDbAdapter.cs index cd7a930..f1b2a19 100644 --- a/Respawn/SqlServerDbAdapter.cs +++ b/Respawn/SqlServerDbAdapter.cs @@ -317,7 +317,11 @@ public async Task CheckSupportsTemporalTables(DbConnection connection) private static async Task GetEngineEdition(DbConnection connection) { +#if NETSTANDARD2_0 + using var command = connection.CreateCommand(); +#else await using var command = connection.CreateCommand(); +#endif command.CommandText = @" SELECT SERVERPROPERTY('EngineEdition');"; var engineEdition = await command.ExecuteScalarAsync(); @@ -326,7 +330,11 @@ private static async Task GetEngineEdition(DbConnection connection) private static async Task GetCompatibilityLevel(DbConnection connection) { +#if NETSTANDARD2_0 + using var command = connection.CreateCommand(); +#else await using var command = connection.CreateCommand(); +#endif command.CommandText = $@" SELECT compatibility_level FROM sys.databases