refactor the logic to init skip_audit_log_database (#22493)

simplify the sql to set the value of skip_audit_log_database, any evidence of previous usage of db to record the audit, set it to false.

Signed-off-by: wang yan <yan-yw.wang@broadcom.com>
Co-authored-by: wang yan <yan-yw.wang@broadcom.com>
This commit is contained in:
Wang Yan 2025-10-24 18:22:38 +08:00 committed by GitHub
parent 01f848e7f2
commit c305a80616
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,48 +1,17 @@
/*
Initialize skip_audit_log_database configuration based on existing audit log usage - Only insert the configuration if it doesn't already exist
1. If audit logs exist in the system (either in audit_log or audit_log_ext tables),
1. If tables exist and show evidence of previous usage
set skip_audit_log_database to false
2. If no audit logs exist but the tables show evidence of previous usage
set skip_audit_log_database to false
3. If tables exist but show no evidence of usage, don't create the configuration record
2. If tables exist but show no evidence of usage, don't create the configuration record
*/
DO $$
DECLARE
audit_log_count INTEGER := 0;
audit_log_ext_count INTEGER := 0;
total_audit_logs INTEGER := 0;
config_exists INTEGER := 0;
audit_log_seq_value BIGINT := 0;
audit_log_ext_seq_value BIGINT := 0;
audit_log_table_used BOOLEAN := FALSE;
audit_log_ext_table_used BOOLEAN := FALSE;
should_set_config BOOLEAN := FALSE;
skip_audit_value TEXT := 'false';
BEGIN
SELECT COUNT(*) INTO config_exists
FROM properties
WHERE k = 'skip_audit_log_database';
IF EXISTS (SELECT 1 FROM properties WHERE k = 'skip_audit_log_database') THEN
RETURN;
END IF;
IF config_exists = 0 THEN
SELECT COUNT(*) INTO audit_log_count FROM audit_log;
SELECT last_value INTO audit_log_seq_value FROM audit_log_id_seq;
audit_log_table_used := (audit_log_seq_value > 1);
SELECT COUNT(*) INTO audit_log_ext_count FROM audit_log_ext;
SELECT last_value INTO audit_log_ext_seq_value FROM audit_log_ext_id_seq;
audit_log_ext_table_used := (audit_log_ext_seq_value > 1);
total_audit_logs := audit_log_count + audit_log_ext_count;
IF total_audit_logs > 0 THEN
should_set_config := TRUE;
ELSIF audit_log_table_used OR audit_log_ext_table_used THEN
should_set_config := TRUE;
END IF;
IF should_set_config THEN
INSERT INTO properties (k, v) VALUES ('skip_audit_log_database', skip_audit_value);
END IF;
IF (SELECT last_value FROM audit_log_id_seq) > 1
OR (SELECT last_value FROM audit_log_ext_id_seq) > 1 THEN
INSERT INTO properties (k, v) VALUES ('skip_audit_log_database', 'false');
END IF;
END $$;