-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_data_export.sql
More file actions
227 lines (198 loc) · 6.5 KB
/
08_data_export.sql
File metadata and controls
227 lines (198 loc) · 6.5 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
-- PostgreSQL Data Anonymization in AWS RDS
-- Data export techniques
-- Create a function to generate an anonymized SQL dump
-- Note: This requires the pg_dump utility and appropriate permissions
CREATE OR REPLACE FUNCTION anon.generate_anonymized_dump(
output_file text,
schema_name text DEFAULT 'public'
)
RETURNS text AS
$$
DECLARE
cmd text;
result text;
BEGIN
-- Create a temporary view that combines all anonymized views
EXECUTE format('
CREATE OR REPLACE VIEW %I.anonymized_export AS
-- Users data
SELECT
''users'' AS source_table,
id,
anon.partial_email(email) AS email,
anon.fake_first_name() AS first_name,
anon.fake_last_name() AS last_name,
anon.hash_string(password) AS password_hash,
date_part(''year'', date_of_birth) AS birth_year,
anon.partial(ssn, 0, ''***-**-'', 4) AS partial_ssn,
anon.fake_address() AS address,
anon.partial(phone, 0, ''***-***-'', 4) AS phone,
created_at
FROM %I.users
UNION ALL
-- Patients data
SELECT
''patients'' AS source_table,
id,
''MRN'' || substring(anon.hash_string(mrn), 1, 8) AS mrn,
anon.fake_first_name() AS first_name,
anon.fake_last_name() AS last_name,
date_part(''year'', date_of_birth) AS birth_year,
anon.partial(ssn, 0, ''***-**-'', 4) AS partial_ssn,
diagnosis_code,
treatment_code,
NULL AS notes
FROM %I.patients
', schema_name, schema_name, schema_name);
-- Build the pg_dump command
cmd := format('pg_dump -t %I.anonymized_export -f %L', schema_name, output_file);
-- Execute the command (this would work in a procedural language with shell access)
-- In AWS RDS, you would need to use a different approach, such as:
-- 1. Using a database link to export to another PostgreSQL instance
-- 2. Using AWS Data Pipeline or AWS DMS
-- 3. Using a client-side script that queries the anonymized views
result := format('Command to execute: %s', cmd);
-- Clean up
EXECUTE format('DROP VIEW IF EXISTS %I.anonymized_export', schema_name);
RETURN result;
END;
$$
LANGUAGE plpgsql;
-- Create a function to export anonymized data to a CSV file
-- Note: This requires the COPY command and appropriate permissions
CREATE OR REPLACE FUNCTION anon.export_to_csv(
view_name text,
output_file text,
delimiter text DEFAULT ','
)
RETURNS text AS
$$
DECLARE
cmd text;
result text;
BEGIN
-- Build the COPY command
cmd := format('COPY %I TO %L WITH (FORMAT CSV, HEADER, DELIMITER %L)', view_name, output_file, delimiter);
-- In AWS RDS, you would typically use the COPY command with S3:
-- COPY view_name TO 's3://bucket-name/path/to/file.csv'
-- CREDENTIALS 'aws_iam_role=arn:aws:iam::account-id:role/role-name'
-- CSV HEADER;
result := format('Command to execute: %s', cmd);
RETURN result;
END;
$$
LANGUAGE plpgsql;
-- Create a function to generate a SQL script with anonymized INSERT statements
CREATE OR REPLACE FUNCTION anon.generate_anonymized_inserts(
table_name text,
output_table text DEFAULT NULL,
limit_rows int DEFAULT NULL
)
RETURNS text AS
$$
DECLARE
target_table text;
query text;
result text := '';
r record;
col_names text := '';
col_values text := '';
BEGIN
-- Determine the target table name
IF output_table IS NULL THEN
target_table := table_name || '_anonymized';
ELSE
target_table := output_table;
END IF;
-- Build the query based on the table
IF table_name = 'users' THEN
query := '
SELECT
id,
anon.partial_email(email) AS email,
anon.fake_first_name() AS first_name,
anon.fake_last_name() AS last_name,
anon.hash_string(password) AS password_hash,
date_of_birth,
anon.partial(ssn, 0, ''***-**-'', 4) AS ssn,
anon.fake_address() AS address,
anon.partial(phone, 0, ''***-***-'', 4) AS phone,
created_at
FROM users';
IF limit_rows IS NOT NULL THEN
query := query || format(' LIMIT %s', limit_rows);
END IF;
-- Generate the CREATE TABLE statement
result := result || format('
CREATE TABLE IF NOT EXISTS %I (
id INTEGER PRIMARY KEY,
email TEXT,
first_name TEXT,
last_name TEXT,
password_hash TEXT,
date_of_birth DATE,
ssn TEXT,
address TEXT,
phone TEXT,
created_at TIMESTAMP
);
', target_table);
-- Generate INSERT statements
FOR r IN EXECUTE query LOOP
-- Build column names and values
col_names := 'id, email, first_name, last_name, password_hash, date_of_birth, ssn, address, phone, created_at';
col_values := format('%s, %L, %L, %L, %L, %L, %L, %L, %L, %L',
r.id, r.email, r.first_name, r.last_name, r.password_hash,
r.date_of_birth, r.ssn, r.address, r.phone, r.created_at);
-- Add the INSERT statement
result := result || format('INSERT INTO %I (%s) VALUES (%s);
', target_table, col_names, col_values);
END LOOP;
ELSIF table_name = 'patients' THEN
query := '
SELECT
id,
''MRN'' || substring(anon.hash_string(mrn), 1, 8) AS mrn,
anon.fake_first_name() AS first_name,
anon.fake_last_name() AS last_name,
date_of_birth,
anon.partial(ssn, 0, ''***-**-'', 4) AS ssn,
diagnosis_code,
treatment_code
FROM patients';
IF limit_rows IS NOT NULL THEN
query := query || format(' LIMIT %s', limit_rows);
END IF;
-- Generate the CREATE TABLE statement
result := result || format('
CREATE TABLE IF NOT EXISTS %I (
id INTEGER PRIMARY KEY,
mrn TEXT,
first_name TEXT,
last_name TEXT,
date_of_birth DATE,
ssn TEXT,
diagnosis_code TEXT,
treatment_code TEXT
);
', target_table);
-- Generate INSERT statements
FOR r IN EXECUTE query LOOP
-- Build column names and values
col_names := 'id, mrn, first_name, last_name, date_of_birth, ssn, diagnosis_code, treatment_code';
col_values := format('%s, %L, %L, %L, %L, %L, %L, %L',
r.id, r.mrn, r.first_name, r.last_name,
r.date_of_birth, r.ssn, r.diagnosis_code, r.treatment_code);
-- Add the INSERT statement
result := result || format('INSERT INTO %I (%s) VALUES (%s);
', target_table, col_names, col_values);
END LOOP;
ELSE
RETURN format('Table %I is not supported for anonymized exports', table_name);
END IF;
RETURN result;
END;
$$
LANGUAGE plpgsql;
-- Example usage:
-- SELECT anon.generate_anonymized_inserts('users', 'users_anon', 10);