Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only allowing SQL adapter for the overload that takes a connection st… #123

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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