Skip to content

Commit 752941b

Browse files
committed
add more db constraints on result-related columns
1 parent ab8c239 commit 752941b

2 files changed

Lines changed: 32 additions & 24 deletions

File tree

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
@@ -5802,18 +5802,22 @@ CREATE TABLE IF NOT EXISTS omicron.public.audit_log (
58025802
OR (time_completed IS NOT NULL AND result_kind IS NOT NULL)
58035803
),
58045804

5805-
-- make sure we always have a status code for success and error results.
5806-
-- in other words, the only times http_status_code is allowed to be null is
5807-
-- when either there is no result yet or the result is a timeout
5808-
CONSTRAINT status_code_present_for_success_error CHECK (
5809-
result_kind = 'timeout'
5810-
OR result_kind IS NULL
5811-
OR http_status_code IS NOT NULL
5812-
),
5813-
5814-
-- when result_kind is error, we always have an error message
5815-
CONSTRAINT message_present_for_error CHECK (
5816-
result_kind != 'error' OR error_message IS NOT NULL
5805+
-- Enforce consistency between result_kind and related fields:
5806+
-- 'timeout': no HTTP status or error details
5807+
-- 'success': requires HTTP status, no error details
5808+
-- 'error': requires HTTP status and error message
5809+
-- other/NULL: no HTTP status or error details
5810+
CONSTRAINT result_kind_state_consistency CHECK (
5811+
CASE result_kind
5812+
WHEN 'timeout' THEN http_status_code IS NULL AND error_code IS NULL
5813+
AND error_message IS NULL
5814+
WHEN 'success' THEN error_code IS NULL AND error_message IS NULL AND
5815+
http_status_code IS NOT NULL
5816+
WHEN 'error' THEN http_status_code IS NOT NULL AND error_message IS
5817+
NOT NULL
5818+
ELSE http_status_code IS NULL AND error_code IS NULL AND error_message
5819+
IS NULL
5820+
END
58175821
),
58185822

58195823
-- Ensure valid actor ID combinations

0 commit comments

Comments
 (0)