Skip to content

Commit 1f30e52

Browse files
committed
Index constraint handling empty indexes
1 parent 89d518a commit 1f30e52

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

sql/020-config-schema.sql

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,38 @@ DO $$
3333
END
3434
$$;
3535

36+
37+
38+
--
39+
-- Extracts index keys/names from configuration json
40+
--
41+
-- Used by the _cs_config_check_indexes as part of the cs_configuration_data_v1_check constraint
42+
--
43+
DROP FUNCTION IF EXISTS _cs_extract_indexes(jsonb);
44+
CREATE FUNCTION _cs_extract_indexes(val jsonb)
45+
RETURNS SETOF text
46+
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
47+
BEGIN ATOMIC
48+
SELECT jsonb_object_keys(jsonb_path_query(val,'$.tables.*.*.indexes'));
49+
END;
50+
3651
--
3752
-- _cs_check_config_indexes returns true if the table configuration only includes valid index types
3853
--
3954
-- Used by the cs_configuration_data_v1_check constraint
4055
--
41-
-- Function types cannot be changed after creation so we always DROP & CREATE for flexibility
42-
--
4356
DROP FUNCTION IF EXISTS _cs_config_check_indexes(jsonb);
44-
4557
CREATE FUNCTION _cs_config_check_indexes(val jsonb)
4658
RETURNS BOOLEAN
4759
AS $$
4860
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
61+
IF (SELECT EXISTS (SELECT _cs_extract_indexes(val))) THEN
62+
IF (SELECT bool_and(index = ANY('{match, ore, unique, ste_vec}')) FROM _cs_extract_indexes(val) AS index) THEN
5163
RETURN true;
5264
END IF;
5365
RAISE 'Invalid index (%) in configuration. Index should be one of {match, ore, unique, ste_vec}', val;
5466
END IF;
67+
RETURN true;
5568
END;
5669
$$ LANGUAGE plpgsql;
5770

sql/021-config-functions.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ AS $$
299299
$$ LANGUAGE plpgsql;
300300

301301

302-
DROP FUNCTION IF EXISTS cs_add_column_v1(table_name text, column_name text);
302+
DROP FUNCTION IF EXISTS cs_add_column_v1(table_name text, column_name text, cast_as text);
303303

304304
CREATE FUNCTION cs_add_column_v1(table_name text, column_name text, cast_as text DEFAULT 'text')
305305
RETURNS jsonb

tests/config.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,12 @@ INSERT INTO cs_configuration_v1 (state, data) VALUES (
148148
"blah": {
149149
"cast_as": "text",
150150
"indexes": {
151-
"match": {}
151+
"match": {}
152152
}
153+
},
154+
"vtha": {
155+
"cast_as": "text",
156+
"indexes": {}
153157
}
154158
}
155159
}

0 commit comments

Comments
 (0)