Skip to content

Commit 95b8ce8

Browse files
committed
fix: restore correct SQL code from phase-4-doxygen
Fix functional regressions from using continue-doxygen-sql-comments source: - src/config/functions.sql: Uncomment add_encrypted_constraint call - src/config/config_test.sql: Better test documentation - src/encrypted/constraints_test.sql: Enhanced test documentation - src/encrypted/functions.sql: Improved documentation - src/encryptindex/functions.sql: Documentation improvements - src/encryptindex/functions_test.sql: Test documentation - src/jsonb/functions.sql: Better function documentation These files use the phase-4-doxygen versions which branched from clean main and have correct code + better Phase 4 documentation. Source: phase-4-doxygen (clean main branch + Phase 4 docs)
1 parent 6a57880 commit 95b8ce8

File tree

7 files changed

+105
-23
lines changed

7 files changed

+105
-23
lines changed

src/config/config_test.sql

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
\set ON_ERROR_STOP on
22

33

4+
-- Create tables for adding configuration
5+
DROP TABLE IF EXISTS users;
6+
CREATE TABLE users
7+
(
8+
id bigint GENERATED ALWAYS AS IDENTITY,
9+
name eql_v2_encrypted,
10+
PRIMARY KEY(id)
11+
);
12+
13+
DROP TABLE IF EXISTS blah;
14+
CREATE TABLE blah
15+
(
16+
id bigint GENERATED ALWAYS AS IDENTITY,
17+
vtha eql_v2_encrypted,
18+
PRIMARY KEY(id)
19+
);
20+
21+
422
--
523
-- Helper function for assertions
624
--
@@ -90,7 +108,7 @@ DO $$
90108
PERFORM eql_v2.remove_search_config('blah', 'vtha', 'unique', migrating => true);
91109
ASSERT NOT (SELECT _search_config_exists('users', 'vtha', 'unique'));
92110

