win7user10 / Laraue.EfCoreTriggers

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

Must RETURN OLD in function for *Delete triggers for PostgreSQL #81

Closed ds2307 closed 1 year ago

ds2307 commented 1 year ago

For PostgreSQL:

   builder.BeforeDelete(trigger => trigger
            .Action(action => action
                .Condition(v => v.PublishedId != null)
                .ExecuteRawSql("RAISE EXCEPTION 'Forbidden';")
            )
        );

generate

migrationBuilder.Sql("CREATE FUNCTION \"core_service\".\"LC_TRIGGER_BEFORE_DELETE_FORM\"() RETURNS trigger as $LC_TRIGGER_BEFORE_DELETE_FORM$\r\nBEGIN\r\n  IF OLD.\"published_id\" IS NOT NULL THEN \r\n    RAISE EXCEPTION 'Forbidden';\r\n  END IF;\r\nRETURN NEW;\r\nEND;\r\n$LC_TRIGGER_BEFORE_DELETE_FORM$ LANGUAGE plpgsql;\r\nCREATE TRIGGER LC_TRIGGER_BEFORE_DELETE_FORM BEFORE DELETE\r\nON \"core_service\".\"forms\"\r\nFOR EACH ROW EXECUTE PROCEDURE \"core_service\".\"LC_TRIGGER_BEFORE_DELETE_FORM\"();");

but function for trigger must RETURN OLD (not RETURN NEW) in this case, otherwise row will not be removed

https://github.com/win7user10/Laraue.EfCoreTriggers/blob/master/src/Laraue.EfCoreTriggers.PostgreSql/PostgreSqlTriggerVisitor.cs#L50

win7user10 commented 1 year ago

Hi, thank you for the reported bug. The project needs refactoring and a couple of changes to process these types of triggers. I will try to make them on the weekend.

win7user10 commented 1 year ago

@ds2307 refactoring was not required but I don't know did I understand the problem correctly. Please check https://github.com/win7user10/Laraue.EfCoreTriggers/pull/82 and versions 7.0.8-alpha/5.4.13-alpha

ds2307 commented 1 year ago

@win7user10 , generated code is correct now. Thank you so much for the quick fix and for this great project!