-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Add a custom convention which adds a blank trigger to all tables #28749
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 0 additions & 2 deletions
2
src/EFCore.Relational/Extensions/RelationalTriggerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/EFCore.Relational/Metadata/Conventions/BlankTriggerAddingConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
|
||
/// <summary> | ||
/// A convention that makes sure there is a trigger on all entity types. | ||
/// </summary> | ||
/// <remarks> | ||
/// See <see href="https://aka.ms/efcore-docs-conventions">Model building conventions</see> for more information and examples. | ||
/// </remarks> | ||
public class BlankTriggerAddingConvention : IModelFinalizingConvention | ||
{ | ||
/// <summary> | ||
/// Creates a new instance of <see cref="BlankTriggerAddingConvention" />. | ||
/// </summary> | ||
/// <param name="dependencies">Parameter object containing dependencies for this convention.</param> | ||
/// <param name="relationalDependencies"> Parameter object containing relational dependencies for this convention.</param> | ||
public BlankTriggerAddingConvention( | ||
ProviderConventionSetBuilderDependencies dependencies, | ||
RelationalConventionSetBuilderDependencies relationalDependencies) | ||
{ | ||
Dependencies = dependencies; | ||
RelationalDependencies = relationalDependencies; | ||
} | ||
|
||
/// <summary> | ||
/// Dependencies for this convention. | ||
/// </summary> | ||
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; } | ||
|
||
/// <summary> | ||
/// Relational provider-specific dependencies for this service. | ||
/// </summary> | ||
protected virtual RelationalConventionSetBuilderDependencies RelationalDependencies { get; } | ||
|
||
/// <summary> | ||
/// Called when a model is being finalized. | ||
/// </summary> | ||
/// <param name="modelBuilder">The builder for the model.</param> | ||
/// <param name="context">Additional information associated with convention execution.</param> | ||
public virtual void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context) | ||
{ | ||
foreach (var entityType in modelBuilder.Metadata.GetEntityTypes()) | ||
{ | ||
var table = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table); | ||
if (table != null | ||
&& entityType.GetDeclaredTriggers().All(t => t.GetName(table.Value) == null)) | ||
{ | ||
entityType.Builder.HasTrigger(table.Value.Name + "_Trigger"); | ||
} | ||
|
||
foreach (var fragment in entityType.GetMappingFragments(StoreObjectType.Table)) | ||
{ | ||
if (entityType.GetDeclaredTriggers().All(t => t.GetName(fragment.StoreObject) == null)) | ||
{ | ||
entityType.Builder.HasTrigger(fragment.StoreObject.Name + "_Trigger"); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
// ReSharper disable CheckNamespace | ||
|
||
namespace Microsoft.EntityFrameworkCore.Internal; | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public static class DbContextExtensions | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public static void ConfigureConventions( | ||
this DbContext context, | ||
ModelConfigurationBuilder configurationBuilder) | ||
=> context.ConfigureConventions(configurationBuilder); | ||
} |
55 changes: 55 additions & 0 deletions
55
test/EFCore.Relational.Tests/Metadata/Conventions/BlankTriggerAddingConventionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#nullable enable | ||
|
||
// ReSharper disable InconsistentNaming | ||
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions; | ||
|
||
public class BlankTriggerAddingConventionTest | ||
{ | ||
[ConditionalFact] | ||
public virtual void Adds_triggers_with_table_splitting() | ||
{ | ||
var modelBuilder = CreateModelBuilder(); | ||
|
||
modelBuilder.Entity<Order>().SplitToTable("OrderDetails", s => s.Property(o => o.CustomerId)); | ||
|
||
var model = modelBuilder.FinalizeModel(); | ||
|
||
var entity = model.FindEntityType(typeof(Order))!; | ||
|
||
Assert.Equal(new[] { "OrderDetails_Trigger", "Order_Trigger" }, entity.GetDeclaredTriggers().Select(t => t.ModelName)); | ||
} | ||
|
||
[ConditionalFact] | ||
public virtual void Does_not_add_triggers_without_tables() | ||
{ | ||
var modelBuilder = CreateModelBuilder(); | ||
|
||
modelBuilder.Entity<Order>().ToView("Orders"); | ||
modelBuilder.Entity<Order>().SplitToView("OrderDetails", s => s.Property(o => o.CustomerId)); | ||
|
||
var model = modelBuilder.FinalizeModel(); | ||
|
||
var entity = model.FindEntityType(typeof(Order))!; | ||
|
||
Assert.Empty(entity.GetDeclaredTriggers()); | ||
} | ||
|
||
protected class Order | ||
{ | ||
public int OrderId { get; set; } | ||
|
||
public int? CustomerId { get; set; } | ||
public Guid AnotherCustomerId { get; set; } | ||
} | ||
|
||
protected virtual ModelBuilder CreateModelBuilder() | ||
=> FakeRelationalTestHelpers.Instance.CreateConventionBuilder( | ||
configureModel: | ||
b => b.Conventions.Add( | ||
p => new BlankTriggerAddingConvention( | ||
p.GetRequiredService<ProviderConventionSetBuilderDependencies>(), | ||
p.GetRequiredService<RelationalConventionSetBuilderDependencies>()))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we are shipping the convention we need to declare these dependencies to avoid potential breaking changes if in the future we need to use some service.