@@ -7,79 +7,79 @@ create schema sanitizing;
7
7
-- - sanitize_value
8
8
-- - sanitize_nullable
9
9
-- - sanitize_jsonb
10
- create or replace function sanitizing .sanitize_email(_t varchar , _c varchar )
10
+ create or replace function sanitizing .sanitize_email(_t varchar , _c varchar , _s varchar )
11
11
returns void as
12
12
$$
13
13
declare
14
14
affected numeric ;
15
15
begin
16
16
execute format(
17
- ' with data as (
18
- select
19
- id,
20
- string_agg(
21
- right(md5(e), 8) || substring(e, ' ' @.+$' ' ) || ' ' t' ' ,
22
- ' ' ,' '
23
- ) as sanitized
24
- from %1$I, unnest(regexp_split_to_array(%2$I, ' ' ,' ' )) e
25
- group by id
26
- )
27
- update %1$I
28
- set %2$I = case when sanitized isnull then ' ' [email protected] ' ' else sanitized end
29
- from data
30
- where data.id = %1$I.id;' , _t, _c);
17
+ ' with data as (
18
+ select
19
+ id,
20
+ string_agg(
21
+ right(md5(e), 8) || substring(e, ' ' @.+$' ' ) || ' ' t' ' ,
22
+ ' ' ,' '
23
+ ) as sanitized
24
+ from %3$I. %1$I, unnest(regexp_split_to_array(%2$I, ' ' ,' ' )) e
25
+ group by id
26
+ )
27
+ update %3$I. %1$I
28
+ set %2$I = case when sanitized isnull then ' ' [email protected] ' ' else sanitized end
29
+ from data
30
+ where data.id = %3$I.% 1$I.id;' , _t, _c, _s );
31
31
get diagnostics affected = row_count;
32
32
raise notice ' %.% is sanitized: % rows affected' , _t, _c, affected;
33
33
end
34
34
$$
35
35
language plpgsql;
36
36
37
- create or replace function sanitizing .sanitize_phone(_t varchar , _c varchar )
37
+ create or replace function sanitizing .sanitize_phone(_t varchar , _c varchar , _s varchar )
38
38
returns void as
39
39
$$
40
40
declare
41
41
affected numeric ;
42
42
begin
43
- execute format(' update %1$I set %2$I = ' ' 93000000' ' where %2$I is not null;' , _t, _c);
43
+ execute format(' update %3$I.% 1$I set %2$I = ' ' 93000000' ' where %2$I is not null;' , _t, _c, _s );
44
44
get diagnostics affected = row_count;
45
45
raise notice ' %.% is sanitized: % rows affected' , _t, _c, affected;
46
46
end
47
47
$$
48
48
language plpgsql;
49
49
50
- create or replace function sanitizing .sanitize_value(_t varchar , _c varchar )
50
+ create or replace function sanitizing .sanitize_value(_t varchar , _c varchar , _s varchar )
51
51
returns void as
52
52
$$
53
53
declare
54
54
affected numeric ;
55
55
begin
56
- execute format(' update %1$I set %2$I = right(md5(%2$I), 12);' , _t, _c);
56
+ execute format(' update %3$I.% 1$I set %2$I = right(md5(%2$I), 12);' , _t, _c, _s );
57
57
get diagnostics affected = row_count;
58
58
raise notice ' %.% is sanitized: % rows affected' , _t, _c, affected;
59
59
end
60
60
$$
61
61
language plpgsql;
62
62
63
- create or replace function sanitizing .sanitize_nullable(_t varchar , _c varchar )
63
+ create or replace function sanitizing .sanitize_nullable(_t varchar , _c varchar , _s varchar )
64
64
returns void as
65
65
$$
66
66
declare
67
67
affected numeric ;
68
68
begin
69
- execute format(' update %I set %I = null;' , _t, _c);
69
+ execute format(' update %I.%I set %I = null;' , _s , _t, _c);
70
70
get diagnostics affected = row_count;
71
71
raise notice ' %.% is sanitized: % rows affected' , _t, _c, affected;
72
72
end
73
73
$$
74
74
language plpgsql;
75
75
76
- create or replace function sanitizing .sanitize_as_empty_jsonb(_t varchar , _c varchar )
76
+ create or replace function sanitizing .sanitize_as_empty_jsonb(_t varchar , _c varchar , _s varchar )
77
77
returns void as
78
78
$$
79
79
declare
80
80
affected numeric ;
81
81
begin
82
- execute format(' update %I set %I = ' ' {}' ' ::jsonb;' , _t, _c);
82
+ execute format(' update %I.%I set %I = ' ' {}' ' ::jsonb;' , _s , _t, _c);
83
83
get diagnostics affected = row_count;
84
84
raise notice ' %.% is sanitized: % rows affected' , _t, _c, affected;
85
85
end
@@ -117,13 +117,13 @@ end
117
117
$$
118
118
language plpgsql;
119
119
120
- create or replace function sanitizing .sanitize_jsonb(_t varchar , _c varchar )
120
+ create or replace function sanitizing .sanitize_jsonb(_t varchar , _c varchar , _s varchar )
121
121
returns void as
122
122
$$
123
123
declare
124
124
affected numeric ;
125
125
begin
126
- execute format(' update %I set %I = sanitizing.get_sanitized_jsonb(%I);' , _t, _c, _c);
126
+ execute format(' update %I.%I set %I = sanitizing.get_sanitized_jsonb(%I);' , _s , _t, _c, _c);
127
127
get diagnostics affected = row_count;
128
128
raise notice ' %.% is sanitized: % rows affected' , _t, _c, affected;
129
129
end
@@ -145,18 +145,22 @@ from pg_catalog.pg_statio_all_tables as st
145
145
-- truncate table which are not needed for the development before the sanitizing (logs, emails etc);
146
146
-- a table must have 'TRUNCATE_ON_SANITIZE' comment
147
147
do $$
148
- declare t text ;
149
- begin
150
- for t in select relname from pg_class
151
- where relkind = ' r' and obj_description(oid ) = ' TRUNCATE_ON_SANITIZE' loop
152
- execute ' truncate table ' || quote_ident(t) || ' cascade' ; -- cascade to drop formDataImages etc
153
- end loop;
154
- end
148
+ declare r record;
149
+ begin
150
+ for r in select nspname, relname
151
+ from pg_class
152
+ join pg_namespace on pg_namespace .oid = pg_class .relnamespace
153
+ join pg_description on pg_class .oid = pg_description .objoid
154
+ where pg_description .description = ' TRUNCATE_ON_SANITIZE' and pg_class .relkind = ' r' loop
155
+ execute format(' truncate table %I.%I cascade' , r .nspname , r .relname ); -- cascade to drop formDataImages etc
156
+ end loop;
157
+ end
155
158
$$;
156
159
157
160
-- call sanitize_email on every column containing SANITIZE_AS_EMAIL in the comment
158
- select sanitizing .sanitize_email (res .table_name :: varchar , res .column_name :: varchar )
161
+ select sanitizing .sanitize_email (res .table_name :: varchar , res .column_name :: varchar , res . table_schema :: varchar )
159
162
from (select
163
+ c .table_schema ,
160
164
c .table_name ,
161
165
c .column_name
162
166
from pg_catalog .pg_statio_all_tables as st
@@ -166,8 +170,9 @@ from (select
166
170
where pgd .description ilike ' %SANITIZE_AS_EMAIL%' ) as res;
167
171
168
172
-- call sanitize_value on every column containing SANITIZE_AS_VALUE in the comment
169
- select sanitizing .sanitize_value (res .table_name :: varchar , res .column_name :: varchar )
173
+ select sanitizing .sanitize_value (res .table_name :: varchar , res .column_name :: varchar , res . table_schema :: varchar )
170
174
from (select
175
+ c .table_schema ,
171
176
c .table_name ,
172
177
c .column_name
173
178
from pg_catalog .pg_statio_all_tables as st
@@ -177,8 +182,9 @@ from (select
177
182
where pgd .description ilike ' %SANITIZE_AS_VALUE%' ) as res;
178
183
179
184
-- call sanitize_phone on every column containing SANITIZE_AS_PHONE in the comment
180
- select sanitizing .sanitize_phone (res .table_name :: varchar , res .column_name :: varchar )
185
+ select sanitizing .sanitize_phone (res .table_name :: varchar , res .column_name :: varchar , res . table_schema :: varchar )
181
186
from (select
187
+ c .table_schema ,
182
188
c .table_name ,
183
189
c .column_name
184
190
from pg_catalog .pg_statio_all_tables as st
@@ -188,8 +194,9 @@ from (select
188
194
where pgd .description ilike ' %SANITIZE_AS_PHONE%' ) as res;
189
195
190
196
-- call sanitize_nullable on every column containing SANITIZE_AS_NULLABLE in the comment
191
- select sanitizing .sanitize_nullable (res .table_name :: varchar , res .column_name :: varchar )
197
+ select sanitizing .sanitize_nullable (res .table_name :: varchar , res .column_name :: varchar , res . table_schema :: varchar )
192
198
from (select
199
+ c .table_schema ,
193
200
c .table_name ,
194
201
c .column_name
195
202
from pg_catalog .pg_statio_all_tables as st
@@ -199,8 +206,9 @@ from (select
199
206
where pgd .description ilike ' %SANITIZE_AS_NULLABLE%' ) as res;
200
207
201
208
-- call sanitize_jsonb on every column containing SANITIZE_AS_JSONB in the comment
202
- select sanitizing .sanitize_jsonb (res .table_name :: varchar , res .column_name :: varchar )
209
+ select sanitizing .sanitize_jsonb (res .table_name :: varchar , res .column_name :: varchar , res . table_schema :: varchar )
203
210
from (select
211
+ c .table_schema ,
204
212
c .table_name ,
205
213
c .column_name
206
214
from pg_catalog .pg_statio_all_tables as st
@@ -210,8 +218,9 @@ from (select
210
218
where pgd .description ilike ' %SANITIZE_AS_JSONB%' ) as res;
211
219
212
220
-- call sanitize_as_empty_jsonb on every column containing SANITIZE_AS_EMPTY_JSONB in the comment
213
- select sanitizing .sanitize_as_empty_jsonb (res .table_name :: varchar , res .column_name :: varchar )
221
+ select sanitizing .sanitize_as_empty_jsonb (res .table_name :: varchar , res .column_name :: varchar , res . table_schema :: varchar )
214
222
from (select
223
+ c .table_schema ,
215
224
c .table_name ,
216
225
c .column_name
217
226
from pg_catalog .pg_statio_all_tables as st
0 commit comments