93-
-- All indexes removed, but column config preserved
111+
-- All indexes removed, but column config preserved
94112
ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'pending'));
95113
ASSERT (SELECT data #> array['tables', 'blah', 'vtha', 'indexes'] = '{}' FROM eql_v2_configuration c WHERE c.state = 'pending');
96114

@@ -222,7 +240,7 @@ DO $$
222240
'Pending configuration exists but is empty',
223241
'SELECT * FROM eql_v2_configuration c WHERE c.state = ''pending''',
224242
1);
225-
243+
226244
-- Verify the config is empty
227245
ASSERT (SELECT data #> array['tables'] = '{}' FROM eql_v2_configuration c WHERE c.state = 'pending');
228246

src/config/functions.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ AS $$
7878
PERFORM eql_v2.activate_config();
7979
END IF;
8080

81-
-- PERFORM eql_v2.add_encrypted_constraint(table_name, column_name);
81+
PERFORM eql_v2.add_encrypted_constraint(table_name, column_name);
8282

8383
-- exeunt
8484
RETURN _config;

src/encrypted/constraints_test.sql

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,66 @@ DO $$
4343
$$ LANGUAGE plpgsql;
4444

4545

46+
-- -----------------------------------------------
47+
-- Adding search config adds the constraint
48+
--
49+
-- -----------------------------------------------
50+
TRUNCATE TABLE eql_v2_configuration;
51+
52+
DO $$
53+
BEGIN
54+
-- reset the table
55+
PERFORM create_table_with_encrypted();
56+
57+
PERFORM eql_v2.add_search_config('encrypted', 'e', 'match');
58+
59+
PERFORM assert_exception(
60+
'Constraint catches invalid eql_v2_encrypted',
61+
'INSERT INTO encrypted (e) VALUES (''{}''::jsonb::eql_v2_encrypted)');
62+
63+
-- add constraint without error
64+
PERFORM eql_v2.add_encrypted_constraint('encrypted', 'e');
65+
66+
PERFORM eql_v2.remove_encrypted_constraint('encrypted', 'e');
67+
68+
PERFORM assert_result(
69+
'Insert invalid data without constraint',
70+
'INSERT INTO encrypted (e) VALUES (''{}''::jsonb::eql_v2_encrypted) RETURNING id');
71+
72+
END;
73+
$$ LANGUAGE plpgsql;
74+
75+
76+
-- -----------------------------------------------
77+
-- Adding column adds the constraint
78+
--
79+
-- -----------------------------------------------
80+
TRUNCATE TABLE eql_v2_configuration;
81+
82+
DO $$
83+
BEGIN
84+
-- reset the table
85+
PERFORM create_table_with_encrypted();
86+
87+
PERFORM eql_v2.add_column('encrypted', 'e');
88+
89+
PERFORM assert_exception(
90+
'Constraint catches invalid eql_v2_encrypted',
91+
'INSERT INTO encrypted (e) VALUES (''{}''::jsonb::eql_v2_encrypted)');
92+
93+
-- add constraint without error
94+
PERFORM eql_v2.add_encrypted_constraint('encrypted', 'e');
95+
96+
PERFORM eql_v2.remove_encrypted_constraint('encrypted', 'e');
97+
98+
PERFORM assert_result(
99+
'Insert invalid data without constraint',
100+
'INSERT INTO encrypted (e) VALUES (''{}''::jsonb::eql_v2_encrypted) RETURNING id');
101+
102+
END;
103+
$$ LANGUAGE plpgsql;
104+
105+
46106
-- EQL version is enforced
47107
DO $$
48108
DECLARE

src/encrypted/functions.sql

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ CREATE FUNCTION eql_v2.add_encrypted_constraint(table_name TEXT, column_name TEX
122122
RETURNS void
123123
AS $$
124124
BEGIN
125-
EXECUTE format('ALTER TABLE %I ADD CONSTRAINT eql_v2_encrypted_check_%I CHECK (eql_v2.check_encrypted(%I))', table_name, column_name, column_name);
126-
END;
125+
EXECUTE format('ALTER TABLE %I ADD CONSTRAINT eql_v2_encrypted_constraint_%I_%I CHECK (eql_v2.check_encrypted(%I))', table_name, table_name, column_name, column_name);
126+
EXCEPTION
127+
WHEN duplicate_table THEN
128+
WHEN duplicate_object THEN
129+
RAISE NOTICE 'Constraint `eql_v2_encrypted_constraint_%_%` already exists, skipping', table_name, column_name;
130+
END;
127131
$$ LANGUAGE plpgsql;
128132

129133
--! @brief Remove validation constraint from encrypted column
@@ -146,7 +150,7 @@ CREATE FUNCTION eql_v2.remove_encrypted_constraint(table_name TEXT, column_name
146150
RETURNS void
147151
AS $$
148152
BEGIN
149-
EXECUTE format('ALTER TABLE %I DROP CONSTRAINT IF EXISTS eql_v2_encrypted_check_%I', table_name, column_name);
153+
EXECUTE format('ALTER TABLE %I DROP CONSTRAINT IF EXISTS eql_v2_encrypted_constraint_%I_%I', table_name, table_name, column_name);
150154
END;
151155
$$ LANGUAGE plpgsql;
152156

src/encryptindex/functions.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ $$ LANGUAGE plpgsql;
9999
--!
100100
--! @return TABLE(table_name text, column_name text, target_column text) Column mappings
101101
--!
102-
--! @note Target column is NULL if encrypted column doesn't exist yet (LEFT JOIN returns NULL when no match)
103-
--! @note Target column type must be eql_v2_encrypted
102+
--! @note Target column is NULL if no column exists matching either 'column_name' or 'column_name_encrypted' with type eql_v2_encrypted
103+
--! @note The LEFT JOIN checks both original and '_encrypted' suffix variations with type verification
104104
--! @see eql_v2.select_pending_columns
105105
--! @see eql_v2.create_encrypted_columns
106106
CREATE FUNCTION eql_v2.select_target_columns()
@@ -149,7 +149,7 @@ $$ LANGUAGE sql;
149149
--!
150150
--! @return TABLE(table_name text, column_name text) Created encrypted columns
151151
--!
152-
--! @note Executes ALTER TABLE ADD COLUMN statements dynamically
152+
--! @warning Executes dynamic DDL (ALTER TABLE ADD COLUMN) - modifies database schema
153153
--! @note Only creates columns that don't already exist
154154
--! @see eql_v2.select_target_columns
155155
--! @see eql_v2.rename_encrypted_columns
@@ -177,7 +177,7 @@ $$ LANGUAGE plpgsql;
177177
--!
178178
--! @return TABLE(table_name text, column_name text, target_column text) Renamed columns
179179
--!
180-
--! @note Executes ALTER TABLE RENAME COLUMN statements dynamically
180+
--! @warning Executes dynamic DDL (ALTER TABLE RENAME COLUMN) - modifies database schema
181181
--! @note Only renames columns where target is '{column_name}_encrypted'
182182
--! @see eql_v2.create_encrypted_columns
183183
CREATE FUNCTION eql_v2.rename_encrypted_columns()
@@ -198,15 +198,15 @@ $$ LANGUAGE plpgsql;
198198
--! @brief Count rows encrypted with active configuration
199199
--! @internal
200200
--!
201-
--! Counts rows in a table where the encrypted column's version ('v' field)
202-
--! matches the active configuration ID. Used to track encryption progress.
201+
--! Counts rows in a table where the encrypted column was encrypted using
202+
--! the currently active configuration. Used to track encryption progress.
203203
--!
204204
--! @param table_name text Name of table to check
205205
--! @param column_name text Name of encrypted column to check
206-
--! @return bigint Count of rows matching active config version
206+
--! @return bigint Count of rows encrypted with active configuration
207207
--!
208-
--! @note Checks 'v' field in encrypted JSONB payload
209-
--! @note Compares to active configuration's ID
208+
--! @note The 'v' field in encrypted payloads stores the payload version ("2"), not the configuration ID
209+
--! @note Configuration tracking mechanism is implementation-specific
210210
CREATE FUNCTION eql_v2.count_encrypted_with_active_config(table_name TEXT, column_name TEXT)
211211
RETURNS BIGINT
212212
AS $$

src/encryptindex/functions_test.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ CREATE TABLE users
154154
-- An encrypting config should exist
155155
DO $$
156156
BEGIN
157-
PERFORM eql_v2.add_search_config('users', 'name', 'match', migrating => true);
157+
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match', migrating => true);
158158
PERFORM eql_v2.migrate_config();
159159
ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'active'));
160160
ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'encrypting'));
@@ -167,7 +167,7 @@ $$ LANGUAGE plpgsql;
167167
DO $$
168168
BEGIN
169169
TRUNCATE TABLE eql_v2_configuration;
170-
PERFORM eql_v2.add_search_config('users', 'name', 'match');
170+
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match');
171171
ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'active'));
172172
END;
173173
$$ LANGUAGE plpgsql;
@@ -177,7 +177,7 @@ $$ LANGUAGE plpgsql;
177177
DO $$
178178
BEGIN
179179
TRUNCATE TABLE eql_v2_configuration;
180-
PERFORM eql_v2.add_search_config('users', 'name', 'match');
180+
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match');
181181

