QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › SQL in QA work

How do you use a copy of the production database for tests without breaking privacy?

Real personal data is not copied into a test database: it is masked before the copy leaves the perimeter, or synthetic data is used. The mask must be stable across tables to keep the links. A hash, even with a secret key, is not anonymisation. QA scans for leftovers with queries against the mask and checks integrity, formats and every copy, including logs and dumps.

SQL · SQL in QA work

The test database holds real customer emails, and a dump ended up in a CI attachment. How to avoid that?

A copy of the production database gives the most realistic data: real volumes, dirty values, rare combinations. But together with them the personal information of customers travels into the test environment, logs, snapshots and dumps. A team that once "just copied production" turns every laptop and every CI artifact into a place where a leak can happen. This lesson is about making the copy fit for tests and safe: masking, its limits and the checks QA performs.

How it works and why. Two notions are distinguished. Pseudonymisation replaces identifiers but keeps the ability to restore them with additional information (a mapping table, a key). Recital 26 of the GDPR says such data is still treated as information about an identifiable person. Anonymisation makes the person not identifiable, and then the data protection principles do not apply; this is judged by whether identification is "reasonably likely" given cost, time and available technology. For tests, in practice this means: masking happens before the copy leaves the protected perimeter, and whether it is truly anonymous is decided together with the person responsible for data protection. This is not legal advice.

TechniqueExampleRisk
Substitutionuser1@example.testloses format and distribution
Hash with a secret key (HMAC)same input, same outputwithout the key values are guessed
Generalisationbirth date, year onlyrare combinations
NULLphone = NULLbreaks phone tests

Example: masking and links between tables. The schema has people (name, email, phone) and logins, where the email repeats. Masking one table breaks the link:

UPDATE people SET
  full_name = 'User ' || id,
  email = 'user' || id || '@example.test',
  phone = '+3800000' || lpad(id::text, 5, '0');

SELECT count(*) FROM people p
JOIN logins l ON l.email = p.email;  -- 0

Before masking both rows joined, after it the result is 0: logins kept the real addresses, so it is a leak and a lost link at once. The fix: first collect the mapping of old to new addresses, mask both tables through it, then delete the mapping itself: it is the key to re-identification. Then count(*) is 2 again and the leftover scan returns 0.

Only people was masked

people
idemail
1user1@example.test
2user2@example.test
logins
email
anna@mail.test
ivan@mail.test
Figure 1. In people the addresses are already masked, in logins the real ones stayed: a JOIN on email finds no pair, and the real addresses are still in the test database.
-- instead of the UPDATE people above, on the still unmasked copy
CREATE TEMP TABLE email_map AS
  SELECT email AS old,
         'user' || id || '@example.test' AS new
  FROM people;
UPDATE logins l SET email = m.new
  FROM email_map m WHERE l.email = m.old;
UPDATE people p SET email = m.new
  FROM email_map m WHERE p.email = m.old;
DROP TABLE email_map;

SELECT count(*) FROM logins
WHERE email !~ '@example\.test$';  -- 0

What QA checks.

  • A scan for leftovers: a query that looks for values that do not match the mask. In the example WHERE email !~ '@example\.test$' returned 0 rows, and after one name was edited by hand the check against the name mask found that row.
  • Integrity: foreign keys, uniqueness and links by value (like the email above) still work.
  • Fitness: phone and email formats pass the application validation, and the data distribution did not become identical for every row.
  • All copies: free text, JSON fields, log tables, replicas, backups and dumps QA creates itself.

Limits and alternatives. Plain hashes of an email or a phone do not protect: the value space is small and they are guessed by brute force, so a hash with a secret key (HMAC) is used, with the key stored apart from the data; this is still pseudonymisation, not anonymisation. Even without names a person can be recognised by a combination of city, birth date and job title, so rare combinations are generalised. When masking is impossible or the risk is high, synthetic data replaces the copy: it is generated from the schema and domain rules, and dirty cases are added by hand. This gives reproducibility, but real production anomalies have to be invented. The approach depends on the risk level and the rules of the organisation.

Common mistakes.

  • Masking only the main table and leaving the same values in neighbouring tables, logs or JSON: the leak stays.
  • Masking each table randomly and differently: links by value stop matching and tests fail with no cause in the code.
  • Treating a hash, even a keyed one, as anonymisation.
  • Masking already on the test environment: by then the real data is outside the perimeter.

Check yourself

After masking, a login-by-email test fails, although people and logins used to share the addresses. What most likely happened?

Answer

The email was masked in one table and the real values stayed in the other, so the link by value disappeared and a leak appeared as well. The mask is computed once and applied to every table, for example through a mapping of old to new addresses.

A team replaced the email with md5(email) and considers the data anonymous. What do you say?

Answer

At best this is pseudonymisation: the value space is small and the hash can be recovered by brute-forcing known addresses. A hash with a secret key stored separately makes guessing harder, but the data stays pseudonymised. Anonymity needs generated replacement values and generalised rare combinations.

In an interview. Junior: Real personal data is not copied into a test database: it is masked or generated data is used. Middle/Senior: I distinguish pseudonymisation from anonymisation, I mask before the copy leaves the perimeter by a stable rule to keep the links, and I check for leftovers with queries against the mask. I do not count a hash as anonymisation, and for risky data I use synthetic data.

More articles in this chapter