11-- REQUIRE: src/config/types.sql
22
3- --
4- -- Extracts index keys/names from configuration json
5- --
6- -- Used by the eql_v2.config_check_indexes as part of the configuration_data_v2 constraint
7- --
3+ -- ! @file config/constraints.sql
4+ -- ! @brief Configuration validation functions and constraints
5+ -- !
6+ -- ! Provides CHECK constraint functions to validate encryption configuration structure.
7+ -- ! Ensures configurations have required fields (version, tables) and valid values
8+ -- ! for index types and cast types before being stored.
9+ -- !
10+ -- ! @see config/tables.sql where constraints are applied
11+
12+
13+ -- ! @brief Extract index type names from configuration
14+ -- ! @internal
15+ -- !
16+ -- ! Helper function that extracts all index type names from the configuration's
17+ -- ! 'indexes' sections across all tables and columns.
18+ -- !
19+ -- ! @param val jsonb Configuration data to extract from
20+ -- ! @return SETOF text Index type names (e.g., 'match', 'ore', 'unique', 'ste_vec')
21+ -- !
22+ -- ! @note Used by config_check_indexes for validation
23+ -- ! @see eql_v2.config_check_indexes
824CREATE FUNCTION eql_v2 .config_get_indexes(val jsonb)
925 RETURNS SETOF text
1026 LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
1127BEGIN ATOMIC
1228 SELECT jsonb_object_keys(jsonb_path_query(val,' $.tables.*.*.indexes' ));
1329END;
1430
15- --
16- -- _cs_check_config_get_indexes returns true if the table configuration only includes valid index types
17- --
18- -- Used by the cs_configuration_data_v2_check constraint
19- --
31+
32+ -- ! @brief Validate index types in configuration
33+ -- ! @internal
34+ -- !
35+ -- ! Checks that all index types specified in the configuration are valid.
36+ -- ! Valid index types are: match, ore, unique, ste_vec.
37+ -- !
38+ -- ! @param val jsonb Configuration data to validate
39+ -- ! @return boolean True if all index types are valid
40+ -- ! @throws Exception if any invalid index type found
41+ -- !
42+ -- ! @note Used in CHECK constraint on eql_v2_configuration table
43+ -- ! @see eql_v2.config_get_indexes
2044CREATE FUNCTION eql_v2 .config_check_indexes(val jsonb)
2145 RETURNS BOOLEAN
2246 IMMUTABLE STRICT PARALLEL SAFE
3458$$ LANGUAGE plpgsql;
3559
3660
37-
61+ -- ! @brief Validate cast types in configuration
62+ -- ! @internal
63+ -- !
64+ -- ! Checks that all 'cast_as' types specified in the configuration are valid.
65+ -- ! Valid cast types are: text, int, small_int, big_int, real, double, boolean, date, jsonb.
66+ -- !
67+ -- ! @param val jsonb Configuration data to validate
68+ -- ! @return boolean True if all cast types are valid or no cast types specified
69+ -- ! @throws Exception if any invalid cast type found
70+ -- !
71+ -- ! @note Used in CHECK constraint on eql_v2_configuration table
72+ -- ! @note Empty configurations (no cast_as fields) are valid
73+ -- ! @note Cast type names are EQL's internal representations, not PostgreSQL native types
3874CREATE FUNCTION eql_v2 .config_check_cast(val jsonb)
3975 RETURNS BOOLEAN
4076AS $$
5288 END;
5389$$ LANGUAGE plpgsql;
5490
55- --
56- -- Should include a tables field
57- -- Tables should not be empty
91+
92+ -- ! @brief Validate tables field presence
93+ -- ! @internal
94+ -- !
95+ -- ! Ensures the configuration has a 'tables' field, which is required
96+ -- ! to specify which database tables contain encrypted columns.
97+ -- !
98+ -- ! @param val jsonb Configuration data to validate
99+ -- ! @return boolean True if 'tables' field exists
100+ -- ! @throws Exception if 'tables' field is missing
101+ -- !
102+ -- ! @note Used in CHECK constraint on eql_v2_configuration table
58103CREATE FUNCTION eql_v2 .config_check_tables(val jsonb)
59104 RETURNS boolean
60105AS $$
@@ -66,7 +111,18 @@ AS $$
66111 END;
67112$$ LANGUAGE plpgsql;
68113
69- -- Should include a version field
114+
115+ -- ! @brief Validate version field presence
116+ -- ! @internal
117+ -- !
118+ -- ! Ensures the configuration has a 'v' (version) field, which tracks
119+ -- ! the configuration format version.
120+ -- !
121+ -- ! @param val jsonb Configuration data to validate
122+ -- ! @return boolean True if 'v' field exists
123+ -- ! @throws Exception if 'v' field is missing
124+ -- !
125+ -- ! @note Used in CHECK constraint on eql_v2_configuration table
70126CREATE FUNCTION eql_v2 .config_check_version(val jsonb)
71127 RETURNS boolean
72128AS $$
@@ -79,8 +135,24 @@ AS $$
79135$$ LANGUAGE plpgsql;
80136
81137
138+ -- ! @brief Drop existing data validation constraint if present
139+ -- ! @note Allows constraint to be recreated during upgrades
82140ALTER TABLE public .eql_v2_configuration DROP CONSTRAINT IF EXISTS eql_v2_configuration_data_check;
83141
142+
143+ -- ! @brief Comprehensive configuration data validation
144+ -- !
145+ -- ! CHECK constraint that validates all aspects of configuration data:
146+ -- ! - Version field presence
147+ -- ! - Tables field presence
148+ -- ! - Valid cast_as types
149+ -- ! - Valid index types
150+ -- !
151+ -- ! @note Combines all config_check_* validation functions
152+ -- ! @see eql_v2.config_check_version
153+ -- ! @see eql_v2.config_check_tables
154+ -- ! @see eql_v2.config_check_cast
155+ -- ! @see eql_v2.config_check_indexes
84156ALTER TABLE public .eql_v2_configuration
85157 ADD CONSTRAINT eql_v2_configuration_data_check CHECK (
86158 eql_v2 .config_check_version (data) AND
0 commit comments