Skip to content

Commit 866ead3

Browse files
committed
add more db constraints on result-related columns
1 parent cebdfe4 commit 866ead3

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

schema/crdb/audit-log/up03.sql

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,22 @@ CREATE TABLE IF NOT EXISTS omicron.public.audit_log (
3737
OR (time_completed IS NOT NULL AND result_kind IS NOT NULL)
3838
),
3939

40-
-- make sure we always have a status code for success and error results.
41-
-- in other words, the only times http_status_code is allowed to be null is
42-
-- when either there is no result yet or the result is a timeout
43-
CONSTRAINT status_code_present_for_success_error CHECK (
44-
result_kind = 'timeout'
45-
OR result_kind IS NULL
46-
OR http_status_code IS NOT NULL
47-
),
48-
49-
-- when result_kind is error, we always have an error message
50-
CONSTRAINT message_present_for_error CHECK (
51-
result_kind != 'error' OR error_message IS NOT NULL
40+
-- Enforce consistency between result_kind and related fields:
41+
-- 'timeout': no HTTP status or error details
42+
-- 'success': requires HTTP status, no error details
43+
-- 'error': requires HTTP status and error message
44+
-- other/NULL: no HTTP status or error details
45+
CONSTRAINT result_kind_state_consistency CHECK (
46+
CASE result_kind
47+
WHEN 'timeout' THEN http_status_code IS NULL AND error_code IS NULL
48+
AND error_message IS NULL
49+
WHEN 'success' THEN error_code IS NULL AND error_message IS NULL AND
50+
http_status_code IS NOT NULL
51+
WHEN 'error' THEN http_status_code IS NOT NULL AND error_message IS
52+
NOT NULL
53+
ELSE http_status_code IS NULL AND error_code IS NULL AND error_message
54+
IS NULL
55+
END
5256
),
5357

5458
-- Ensure valid actor ID combinations

schema/crdb/dbinit.sql

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5821,18 +5821,22 @@ CREATE TABLE IF NOT EXISTS omicron.public.audit_log (
58215821
OR (time_completed IS NOT NULL AND result_kind IS NOT NULL)
58225822
),
58235823

5824-
-- make sure we always have a status code for success and error results.
5825-
-- in other words, the only times http_status_code is allowed to be null is
5826-
-- when either there is no result yet or the result is a timeout
5827-
CONSTRAINT status_code_present_for_success_error CHECK (
5828-
result_kind = 'timeout'
5829-
OR result_kind IS NULL
5830-
OR http_status_code IS NOT NULL
5831-
),
5832-
5833-
-- when result_kind is error, we always have an error message
5834-
CONSTRAINT message_present_for_error CHECK (
5835-
result_kind != 'error' OR error_message IS NOT NULL
5824+
-- Enforce consistency between result_kind and related fields:
5825+
-- 'timeout': no HTTP status or error details
5826+
-- 'success': requires HTTP status, no error details
5827+
-- 'error': requires HTTP status and error message
5828+
-- other/NULL: no HTTP status or error details
5829+
CONSTRAINT result_kind_state_consistency CHECK (
5830+
CASE result_kind
5831+
WHEN 'timeout' THEN http_status_code IS NULL AND error_code IS NULL
5832+
AND error_message IS NULL
5833+
WHEN 'success' THEN error_code IS NULL AND error_message IS NULL AND
5834+
http_status_code IS NOT NULL
5835+
WHEN 'error' THEN http_status_code IS NOT NULL AND error_message IS
5836+
NOT NULL
5837+
ELSE http_status_code IS NULL AND error_code IS NULL AND error_message
5838+
IS NULL
5839+
END
58365840
),
58375841

58385842
-- Ensure valid actor ID combinations

0 commit comments

Comments
 (0)