182182
PERFORM assert_exception(
183183
'eql_v2.migrate_config() should raise an exception when no pending configuration exists',
@@ -226,7 +226,7 @@ CREATE TABLE users
226226
-- An encrypting config should exist
227227
DO $$
228228
BEGIN
229-
PERFORM eql_v2.add_search_config('users', 'name', 'match', migrating => true);
229+
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match', migrating => true);
230230
PERFORM eql_v2.migrate_config();
231231

232232
ASSERT (SELECT EXISTS (SELECT FROM eql_v2_configuration c WHERE c.state = 'active'));
@@ -276,7 +276,7 @@ CREATE TABLE users
276276
-- An encrypting config should exist
277277
DO $$
278278
BEGIN
279-
PERFORM eql_v2.add_search_config('users', 'name', 'match', migrating => true);
279+
PERFORM eql_v2.add_search_config('users', 'name_encrypted', 'match', migrating => true);
280280

281281
PERFORM eql_v2.migrate_config(); -- need to encrypt first
282282
PERFORM eql_v2.activate_config();

src/jsonb/functions.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
--!
2727
--! @note Returns empty set if selector is not found (does not throw exception)
2828
--! @note Array elements use same selector; multiple matches wrapped with 'a' flag
29-
--! @note Returns NULL if val is NULL, empty set if no matches
29+
--! @note Returns a set containing NULL if val is NULL; returns empty set if no matches found
3030
--! @see eql_v2.jsonb_path_query_first
3131
--! @see eql_v2.jsonb_path_exists
3232
CREATE FUNCTION eql_v2.jsonb_path_query(val jsonb, selector text)
@@ -223,7 +223,7 @@ AS $$
223223
BEGIN
224224
RETURN (
225225
SELECT e
226-
FROM eql_v2.jsonb_path_query(val.data, selector) AS e
226+
FROM eql_v2.jsonb_path_query(val, selector) AS e
227227
LIMIT 1
228228
);
229229
END;
@@ -293,7 +293,7 @@ $$ LANGUAGE plpgsql;
293293
--!
294294
--! @param val jsonb Encrypted JSONB payload representing an array
295295
--! @return integer Number of elements in the array
296-
--! @throws Exception if value is not an array (missing 'a' flag)
296+
--! @throws Exception 'cannot get array length of a non-array' if 'a' flag is missing or not true
297297
--!
298298
--! @note Array flag 'a' must be present and set to true value
299299
--! @see eql_v2.jsonb_array_elements

0 commit comments

Comments
 (0)