Skip to content

Commit

Permalink
Merge pull request #65 from deveel/64-entityframework-sqlserver-webho…
Browse files Browse the repository at this point in the history
…okdbcontext-schema-issue

Fixes to Entity Framework Configuration Bug #64
  • Loading branch information
tsutomi authored Sep 2, 2024
2 parents adc1160 + f7f7d0a commit f63fe38
Show file tree
Hide file tree
Showing 25 changed files with 345 additions and 69 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: true
matrix:
dotnet: [ '6.0.x', '7.0.x' ]
dotnet: [ '6.0.x', '7.0.x', '8.0.x' ]
os: [ ubuntu-latest, windows-latest ]

runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -48,8 +48,14 @@ jobs:
- name: Build
run: dotnet build --no-restore -c Release -f ${{ env.DOTNET_TFM }}

- name: Test
- name: Test (Ubuntu)
if: matrix.os == 'ubuntu-latest'
run: dotnet test --no-build --verbosity normal -c Release -f ${{ env.DOTNET_TFM }} /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[*.XUnit]*"

- name: Test (Ubuntu)
if: matrix.os == 'windows-latest'
run: dotnet test --no-build --verbosity normal -c Release -f ${{ env.DOTNET_TFM }} --filter DB!=SQLServer /p:CollectCoverage=true /p:CoverletOutputFormat=opencover /p:Exclude="[*.XUnit]*"


- name: Collect to Codecov
uses: codecov/codecov-action@v3
Expand Down
36 changes: 22 additions & 14 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
fail-fast: true
matrix:
dotnet: [ '6.0.x', '7.0.x' ]
dotnet: [ '6.0.x', '7.0.x', '8.0.x' ]
os: [ ubuntu-latest, windows-latest ]

runs-on: ${{ matrix.os }}
Expand All @@ -27,23 +27,31 @@ jobs:
with:
dotnet-version: ${{ matrix.dotnet }}

- name: Set the TFM for .NET 6.0
if: matrix.dotnet == '6.0.x'
run: echo "DOTNET_TFM=net6.0" >> $GITHUB_ENV

- name: Set the TFM for .NET 7.0
if: matrix.dotnet == '7.0.x'
run: echo "DOTNET_TFM=net7.0" >> $GITHUB_ENV

- name: Set the TFM for .NET 8.0
if: matrix.dotnet == '8.0.x'
run: echo "DOTNET_TFM=net8.0" >> $GITHUB_ENV
- name: Set the TFM in Ubuntu
if: startsWith(matrix.os, 'ubuntu')
run: |
VERSION=$(echo "${{ matrix.dotnet }}" | sed 's/[^0-9.]*//g')
VERSION=$(echo "${VERSION}" | sed 's/\.$//')
DOTNET_TFM="net${VERSION}"
echo "DOTNET_TFM=$DOTNET_TFM" >> $GITHUB_ENV
- name: Set the TFM in Windows
if: startsWith(matrix.os, 'windows')
run: |
$VERSION = "${{ matrix.dotnet }}".Substring(0, "${{ matrix.dotnet }}".LastIndexOf('.'))
$DOTNET_TFM = "net$VERSION"
echo "DOTNET_TFM=$DOTNET_TFM" | Out-File -FilePath $env:GITHUB_ENV -Append
- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --no-restore -c Release -f ${{ env.DOTNET_TFM }}

- name: Test
run: dotnet test --no-build --verbosity normal -c Release -f ${{ env.DOTNET_TFM }}
- name: Test (Ubuntu)
if: matrix.os == 'ubuntu-latest'
run: dotnet test --no-build --verbosity normal -c Release -f ${{ env.DOTNET_TFM }}

- name: Test (Windows)
if: matrix.os == 'windows-latest'
run: dotnet test --no-build --verbosity normal -c Release -f ${{ env.DOTNET_TFM }} --filter DB!=SQLServer
5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
strategy:
fail-fast: true
matrix:
dotnet: [ '6.0.x', '7.0.x' ]
dotnet: [ '6.0.x', '7.0.x', '8.0.x' ]

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -68,4 +68,7 @@ jobs:
run: dotnet pack --configuration Release --include-symbols -p:PackageVersion=$VERSION --output ./nuget

- name: Push Packages to GitHub NuGet
run: dotnet nuget push ./nuget/**/*.nupkg --skip-duplicate --api-key ${{secrets.GITHUB_TOKEN}} --source "https://nuget.pkg.github.com/deveel/index.json"

