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

SQL › Changing data and transactions

How do you safely change data on a shared test environment?

On a shared environment only your own data is changed, data that can be reliably told from other people's, and this is verified before the change. A marker or owner column, a SELECT with a row-count check and a transaction with a rollback serve for that. Patterns are not trusted: in LIKE an underscore means any character. The best isolation is a separate schema, database or container per run.

SQL · Changing data and transactions

You removed your test accounts and your colleagues' autotests failed. What went wrong?

On a shared environment other testers, autotests and developers work. The data there belongs to nobody and to everybody. A typical story: QA deletes "its own" accounts by an email pattern and the pattern also catches other people's. This lesson is about telling your data from other people's and leaving no breakage behind.

How it works and why. A shared environment has no isolation: everyone uses the same tables. Three risks follow: you change other people's data, other people's changes break your test, and a long transaction or TRUNCATE blocks everyone. The protection has layers: a marker for your own data, a count check before the change, a transaction with a rollback, saved scripts and an agreement with the team.

Example: a pattern that caught other people. The users table has six rows. QA created qa_ana_1 and qa_ana_2 and removes them with the pattern LIKE 'qa_%@test.com':

idemailWhoseMatches the pattern?
1qa_ana_1@test.commineyes
2qa_ana_2@test.commineyes
3qa2team@test.coma colleague'syes
4qamobile@test.coma colleague'syes
5qa_robot@test.comautotests'yes
6anna@test.coma real userno

In a LIKE pattern the _ character means "any single character", so qa_% matches qa2team and qamobile. Two rows were expected and DELETE would touch 5. The protection works if you check the count before the change:

-- unreliable: _ is a wildcard
SELECT count(*) FROM users WHERE email LIKE 'qa_%@test.com';  -- 5, expected 2

-- the wildcard is escaped and the prefix is unique to the owner
SELECT count(*) FROM users
WHERE email LIKE 'qa!_ana!_%@test.com' ESCAPE '!';            -- 2

-- more reliable: delete by the ids RETURNING gave at creation
BEGIN;
SET LOCAL lock_timeout = '3s';
DELETE FROM users WHERE id IN (1, 2);
-- DELETE 2
COMMIT;

SET LOCAL lock_timeout lasts until the end of the transaction: if the needed row is busy, the command does not hang but ends with an error, and you do not block anyone by waiting. Even more reliable is a column for the owner or run (created_by, a tenant) in the table, filtered on.

Removing your test data

Wrong

A pattern with a common prefix

LIKE 'qa_%' catches other people's accounts and nobody checked the count.

Right

Ids from creation, count check, transaction

Exactly what was created is deleted, and a count mismatch is visible before COMMIT.

Figure 1. Your own data is recognised by identifiers, not by similar text.

Rules for a shared environment:

  • First make sure where you are: SELECT current_database(), current_user;.
  • Keep setup and cleanup scripts so they can be repeated instead of typed by hand each time.
  • Do not touch data that colleagues and autotests depend on; if needed, warn in the team channel.
  • Do not make long changes in a long transaction.
  • Production: no changes; reading only with permission. Real personal data is not copied into a test environment.

Limits and alternatives. The best isolation is your own schema, database or container per run (Testcontainers, a separate tenant): then patterns and markers are unnecessary. When a shared environment is unavoidable, each test creates its data with a unique suffix (a run id) and cleans up by it. Privileges also limit the risk: a QA role without TRUNCATE and DROP rights cannot do anything irreversible.

Common mistakes.

  • The _ or % wildcard in LIKE without escaping.
  • A change without a preceding SELECT and a count comparison.
  • One shared test account for everybody: tests spoil each other's state.
  • A cleanup script that does not return the environment to its initial state and leaves "garbage" for the next runs.
  • Changing shared reference data (countries, roles, settings) because "it is faster".

Check yourself

Why does email LIKE 'qa_%@test.com' match qamobile@test.com?

Answer

In LIKE _ stands for any one character and % for any sequence. Here _ matched "m" and % matched "obile". To search for a literal underscore, escape it: qa!_% with ESCAPE '!'.

Before DELETE you expected 2 rows and SELECT count(*) showed 5. What is the next step?

Answer

Do not run the change. Look at those 5 rows, narrow the condition or take exact ids, and repeat the check. A count mismatch is a signal to stop.

In an interview. Junior: On a shared environment I change only my own data, marked with a marker, run a SELECT first, and do the change in a transaction with a row count check. On production I change nothing. Middle/Senior: I do not trust patterns: in LIKE the underscore is a wildcard, so I clean up by the ids obtained at creation or by an owner column. I add lock_timeout so I do not block colleagues, keep the scripts and, where possible, take a separate schema or container per run.

More articles in this chapter