Skip to content

Commit aa3f683

Browse files
committed
fix: add constraint on add search config
1 parent 77b8935 commit aa3f683

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/config/functions.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ AS $$
6161
PERFORM eql_v2.activate_config();
6262
END IF;
6363

64-
-- PERFORM eql_v2.add_encrypted_constraint(table_name, column_name);
64+
PERFORM eql_v2.add_encrypted_constraint(table_name, column_name);
6565

6666
-- exeunt
6767
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
@@ -51,8 +51,12 @@ CREATE FUNCTION eql_v2.add_encrypted_constraint(table_name TEXT, column_name TEX
5151
RETURNS void
5252
AS $$
5353
BEGIN
54-
EXECUTE format('ALTER TABLE %I ADD CONSTRAINT eql_v2_encrypted_check_%I CHECK (eql_v2.check_encrypted(%I))', table_name, column_name, column_name);
55-
END;
54+
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);
55+
EXCEPTION
56+
WHEN duplicate_table THEN
57+
WHEN duplicate_object THEN
58+
RAISE NOTICE 'Constraint `eql_v2_encrypted_constraint_%_%` already exists, skipping', table_name, column_name;
59+
END;
5660
$$ LANGUAGE plpgsql;
5761

5862

@@ -66,7 +70,7 @@ CREATE FUNCTION eql_v2.remove_encrypted_constraint(table_name TEXT, column_name
6670
RETURNS void
6771
AS $$
6872
BEGIN
69-
EXECUTE format('ALTER TABLE %I DROP CONSTRAINT IF EXISTS eql_v2_encrypted_check_%I', table_name, column_name);
73+
EXECUTE format('ALTER TABLE %I DROP CONSTRAINT IF EXISTS eql_v2_encrypted_constraint_%I_%I', table_name, table_name, column_name);
7074
END;
7175
$$ LANGUAGE plpgsql;
7276

0 commit comments

Comments
 (0)