forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0034-migration.sql
51 lines (44 loc) · 1.64 KB
/
0034-migration.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
begin
-- Given an encrypted field from tc-lib-entities, this function will make the required transformations to the data
-- structure before storing it in a column.
create or replace function entity_to_crypto_container_v0(value jsonb, name text) returns jsonb
as $$
declare
chunks integer;
chunk integer = 0;
result jsonb = json_build_object('v', 0, 'kid', 'azure');
begin
chunks = (value ->> ('__bufchunks_' || name))::integer;
result = result || jsonb_build_object('__bufchunks_val', chunks);
loop
exit when chunks is null or chunk >= chunks;
result = jsonb_set(result, ('{__buf' || chunk || '_val}')::text[], value -> ('__buf' || chunk || '_' || name));
chunk = chunk + 1;
end loop;
return result;
end;
$$
language plpgSQL
strict immutable;
-- Replaces the keys in an encrypted column to be compatible with tc-lib-entities.
create or replace function encrypted_entity_buf_encode(value jsonb, name text, data jsonb) returns jsonb
as $$
declare
chunks integer;
chunk integer = 0;
result jsonb = jsonb_build_object();
begin
chunks = (data -> '__bufchunks_val')::integer;
result = jsonb_build_object('__bufchunks_' || name, chunks);
value = value || jsonb_build_object('__bufchunks_' || name, chunks);
loop
exit when chunks is null or chunk >= chunks;
value = value || jsonb_build_object('__buf' || chunk || '_' || name, data -> ('__buf' || chunk || '_val'));
chunk = chunk + 1;
end loop;
return value;
end;
$$
language plpgSQL
strict immutable;
end