win7user10 / Laraue.EfCoreTriggers

Library to write triggers in C# with EF.Core
MIT License
112 stars 20 forks source link

postgresql trigger/function name #79

Closed Klynk closed 1 year ago

Klynk commented 1 year ago

There are quotes on the trigger function create statement, and no quotes on the drop statement.

This causes subsequent Up migrations to fail, as the trigger/function cannot be found when dropped. This is the case for the Down as well.

image

Laraue.EfCoreTriggers.Common: 7.0.4 (also tested on 7.0.3 - 7.0.0) Laraue.EfCoreTriggers.PostgreSql: 7.0.4 (also tested on 7.0.3 - 7.0.0)

win7user10 commented 1 year ago

Hi, I made a fix in version 7.0.5, it should work

Klynk commented 1 year ago

Hello, thank for the fast response.

Now when I create the first migration, everything works as expected.

entity.AfterUpdate(trigger =>
{
    trigger.Action(action =>
    {
        action.Update<MyEntity>((ot, nt, ce) => ce.Id == ot.Id && ot.FirstProperty != null && nt.FirstProperty == null, (ot, nt, oe) => new MyEntity() { SecondProperty = null });
    });
});

When I make a change(s) to the trigger and create a new migration, I get this error message (I changed == into !=):

entity.AfterUpdate(trigger =>
{
    trigger.Action(action =>
    {
        action.Update<MyEntity>((ot, nt, ce) => ce.Id == ot.Id && ot.FirstProperty != null && nt.FirstProperty != null, (ot, nt, oe) => new MyEntity() { SecondProperty = null });
    });
});
PM> add-migration 2
Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.RelationalEntityTypeExtensions.GetTableName(IReadOnlyEntityType entityType)
   at Microsoft.EntityFrameworkCore.RelationalEntityTypeExtensions.GetSchema(IReadOnlyEntityType entityType)
   at Laraue.EfCoreTriggers.Common.Services.Impl.EfCoreDbSchemaRetriever.GetTableSchemaName(Type entity)
   at Laraue.EfCoreTriggers.Common.Services.Impl.SqlGenerator.GetFunctionNameSql(Type entity, String name)
   at Laraue.EfCoreTriggers.PostgreSql.PostgreSqlTriggerVisitor.GenerateDeleteTriggerSql(String triggerName, IEntityType entityType)
   at Laraue.EfCoreTriggers.Common.Migrations.MigrationsExtensions.AddDeleteTriggerSqlMigration(ITriggerVisitor triggerVisitor, IList`1 list, IAnnotation annotation, IEntityType entityType)
   at Laraue.EfCoreTriggers.Common.Migrations.TriggerModelDiffer.AddTriggerOperations(IEnumerable`1 operations, IRelationalModel source, IRelationalModel target)
   at Laraue.EfCoreTriggers.Common.Migrations.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.

If I change back into version 7.0.4 then I no longer get this error message, but I need to manually change the trigger name after migration is generated.

win7user10 commented 1 year ago

@Klynk I made a fix yesterday, hope it will work

Klynk commented 1 year ago

Thank you for the response. I am still having the same issue after the update. I made a smaller project which reproduces the issue for me.

MigrationBug.zip

When I run add-migration 2 I get the following error with 7.0.6:

PM> add-migration 2
Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.RelationalEntityTypeExtensions.GetTableName(IReadOnlyEntityType entityType)
   at Microsoft.EntityFrameworkCore.RelationalEntityTypeExtensions.GetSchema(IReadOnlyEntityType entityType)
   at Laraue.EfCoreTriggers.Common.Services.Impl.EfCoreDbSchemaRetriever.GetTableSchemaName(Type entity)
   at Laraue.EfCoreTriggers.Common.Services.Impl.SqlGenerator.GetFunctionNameSql(Type entity, String name)
   at Laraue.EfCoreTriggers.PostgreSql.PostgreSqlTriggerVisitor.GenerateDeleteTriggerSql(String triggerName, IEntityType entityType)
   at Laraue.EfCoreTriggers.Common.Migrations.MigrationsExtensions.AddDeleteTriggerSqlMigration(ITriggerVisitor triggerVisitor, IList`1 list, IAnnotation annotation, IEntityType entityType)
   at Laraue.EfCoreTriggers.Common.Migrations.TriggerModelDiffer.AddTriggerOperations(IEnumerable`1 operations, IRelationalModel source, IRelationalModel target)
   at Laraue.EfCoreTriggers.Common.Migrations.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.
win7user10 commented 1 year ago

@Klynk just released another version

Klynk commented 1 year ago

@win7user10 I tested version 7.0.7 and it works as expected now. Thank you for your help 🎉