Skip to content

Commit d14dea3

Browse files
committed
chore: split sql into modules
1 parent a996a21 commit d14dea3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+4427
-0
lines changed

src/common.sql

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- AUTOMATICALLY GENERATED FILE
2+
-- REQUIRE: src/schema.sql
3+
4+
5+
6+
--
7+
-- Convenience function to log a message
8+
--
9+
DROP FUNCTION IF EXISTS eql_v1.log(text);
10+
CREATE FUNCTION eql_v1.log(s text)
11+
RETURNS void
12+
AS $$
13+
BEGIN
14+
RAISE NOTICE '[LOG] %', s;
15+
END;
16+
$$ LANGUAGE plpgsql;
17+
18+
19+
--
20+
-- Convenience function to describe a test
21+
--
22+
DROP FUNCTION IF EXISTS eql_v1.log(text, text);
23+
CREATE FUNCTION eql_v1.log(ctx text, s text)
24+
RETURNS void
25+
AS $$
26+
BEGIN
27+
RAISE NOTICE '[LOG] % %', ctx, s;
28+
END;
29+
$$ LANGUAGE plpgsql;

src/config/config_test.sql

Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
\set ON_ERROR_STOP on
2+
3+
4+
--
5+
-- Helper function for assertions
6+
--
7+
DROP FUNCTION IF EXISTS _index_exists(text, text, text, text);
8+
CREATE FUNCTION _index_exists(table_name text, column_name text, index_name text, state text DEFAULT 'pending')
9+
RETURNS boolean
10+
LANGUAGE sql STRICT PARALLEL SAFE
11+
BEGIN ATOMIC
12+
SELECT EXISTS (SELECT id FROM eql_v1_configuration c
13+
WHERE c.state = state AND
14+
c.data #> array['tables', table_name, column_name, 'indexes'] ? index_name);
15+
END;
16+
17+
18+
-- -----------------------------------------------
19+
-- Add and remove multiple indexes
20+
--
21+
-- -----------------------------------------------
22+
TRUNCATE TABLE eql_v1_configuration;
23+
24+
25+
DO $$
26+
BEGIN
27+
28+
-- Add indexes
29+
PERFORM eql_v1.add_index('users', 'name', 'match');
30+
ASSERT (SELECT _index_exists('users', 'name', 'match'));
31+
32+
-- -- Add index with cast
33+
-- PERFORM eql_v1.add_index('users', 'name', 'unique', 'int');
34+
-- ASSERT (SELECT _index_exists('users', 'name', 'unique'));
35+
36+
-- ASSERT (SELECT EXISTS (SELECT id FROM eql_v1_configuration c
37+
-- WHERE c.state = 'pending' AND
38+
-- c.data #> array['tables', 'users', 'name'] ? 'cast_as'));
39+
40+
-- -- Match index removed
41+
-- PERFORM eql_v1.remove_index('users', 'name', 'match');
42+
-- ASSERT NOT (SELECT _index_exists('users', 'name', 'match'));
43+
44+
-- -- All indexes removed, delete the emtpty pending config
45+
-- PERFORM eql_v1.remove_index('users', 'name', 'unique');
46+
-- ASSERT (SELECT NOT EXISTS (SELECT FROM eql_v1_configuration c WHERE c.state = 'pending'));
47+
48+
END;
49+
$$ LANGUAGE plpgsql;
50+
51+
52+
53+
-- -----------------------------------------------
54+
-- Add and remove multiple indexes from multiple tables
55+
--
56+
-- -----------------------------------------------
57+
TRUNCATE TABLE eql_v1_configuration;
58+
59+
60+
DO $$
61+
BEGIN
62+
63+
-- Add indexes
64+
PERFORM eql_v1.add_index('users', 'name', 'match');
65+
ASSERT (SELECT _index_exists('users', 'name', 'match'));
66+
67+
ASSERT (SELECT EXISTS (SELECT id FROM eql_v1_configuration c
68+
WHERE c.state = 'pending' AND
69+
c.data #> array['tables', 'users', 'name', 'indexes'] ? 'match'));
70+
71+
-- Add index with cast
72+
PERFORM eql_v1.add_index('blah', 'vtha', 'unique', 'int');
73+
ASSERT (SELECT _index_exists('blah', 'vtha', 'unique'));
74+
75+
ASSERT (SELECT EXISTS (SELECT id FROM eql_v1_configuration c
76+
WHERE c.state = 'pending' AND
77+
c.data #> array['tables', 'users', 'name', 'indexes'] ? 'match'));
78+
79+
80+
ASSERT (SELECT EXISTS (SELECT id FROM eql_v1_configuration c
81+
WHERE c.state = 'pending' AND
82+
c.data #> array['tables', 'blah', 'vtha', 'indexes'] ? 'unique'));
83+
84+
85+
-- Match index removed
86+
PERFORM eql_v1.remove_index('users', 'name', 'match');
87+
ASSERT NOT (SELECT _index_exists('users', 'name', 'match'));
88+
89+
-- Match index removed
90+
PERFORM eql_v1.remove_index('blah', 'vtha', 'unique');
91+
ASSERT NOT (SELECT _index_exists('users', 'vtha', 'unique'));
92+
93+
-- All indexes removed, delete the emtpty pending config
94+
ASSERT (SELECT NOT EXISTS (SELECT FROM eql_v1_configuration c WHERE c.state = 'pending'));
95+
96+
END;
97+
$$ LANGUAGE plpgsql;
98+
99+
SELECT FROM eql_v1_configuration c WHERE c.state = 'pending';
100+
101+
102+
-- -----------------------------------------------
103+
-- Add & modify index
104+
-- Pending configuration created and contains the path `user/name.match.option`
105+
-- -----------------------------------------------
106+
-- TRUNCATE TABLE eql_v1_configuration;
107+
108+
109+
DO $$
110+
BEGIN
111+
PERFORM eql_v1.add_index('users', 'name', 'match');
112+
ASSERT (SELECT _index_exists('users', 'name', 'match'));
113+
114+
-- Pending configuration contains the path `user/name.match.option`
115+
PERFORM eql_v1.modify_index('users', 'name', 'match', 'int', '{"option": "value"}'::jsonb);
116+
ASSERT (SELECT _index_exists('users', 'name', 'match'));
117+
118+
ASSERT (SELECT EXISTS (SELECT id FROM eql_v1_configuration c
119+
WHERE c.state = 'pending' AND
120+
c.data #> array['tables', 'users', 'name', 'indexes', 'match'] ? 'option'));
121+
122+
ASSERT (SELECT EXISTS (SELECT id FROM eql_v1_configuration c
123+
WHERE c.state = 'pending' AND
124+
c.data #> array['tables', 'users', 'name'] ? 'cast_as'));
125+
126+
-- All indexes removed, delete the emtpty pending config
127+
PERFORM eql_v1.remove_index('users', 'name', 'match');
128+
ASSERT (SELECT NOT EXISTS (SELECT FROM eql_v1_configuration c WHERE c.state = 'pending'));
129+
END;
130+
$$ LANGUAGE plpgsql;
131+
132+
133+
-- -- -----------------------------------------------
134+
-- -- With existing active config
135+
-- -- Adding an index creates a new pending configuration
136+
-- -- -----------------------------------------------
137+
TRUNCATE TABLE eql_v1_configuration;
138+
139+
-- create an active configuration
140+
INSERT INTO eql_v1_configuration (state, data) VALUES (
141+
'active',
142+
'{
143+
"v": 1,
144+
"tables": {
145+
"users": {
146+
"blah": {
147+
"cast_as": "text",
148+
"indexes": {
149+
"match": {}
150+
}
151+
},
152+
"vtha": {
153+
"cast_as": "text",
154+
"indexes": {}
155+
}
156+
}
157+
}
158+
}'::jsonb
159+
);
160+
161+
-- An encrypting config should exist
162+
DO $$
163+
BEGIN
164+
ASSERT (SELECT _index_exists('users', 'blah', 'match', 'active'));
165+
166+
PERFORM eql_v1.add_index('users', 'name', 'match');
167+
168+
-- index added to name
169+
ASSERT (SELECT _index_exists('users', 'name', 'match' ));
170+
171+
-- pending is a copy of the active config
172+
-- and the active index still exists
173+
ASSERT (SELECT _index_exists('users', 'blah', 'match'));
174+
175+
END;
176+
$$ LANGUAGE plpgsql;
177+
178+
179+
-- -- -----------------------------------------------
180+
-- -- Add and remove column
181+
-- --
182+
-- -- -----------------------------------------------
183+
TRUNCATE TABLE eql_v1_configuration;
184+
DO $$
185+
BEGIN
186+
-- Create pending configuration
187+
PERFORM eql_v1.add_column('user', 'name');
188+
ASSERT (SELECT EXISTS (SELECT FROM eql_v1_configuration c WHERE c.state = 'pending'));
189+
190+
PERFORM eql_v1.remove_column('user', 'name');
191+
192+
-- Config now empty and removed
193+
ASSERT (SELECT NOT EXISTS (SELECT FROM eql_v1_configuration c WHERE c.state = 'pending'));
194+
END;
195+
$$ LANGUAGE plpgsql;
196+
197+
-- -----------------------------------------------
198+
---
199+
-- eql_v1_configuration tyoe
200+
-- Validate configuration schema
201+
-- Try and insert many invalid configurations
202+
-- None should exist
203+
--
204+
-- -----------------------------------------------
205+
TRUNCATE TABLE eql_v1_configuration;
206+
207+
\set ON_ERROR_STOP off
208+
\set ON_ERROR_ROLLBACK on
209+
210+
DO $$
211+
BEGIN
212+
RAISE NOTICE '------------------------------------------------------';
213+
RAISE NOTICE 'eql_v1_configuration constraint tests: 4 errors follow';
214+
END;
215+
$$ LANGUAGE plpgsql;
216+
--
217+
-- No schema version
218+
INSERT INTO eql_v1_configuration (data) VALUES (
219+
'{
220+
"tables": {
221+
"users": {
222+
"blah": {
223+
"cast_as": "text",
224+
"indexes": {}
225+
}
226+
}
227+
}
228+
}'::jsonb
229+
);
230+
231+
--
232+
-- Empty tables
233+
INSERT INTO eql_v1_configuration (data) VALUES (
234+
'{
235+
"v": 1,
236+
"tables": {}
237+
}'::jsonb
238+
);
239+
240+
241+
--
242+
-- invalid cast
243+
INSERT INTO eql_v1_configuration (data) VALUES (
244+
'{
245+
"v": 1,
246+
"tables": {
247+
"users": {
248+
"blah": {
249+
"cast_as": "regex"
250+
}
251+
}
252+
}
253+
}'::jsonb
254+
);
255+
256+
--
257+
-- invalid index
258+
INSERT INTO eql_v1_configuration (data) VALUES (
259+
'{
260+
"v": 1,
261+
"tables": {
262+
"users": {
263+
"blah": {
264+
"cast_as": "text",
265+
"indexes": {
266+
"blah": {}
267+
}
268+
}
269+
}
270+
}
271+
}'::jsonb
272+
);
273+
274+
275+
-- Pending configuration should not be created;
276+
DO $$
277+
BEGIN
278+
ASSERT (SELECT NOT EXISTS (SELECT FROM eql_v1_configuration c WHERE c.state = 'pending'));
279+
END;
280+
$$ LANGUAGE plpgsql;
281+
282+
DO $$
283+
BEGIN
284+
RAISE NOTICE 'eql_v1_configuration constraint tests: OK';
285+
RAISE NOTICE '------------------------------------------------------';
286+
END;
287+
$$ LANGUAGE plpgsql;
288+
289+
290+
\set ON_ERROR_STOP on
291+
\set ON_ERROR_ROLLBACK off
292+
293+
294+
295+

0 commit comments

Comments
 (0)