Skip to content

Commit

Permalink
Downgrade to "netstandard2.0"
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-carvalho committed May 24, 2024
1 parent 2ad376d commit fab7217
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
5 changes: 1 addition & 4 deletions Respawn/IDbAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public interface IDbAdapter
string BuildReseedSql(IEnumerable<Table> tablesToDelete);
string BuildTurnOffSystemVersioningCommandText(IEnumerable<TemporalTable> tablesToTurnOffSystemVersioning);
string BuildTurnOnSystemVersioningCommandText(IEnumerable<TemporalTable> tablesToTurnOnSystemVersioning);
Task<bool> CheckSupportsTemporalTables(DbConnection connection)
{
return Task.FromResult(false);
}
Task<bool> CheckSupportsTemporalTables(DbConnection connection);
}
}
5 changes: 3 additions & 2 deletions Respawn/Respawn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<Description>Intelligent resetting for database tests</Description>
<Copyright>Copyright Jimmy Bogard</Copyright>
<Authors>Jimmy Bogard</Authors>
<TargetFrameworks>netstandard2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<AssemblyName>Respawn</AssemblyName>
<PackageId>Respawn</PackageId>
<PackageTags>tests;integration tests;database tests</PackageTags>
Expand All @@ -26,9 +26,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" Condition="'$(TargetFramework)' == 'netstandard2.0'"/>
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.0.5" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="MinVer" Version="2.5.0" PrivateAssets="All" />
</ItemGroup>

</Project>
56 changes: 55 additions & 1 deletion Respawn/Respawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@ public static async Task<Respawner> 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);

Expand All @@ -48,7 +54,7 @@ public static async Task<Respawner> CreateAsync(string nameOrConnectionString, R
}

/// <summary>
/// Creates a <see cref="Respawner"/> based on the supplied connection and options.
/// Creates a <see cref="Respawner"/> based on the supplied connection and options.
/// </summary>
/// <param name="connection">Connection object for your target database</param>
/// <param name="options">Options</param>
Expand All @@ -67,9 +73,15 @@ public static async Task<Respawner> 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);
}
Expand Down Expand Up @@ -98,22 +110,36 @@ 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;
cmd.Transaction = tx;

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;
Expand All @@ -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)
Expand Down Expand Up @@ -158,11 +188,19 @@ private async Task<HashSet<Relationship>> GetRelationships(DbConnection connecti
var relationships = new HashSet<Relationship>();
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())
{
Expand All @@ -181,11 +219,19 @@ private async Task<HashSet<Table>> 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())
{
Expand All @@ -201,11 +247,19 @@ private async Task<IList<TemporalTable>> 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())
{
Expand Down
8 changes: 8 additions & 0 deletions Respawn/SqlServerDbAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ public async Task<bool> CheckSupportsTemporalTables(DbConnection connection)

private static async Task<int> 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();
Expand All @@ -326,7 +330,11 @@ private static async Task<int> GetEngineEdition(DbConnection connection)

private static async Task<byte> 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
Expand Down

0 comments on commit fab7217

Please sign in to comment.