boolean triggers

This commit is contained in:
mertalev
2026-01-16 16:30:29 -05:00
parent 9b6c9c41c7
commit 137b65a9f6
2 changed files with 9 additions and 6 deletions

View File

@@ -263,8 +263,9 @@ export const asset_edit_insert = registerFunction({
body: `
BEGIN
UPDATE asset
SET "editCount" = "editCount" + 1
WHERE "id" = NEW."assetId";
SET "isEdited" = true
FROM NEW
WHERE asset.id = NEW."assetId" AND NOT "isEdited";
RETURN NULL;
END
`,
@@ -277,8 +278,10 @@ export const asset_edit_delete = registerFunction({
body: `
BEGIN
UPDATE asset
SET "editCount" = "editCount" - 1
WHERE "id" = OLD."assetId";
SET "isEdited" = false
FROM OLD
WHERE asset.id = OLD."assetId" AND "isEdited"
AND NOT EXISTS (SELECT FROM asset_edit edit WHERE edit.asset_id = asset.id);
RETURN NULL;
END
`,

View File

@@ -12,9 +12,9 @@ import {
} from 'src/sql-tools';
@Table('asset_edit')
@AfterInsertTrigger({ scope: 'row', function: asset_edit_insert })
@AfterInsertTrigger({ scope: 'statement', function: asset_edit_insert })
@AfterDeleteTrigger({
scope: 'row',
scope: 'statement',
function: asset_edit_delete,
referencingOldTableAs: 'old',
when: 'pg_trigger_depth() = 0',