- name: Push Packages to NuGet
run: dotnet nuget push ./nuget/**/*.nupkg --skip-duplicate --api-key ${{secrets.NUGET_API_KEY}} --source https://api.nuget.org/v3/index.json
2 changes: 1 addition & 1 deletion samples/WebhookNotifierApp/WebhookNotifierApp.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Deveel.Webhooks</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Deveel.Webhooks</RootNamespace>
Expand Down
2 changes: 1 addition & 1 deletion samples/WebhookReceiverApp/WebhookReceiverApp.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Deveel.Webhooks</RootNamespace>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ public virtual void Configure(EntityTypeBuilder<DbWebhookDeliveryResult> builder
.HasMaxLength(36)
.IsRequired();

builder.Property(x => x.TenantId)
.HasColumnName("tenant_id");

builder.Property(x => x.WebhookId)
.HasColumnName("webhook_id")
.IsRequired();

builder.Property(x => x.EventId)
.HasColumnName("event_id")
.IsRequired();
.IsRequired(false);

builder.Property(x => x.ReceiverId)
.HasColumnName("receiver_id");
.HasColumnName("receiver_id")
.IsRequired(false);

builder.HasOne(x => x.EventInfo)
.WithMany()
Expand All @@ -61,7 +69,7 @@ public virtual void Configure(EntityTypeBuilder<DbWebhookDeliveryResult> builder
builder.HasOne(x => x.Webhook)
.WithMany()
.HasForeignKey(x => x.WebhookId)
.OnDelete(DeleteBehavior.SetNull);
.OnDelete(DeleteBehavior.NoAction);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,39 @@ public EntityWebhookDeliveryResultRepository(WebhookDbContext context, ILogger<E
: base(context, logger) {
}

/// <summary>
/// Gets a queryable object to query the delivery results,
/// including the related entities (webhook, event, delivery
/// attempts, receiver).
/// </summary>
/// <returns>
/// Returns an <see cref="IQueryable{TResult}"/> object that can be used
/// to query the delivery results.
/// </returns>
public override IQueryable<TResult> AsQueryable()
{
return Entities
.Include(x => x.Webhook)
.Include(x => x.EventInfo)
.Include(x => x.DeliveryAttempts)
.Include(x => x.Receiver);
}

/// <inheritdoc/>
public override async Task<TResult?> FindAsync(int key, CancellationToken cancellationToken = default)
{
// This overload to the identity key uses the 'FindAsync' method
// to include all the related entities
// TODO: find a better way to include the related entities through lazy loading
return await FindFirstAsync(Query.Where<TResult>(x => x.Id == key), cancellationToken);
}

/// <inheritdoc/>
public async Task<TResult?> FindByWebhookIdAsync(string webhookId, CancellationToken cancellationToken) {
cancellationToken.ThrowIfCancellationRequested();

try {
return await this.FindFirstAsync<TResult, int>(x => x.Webhook.WebhookId == webhookId, cancellationToken);
return await FindFirstAsync(Query.Where<TResult>(x => x.Webhook.WebhookId == webhookId), cancellationToken);
} catch (Exception ex) {
throw new WebhookEntityException("Unable to query the database for results", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public WebhookDbContext(DbContextOptions<WebhookDbContext> options) : base(optio
/// </summary>
public virtual DbSet<DbWebhookDeliveryResult> DeliveryResults { get; set; }

/// <summary>
/// Gets or sets the set a <c>DbSet</c> that provides query access to
/// the <see cref="DbWebhookDeliveryAttempt"/> entities.
/// </summary>
public virtual DbSet<DbWebhook> Webhooks { get; set; }

/// <inheritdoc/>
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.ApplyConfiguration(new DbWebhookSubscriptionConfiguration());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.24" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.24" />
<PackageReference Include="RichardSzalay.MockHttp" Version="6.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.12" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Testcontainers.MsSql" Version="3.8.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

using Deveel.Data;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;

using Xunit.Abstractions;

namespace Deveel.Webhooks {
public class EntityDeliveryResultRepositoryTests : EntityWebhookTestBase {
public abstract class EntityDeliveryResultRepositoryTests : EntityWebhookTestBase {
private readonly Faker<DbWebhookDeliveryResult> resultFaker;
private readonly Faker<DbEventInfo> eventFaker;
private List<DbWebhookDeliveryResult>? results;

public EntityDeliveryResultRepositoryTests(SqliteTestDatabase sqlite, ITestOutputHelper outputHelper) : base(sqlite, outputHelper) {
public EntityDeliveryResultRepositoryTests(ITestOutputHelper outputHelper)
: base(outputHelper) {
resultFaker = new DbWebhookDeliveryResultFaker();
eventFaker = new DbEventInfoFaker();
}
Expand All @@ -23,16 +25,34 @@ private IWebhookDeliveryResultRepository<DbWebhookDeliveryResult,int> Repository
public override async Task InitializeAsync() {
await base.InitializeAsync();

var events = eventFaker.Generate(10).ToList();
var options = Services.GetRequiredService<DbContextOptions<WebhookDbContext>>();
using var context = new WebhookDbContext(options);

var events = eventFaker.Generate(10).ToList();
results = new List<DbWebhookDeliveryResult>(10 * 5);

foreach (var eventInfo in events) {
var faker = new DbWebhookDeliveryResultFaker(eventInfo);
var deliveryResults = resultFaker.Generate(5);
results.AddRange(deliveryResults);
}

context.DeliveryResults.AddRange(results);
await context.SaveChangesAsync();

// await Repository.AddRangeAsync(results);
}

public override async Task DisposeAsync()
{
var options = Services.GetRequiredService<DbContextOptions<WebhookDbContext>>();
using var context = new WebhookDbContext(options);

context.DeliveryResults.RemoveRange(context.DeliveryResults);
context.Webhooks.RemoveRange(context.Webhooks);
await context.SaveChangesAsync();

await Repository.AddRangeAsync(results);
await base.DisposeAsync();
}

private DbWebhookDeliveryResult NextRandom()
Expand Down Expand Up @@ -117,6 +137,8 @@ public async Task GetByWebhookId() {

Assert.NotNull(found);
Assert.Equal(result.Id, found.Id);
Assert.NotNull(result.Webhook.WebhookId);
Assert.Equal(result.Webhook.WebhookId, found.Webhook.WebhookId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

using Xunit.Abstractions;

using static System.Formats.Asn1.AsnWriter;

namespace Deveel.Webhooks {
public class EntitySubscriptionResolverTests : EntityWebhookTestBase {
public EntitySubscriptionResolverTests(SqliteTestDatabase sqlite, ITestOutputHelper outputHelper)
: base(sqlite, outputHelper) {
namespace Deveel.Webhooks
{
public abstract class EntitySubscriptionResolverTests : EntityWebhookTestBase {
protected EntitySubscriptionResolverTests(ITestOutputHelper outputHelper)
: base(outputHelper) {
}

protected IList<DbWebhookSubscription> Subscriptions { get; private set; }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,21 @@
using Xunit.Abstractions;

namespace Deveel.Webhooks {
[Collection(nameof(EntityWebhookManagementTestCollection))]
public class EntityWebhookManagementTests : WebhookManagementTestSuite<DbWebhookSubscription, string> {
private readonly SqliteTestDatabase sql;
public abstract class EntityWebhookManagementTests : WebhookManagementTestSuite<DbWebhookSubscription, string> {
protected EntityWebhookManagementTests(ITestOutputHelper testOutput) : base(testOutput)
{
}

public EntityWebhookManagementTests(SqliteTestDatabase sql, ITestOutputHelper testOutput) : base(testOutput) {
this.sql = sql;
}

protected override Faker<DbWebhookSubscription> Faker => new DbWebhookSubscriptionFaker();
protected override Faker<DbWebhookSubscription> Faker => new DbWebhookSubscriptionFaker();

protected override string GenerateSubscriptionKey() => Guid.NewGuid().ToString();

protected override void ConfigureWebhookStorage(WebhookSubscriptionBuilder<DbWebhookSubscription, string> options)
=> options.UseEntityFramework(builder => builder.UseContext(ctx => ctx.UseSqlite(sql.Connection)));

protected override async Task InitializeAsync() {
var options = Services!.GetRequiredService<DbContextOptions<WebhookDbContext>>();

using var context = new WebhookDbContext(options);

await context.Database.EnsureDeletedAsync();
// await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

await base.InitializeAsync();
Expand All @@ -39,7 +33,7 @@ protected override async Task DisposeAsync() {
context.Subscriptions.RemoveRange(context.Subscriptions);
await context.SaveChangesAsync(true);

await context.Database.EnsureDeletedAsync();
//await context.Database.EnsureDeletedAsync();
}
}
}
Loading

0 comments on commit f63fe38

Please sign in to comment.