Skip to content

Commit

Permalink
Merge pull request #123 from jbogard/exception-for-non-sql
Browse files Browse the repository at this point in the history
Only allowing SQL adapter for the overload that takes a connection st…
  • Loading branch information
jbogard authored Jul 7, 2023
2 parents dc07249 + 59f851b commit 6bb39d4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
- name: Start SQL Local DB
run: sqllocaldb start mssqllocaldb
- name: Build and Test
run: ./Build.ps1
shell: pwsh
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
- name: Start SQL Local DB
run: sqllocaldb start mssqllocaldb
- name: Build and Test
run: ./Build.ps1
shell: pwsh
Expand Down
18 changes: 18 additions & 0 deletions Respawn.UnitTests/RespawnerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Threading.Tasks;
using Shouldly;
using Xunit;

namespace Respawn.UnitTests;

public class RespawnerTests
{
[Fact]
public async Task Should_throw_when_adapter_not_SQL()
{
var exception = await Should.ThrowAsync<ArgumentException>(Respawner.CreateAsync("Server=(LocalDb)\\mssqllocaldb;Database=SqlServerTests;Integrated Security=True", new RespawnerOptions
{
DbAdapter = DbAdapter.MySql
}));
}
}
18 changes: 18 additions & 0 deletions Respawn/Respawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,22 @@ private Respawner(RespawnerOptions options)
Options = options;
}

/// <summary>
/// Creates a <see cref="Respawner" /> based on the supplied options and connection string or name. This overload only supports SQL Server.
/// </summary>
/// <param name="nameOrConnectionString">Name or connection string</param>
/// <param name="options">Options</param>
/// <returns>A respawner with generated SQL based on the supplied connection string</returns>
/// <exception cref="ArgumentException">Throws if the options are any other database adapter besides SQL</exception>
public static async Task<Respawner> CreateAsync(string nameOrConnectionString, RespawnerOptions? options = default)
{
options ??= new RespawnerOptions();

if (options.DbAdapter is not SqlServerDbAdapter)
{
throw new ArgumentException("This overload only supports the SqlDataAdapter. To use an alternative adapter, use the overload that supplies a DbConnection.", nameof(options.DbAdapter));
}

await using var connection = new SqlConnection(nameOrConnectionString);

await connection.OpenAsync();
Expand All @@ -35,6 +47,12 @@ public static async Task<Respawner> CreateAsync(string nameOrConnectionString, R
return respawner;
}

/// <summary>
/// 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>
/// <returns>A respawner with generated SQL based on the supplied connection object.</returns>
public static async Task<Respawner> CreateAsync(DbConnection connection, RespawnerOptions? options = default)
{
options ??= new RespawnerOptions();
Expand Down

0 comments on commit 6bb39d4

Please sign in to comment.