Skip to content

Commit

Permalink
SQLite: RETURNING clause doesn't work with AFTER triggers #gh-29811 - (
Browse files Browse the repository at this point in the history
…dotnet/efcore#29811)

Overcomming with BEFORE UPDATER trigger not AFTER for sqlite.
  • Loading branch information
ChaosEngine committed Dec 16, 2022
1 parent 3481c46 commit 2a83248
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/InkBall.Module/Model/ModelHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ internal static MigrationBuilder CreateTimestampTrigger(this MigrationBuilder mi
switch (migrationBuilder.ActiveProvider)
{
case "Microsoft.EntityFrameworkCore.Sqlite":
string command =
//SQLite: RETURNING clause doesn't work with AFTER triggers #gh-29811
//https://github.com/dotnet/efcore/issues/29811
//
string command =
$@"CREATE TRIGGER IF NOT EXISTS {tableName}_update_{timeStampColumnName}_Trigger
AFTER UPDATE ON {tableName}
BEFORE UPDATE ON {tableName}
BEGIN
UPDATE {tableName} SET {timeStampColumnName} = datetime(CURRENT_TIMESTAMP, 'localtime') WHERE {primaryKey} = NEW.{primaryKey};
END;";
Expand All @@ -110,7 +113,7 @@ AFTER UPDATE ON {tableName}

case "Microsoft.EntityFrameworkCore.SqlServer":
command =
$@"CREATE TRIGGER [dbo].[{tableName}_update_{timeStampColumnName}_Trigger] ON [dbo].[{tableName}]
$@"CREATE OR ALTER TRIGGER [dbo].[{tableName}_update_{timeStampColumnName}_Trigger] ON [dbo].[{tableName}]
AFTER UPDATE
AS
BEGIN
Expand Down Expand Up @@ -141,14 +144,14 @@ FOR EACH ROW

case "Npgsql.EntityFrameworkCore.PostgreSQL":
command =
$@"CREATE FUNCTION ""{tableName}_update_{timeStampColumnName}_TrigFunc""() RETURNS trigger AS $$
$@"CREATE OR REPLACE FUNCTION ""{tableName}_update_{timeStampColumnName}_TrigFunc""() RETURNS trigger AS $$
BEGIN
NEW.""{timeStampColumnName}"" := CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER ""{tableName}_update_{timeStampColumnName}_Trigger"" BEFORE UPDATE ON ""{tableName}""
CREATE OR REPLACE TRIGGER ""{tableName}_update_{timeStampColumnName}_Trigger"" BEFORE UPDATE ON ""{tableName}""
FOR EACH ROW EXECUTE FUNCTION ""{tableName}_update_{timeStampColumnName}_TrigFunc""();
";
//Console.Error.WriteLine($"executing '{command}'");
Expand Down Expand Up @@ -177,7 +180,7 @@ internal static MigrationBuilder DropTimestampTrigger(this MigrationBuilder migr
break;

case "Microsoft.EntityFrameworkCore.SqlServer":
command = $@"DROP TRIGGER [dbo].[{tableName}_update_{timeStampColumnName}_Trigger];";
command = $@"DROP TRIGGER IF EXISTS [dbo].[{tableName}_update_{timeStampColumnName}_Trigger];";

//Console.Error.WriteLine($"executing '{command}'");
migrationBuilder.Sql(command);
Expand Down

0 comments on commit 2a83248

Please sign in to comment.