From b4a811e781250bde9968dae25d6d5068ea7e171d Mon Sep 17 00:00:00 2001 From: Danel Kungla Date: Tue, 12 Aug 2025 16:07:56 +0300 Subject: [PATCH] feat(migrations): add missing fields and triggers for auditing changes in accounts and company_params --- .../20250812143800_log_company_params.sql | 86 ------------------- ...155500_add_missing_fields_to_medreport.sql | 5 ++ 2 files changed, 5 insertions(+), 86 deletions(-) create mode 100644 supabase/migrations/20250812155500_add_missing_fields_to_medreport.sql diff --git a/supabase/migrations/20250812143800_log_company_params.sql b/supabase/migrations/20250812143800_log_company_params.sql index 4641799..26e31bf 100644 --- a/supabase/migrations/20250812143800_log_company_params.sql +++ b/supabase/migrations/20250812143800_log_company_params.sql @@ -25,92 +25,6 @@ $function$ grant execute on function medreport.insert_company_params_on_new_company() to authenticated, service_role; -CREATE OR REPLACE FUNCTION log_company_params_changes() -RETURNS trigger AS $$ -BEGIN - -- For INSERT operation - IF (TG_OP = 'INSERT') THEN - INSERT INTO audit.log_entries ( - schema_name, - table_name, - record_key, - operation, - row_data, - changed_data, - changed_by, - changed_by_role, - changed_at - ) - VALUES ( - 'medreport', -- Schema name - 'company_params', -- Table name - NEW.id, -- The ID of the inserted row - 'INSERT', -- Operation type - NULL, -- No old data for INSERT - row_to_json(NEW), -- New data (after the INSERT) - auth.uid(), -- The user performing the insert - SESSION_USER, -- The role performing the insert - CURRENT_TIMESTAMP -- Timestamp of the insert - ); - -- For UPDATE operation - ELSIF (TG_OP = 'UPDATE') THEN - INSERT INTO audit.log_entries ( - schema_name, - table_name, - record_key, - operation, - row_data, - changed_data, - changed_by, - changed_by_role, - changed_at - ) - VALUES ( - 'medreport', -- Schema name - 'company_params', -- Table name - OLD.id, -- The ID of the updated row - 'UPDATE', -- Operation type - row_to_json(OLD), -- Old data (before the update) - row_to_json(NEW), -- New data (after the update) - auth.uid(), -- The user performing the update - SESSION_USER, -- The role performing the update - CURRENT_TIMESTAMP -- Timestamp of the update - ); - -- For DELETE operation - ELSIF (TG_OP = 'DELETE') THEN - INSERT INTO audit.log_entries ( - schema_name, - table_name, - record_key, - operation, - row_data, - changed_data, - changed_by, - changed_by_role, - changed_at - ) - VALUES ( - 'medreport', -- Schema name - 'company_params', -- Table name - OLD.id, -- The ID of the deleted row - 'DELETE', -- Operation type - row_to_json(OLD), -- Old data (before the delete) - NULL, -- No new data for DELETE - auth.uid(), -- The user performing the delete - SESSION_USER, -- The role performing the delete - CURRENT_TIMESTAMP -- Timestamp of the delete - ); - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE TRIGGER company_params_audit_trigger -AFTER INSERT OR UPDATE OR DELETE ON medreport.company_params -FOR EACH ROW -EXECUTE FUNCTION log_company_params_changes(); - create or replace function medreport.create_team_account ( account_name text, new_personal_code text diff --git a/supabase/migrations/20250812155500_add_missing_fields_to_medreport.sql b/supabase/migrations/20250812155500_add_missing_fields_to_medreport.sql new file mode 100644 index 0000000..259293f --- /dev/null +++ b/supabase/migrations/20250812155500_add_missing_fields_to_medreport.sql @@ -0,0 +1,5 @@ +alter table "medreport"."accounts" add column "has_consent_anonymized_company_statistics" boolean; + +CREATE TRIGGER log_account_change AFTER DELETE OR UPDATE ON medreport.accounts FOR EACH ROW EXECUTE FUNCTION audit.log_audit_changes(); +CREATE TRIGGER log_account_change AFTER DELETE OR UPDATE ON medreport.company_params FOR EACH ROW EXECUTE FUNCTION audit.log_audit_changes(); +