Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
QA interview question
Databases, SQL & BIDatabase conceptSeniorCommonTest design

How would you test a database schema migration with existing production data?

Answer

Test the migration against a representative, sanitized copy that includes old versions, large tables, invalid historical edge cases and realistic indexes and constraints. Verify forward migration, application compatibility during rollout, transformed values, row and control totals, locks and duration, restart after interruption, rollback or forward-fix strategy and backups. Rehearse the exact deployment order and prove that mixed application versions cannot corrupt data during a rolling release.

Strong answer includes

  • uses representative historical data and volume
  • covers compatibility, interruption and recovery
  • rehearses mixed-version rollout behaviour

Practical example

Backfill and verify a new constrained column
Dialect · PostgreSQLRuntime · static example

ALTER TABLE ... ALTER COLUMN ... SET NOT NULL is shown in PostgreSQL syntax; migration DDL differs substantially by DBMS.

ALTER TABLE orders ADD COLUMN currency CHAR(3);

UPDATE orders
SET currency = 'USD'
WHERE currency IS NULL;

SELECT COUNT(*) AS missing_currency
FROM orders
WHERE currency IS NULL;

ALTER TABLE orders ALTER COLUMN currency SET NOT NULL;

A safe migration separates schema introduction, data backfill, verification and constraint enforcement. Production rehearsal should also measure locks/duration and cover interruption, rollback or forward-fix behavior.

Expected result: The verification query returns zero before NOT NULL is enforced.

#database#sql#schema-migration#deployment

Sources

PostgreSQL documentationPostgreSQL Global Development Group · Relational modelling, SQL, joins, constraints, transactions, concurrency, indexes, query plans and recoveryGoogle SRE booksGoogle · Reliability, SLOs, monitoring, incidents and resilience