-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema3_update.sql
34 lines (31 loc) · 1.11 KB
/
schema3_update.sql
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
32
33
34
-- Add new columns to the documents table
ALTER TABLE documents
ADD COLUMN created_by INT REFERENCES users (id),
ADD COLUMN last_modified_by INT REFERENCES users (id),
ADD COLUMN last_modified_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
ADD COLUMN status VARCHAR(255) NOT NULL DEFAULT 'draft',
ADD COLUMN compliance_status VARCHAR(255) NOT NULL DEFAULT 'pending review';
-- Create a view for documents with "pending approval" status
CREATE VIEW pending_approval_documents AS
SELECT *
FROM documents
WHERE status = 'pending approval';
-- Create a stored procedure for documents with "pending approval" status
DELIMITER //
CREATE PROCEDURE get_pending_approval_documents()
BEGIN
SELECT *
FROM documents
WHERE status = 'pending approval';
END //
DELIMITER ;
-- Trigger for updating last_modified_by and last_modified_at when a document is created or modified
DELIMITER //
CREATE TRIGGER update_document_modification
BEFORE INSERT OR UPDATE ON documents
FOR EACH ROW
BEGIN
SET NEW.last_modified_by = (SELECT id FROM users WHERE username = CURRENT_USER());
SET NEW.last_modified_at = CURRENT_TIMESTAMP;
END //
DELIMITER ;