Skip to content

Commit 2e6f40e

Browse files
committed
Improved config constraint errors
1 parent 4fd756f commit 2e6f40e

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

sql/020-config-schema.sql

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,66 @@ DROP FUNCTION IF EXISTS _cs_config_check_indexes(jsonb);
4444

4545
CREATE FUNCTION _cs_config_check_indexes(val jsonb)
4646
RETURNS BOOLEAN
47-
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
48-
BEGIN ATOMIC
49-
SELECT jsonb_object_keys(jsonb_path_query(val, '$.tables.*.*.indexes')) = ANY('{match, ore, unique, ste_vec}');
50-
END;
47+
AS $$
48+
BEGIN
49+
IF jsonb_path_query(val, '$.tables.*.*.indexes') <> '{}':jsonb THEN
50+
IF EXISTS (SELECT jsonb_object_keys(jsonb_path_query(val, '$.tables.*.*.indexes')) = ANY('{match, ore, unique, ste_vec}')) THEN
51+
RETURN true;
52+
END IF;
53+
RAISE 'Invalid index (%) in configuration. Index should be one of {match, ore, unique, ste_vec}', val;
54+
END IF;
55+
END;
56+
$$ LANGUAGE plpgsql;
5157

5258

5359
DROP FUNCTION IF EXISTS _cs_config_check_cast(jsonb);
5460

5561
CREATE FUNCTION _cs_config_check_cast(val jsonb)
5662
RETURNS BOOLEAN
57-
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
58-
BEGIN ATOMIC
59-
SELECT jsonb_array_elements_text(jsonb_path_query_array(val, '$.tables.*.*.cast_as')) = ANY('{text, int, small_int, big_int, real, double, boolean, date, jsonb}');
60-
END;
63+
AS $$
64+
BEGIN
65+
IF EXISTS (SELECT jsonb_array_elements_text(jsonb_path_query_array(val, '$.tables.*.*.cast_as')) = ANY('{text, int, small_int, big_int, real, double, boolean, date, jsonb}')) THEN
66+
RETURN true;
67+
END IF;
68+
RAISE 'Invalid cast (%) in configuration. Cast should be one of {text, int, small_int, big_int, real, double, boolean, date, jsonb}', val;
69+
END;
70+
$$ LANGUAGE plpgsql;
71+
72+
--
73+
-- Should include a tables field
74+
-- Tables should not be empty
75+
DROP FUNCTION IF EXISTS _cs_config_check_tables(jsonb);
76+
CREATE FUNCTION _cs_config_check_tables(val jsonb)
77+
RETURNS boolean
78+
AS $$
79+
BEGIN
80+
IF (val ? 'tables') AND (val->'tables' <> '{"A":"a"}'::jsonb) THEN
81+
RETURN true;
82+
END IF;
83+
RAISE 'Configuration missing tables (tables) field: %', val;
84+
END;
85+
$$ LANGUAGE plpgsql;
86+
87+
-- Should include a version field
88+
DROP FUNCTION IF EXISTS _cs_config_check_v(jsonb);
89+
CREATE FUNCTION _cs_config_check_v(val jsonb)
90+
RETURNS boolean
91+
AS $$
92+
BEGIN
93+
IF (val ? 'v') THEN
94+
RETURN true;
95+
END IF;
96+
RAISE 'Configuration missing version (v) field: %', val;
97+
END;
98+
$$ LANGUAGE plpgsql;
6199

62100

63101
ALTER DOMAIN cs_configuration_data_v1 DROP CONSTRAINT IF EXISTS cs_configuration_data_v1_check;
64102

65103
ALTER DOMAIN cs_configuration_data_v1
66104
ADD CONSTRAINT cs_configuration_data_v1_check CHECK (
67-
VALUE ?& array['v', 'tables'] AND
68-
VALUE->'tables' <> '{}'::jsonb AND
105+
_cs_config_check_v(VALUE) AND
106+
_cs_config_check_tables(VALUE) AND
69107
_cs_config_check_cast(VALUE) AND
70108
_cs_config_check_indexes(VALUE)
71109
);

0 commit comments

Comments
 (0)