34 lines
No EOL
1.1 KiB
PL/PgSQL
34 lines
No EOL
1.1 KiB
PL/PgSQL
-- Create a trigger function that automatically creates tablo_access when a new tablo is created
|
|
CREATE OR REPLACE FUNCTION create_tablo_access_for_owner()
|
|
RETURNS TRIGGER
|
|
SECURITY DEFINER
|
|
AS $$
|
|
BEGIN
|
|
-- Insert a tablo_access record for the tablo owner
|
|
INSERT INTO tablo_access (
|
|
tablo_id,
|
|
user_id,
|
|
granted_by,
|
|
is_active,
|
|
is_admin
|
|
) VALUES (
|
|
NEW.id, -- tablo_id: the newly created tablo's id
|
|
NEW.owner_id, -- user_id: the tablo owner gets access
|
|
NEW.owner_id, -- granted_by: self-granted by the owner
|
|
TRUE, -- is_active: access is active
|
|
TRUE -- is_admin: owner has admin privileges
|
|
);
|
|
|
|
RETURN NEW;
|
|
END;
|
|
$$ LANGUAGE plpgsql;
|
|
|
|
-- Create the trigger that fires after INSERT on tablos table
|
|
CREATE TRIGGER trigger_create_tablo_access
|
|
AFTER INSERT ON tablos
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION create_tablo_access_for_owner();
|
|
|
|
-- Add a comment to document the trigger
|
|
COMMENT ON TRIGGER trigger_create_tablo_access ON tablos IS
|
|
'Automatically creates tablo_access record for the tablo owner when a new tablo is created'; |