-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaintain_single_current_document.sql
More file actions
31 lines (28 loc) · 1.08 KB
/
Copy pathmaintain_single_current_document.sql
File metadata and controls
31 lines (28 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-- Function to ensure only one document has is_current = true per form
CREATE OR REPLACE FUNCTION maintain_single_current_document()
RETURNS TRIGGER AS $$
BEGIN
-- Only proceed if the new/updated record has is_current = true
IF NEW.is_current = true THEN
-- Set is_current = false for all other documents with the same form_id
-- Exclude the current document being inserted/updated (using its ID)
UPDATE documents
SET is_current = false
WHERE
form_id = NEW.form_id AND
id != NEW.id AND
is_current = true;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create a trigger that runs before insert or update on the documents table
DROP TRIGGER IF EXISTS ensure_single_current_document ON documents;
CREATE TRIGGER ensure_single_current_document
BEFORE INSERT OR UPDATE OF is_current
ON documents
FOR EACH ROW
EXECUTE FUNCTION maintain_single_current_document();
-- Add a comment explaining the trigger's purpose
COMMENT ON TRIGGER ensure_single_current_document ON documents IS
'Ensures that only one document can have is_current = true for each form_id';