Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 40/ 160
02. Advanced SQL & Relational Databases

Database Migration Tools (Alembic, Flyway, Liquibase)

Version schema changes safely with migration tools that apply ordered, reviewable DDL.

IntermediateAlembic · Flyway · Liquibase3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Add compatible structures and code paths. Next, move old rows in bounded, resumable batches. Finally, remove legacy columns in a later release.

Think of it like…

A schema migration is a bridge replacement beside live traffic: add a bypass, move traffic, verify capacity, and only then remove the old bridge.

Where you’ve seen it

Add nullable tenant_id, deploy code that writes it, backfill UPDATE ... WHERE id > $last LIMIT 10_000, add NOT VALID FK/CHECK, validate in a low-lock pass, then enforce and drop the legacy column later.

Words you’ll hear

Alembic
Python SQLAlchemy's revision graph and migration scripting tool for ordered schema and data changes.
Flyway
A JVM-style migration runner driven by ordered SQL or Java files and a schema history table.
Liquibase
A change-set migration format with checksums, contexts, and database-abstracted operations.
Operational boundary
A single NOT NULL/table rewrite migration can lock a large table or fail after old data violates the new rule, and one developer running downgrade can lose data that a code rollback alone cannot reconstruct. Use expand-migrate-contract, small idempotent steps, online/index-concurrent options where supported, backfill in bounded batches, validate before enforcing constraints, and treat destructive rollback as an explicit data recovery plan.

Visual diagram

Loading interactive diagram

Theory & deep dive

Alembic: the working mechanism

Migration tools order versioned DDL/DML changes, record which revisions ran, and provide reviewable upgrade and sometimes downgrade paths across environments.

Add compatible structures and code paths. Move old rows in bounded, resumable batches, then validate constraints and consumer compatibility; this exposes the state governed by Liquibase.

Database Migration Tools in one concrete design

Add nullable tenant_id, deploy code that writes it, backfill UPDATE ... WHERE id > $last LIMIT 10_000, add NOT VALID FK/CHECK, validate in a low-lock pass, then enforce and drop the legacy column later.

Key takeaway: Migration tools order versioned DDL/DML changes, record which revisions ran, and provide reviewable upgrade and sometimes downgrade paths across environments

Database Migration Tools: failure mode and production response

A single NOT NULL/table rewrite migration can lock a large table or fail after old data violates the new rule, and one developer running downgrade can lose data that a code rollback alone cannot reconstruct.

Use expand-migrate-contract, small idempotent steps, online/index-concurrent options where supported, backfill in bounded batches, validate before enforcing constraints, and treat destructive rollback as an explicit data recovery plan. Track Revision checksum as "detect modified migrations already applied", and make Lock time visible to the dataset owner.

Key takeaway: Use expand-migrate-contract, small idempotent steps, online/index-concurrent options where supported, backfill in bounded batches, validate before enforcing constraints, and treat destructive rollback as an explicit data recovery plan.

How it works, step by step

Step 1 of 4
Alembic

1. Expand

Add compatible structures and code paths. Migration tools order versioned DDL/DML changes, record which revisions ran, and provide reviewable upgrade and sometimes downgrade paths across environments.

add compatible structures and code paths
Key numbers at this step
Revision checksum
detect modified migrations already applied
Flyway
A JVM-style migration runner driven by ordered SQL or Java files and a schema history table.

When to use it & trade-offs

What it is

Migration tools order versioned DDL/DML changes, record which revisions ran, and provide reviewable upgrade and sometimes downgrade paths across environments. Add nullable tenant_id, deploy code that writes it, backfill UPDATE ... WHERE id > $last LIMIT 10_000, add NOT VALID FK/CHECK, validate in a low-lock pass, then enforce and drop the legacy column later.

Why it matters: A single NOT NULL/table rewrite migration can lock a large table or fail after old data violates the new rule, and one developer running downgrade can lose data that a code rollback alone cannot reconstruct. Use expand-migrate-contract, small idempotent steps, online/index-concurrent options where supported, backfill in bounded batches, validate before enforcing constraints, and treat destructive rollback as an explicit data recovery plan.

When to reach for it

  • Version schema changes safely with migration tools that apply ordered, reviewable DDL.
  • Choose Alembic because it provides this capability: Python SQLAlchemy's revision graph and migration scripting tool for ordered schema and data changes.
  • Keep evidence for Revision checksum at "detect modified migrations already applied" so the last production stage-Retire - remove legacy columns in a later release-remains testable.

When to avoid or be careful

  • A single NOT NULL/table rewrite migration can lock a large table or fail after old data violates the new rule, and one developer running downgrade can lose data that a code rollback alone cannot reconstruct.
  • Do not conflate Flyway with Alembic. The second concept contributes different behavior: A JVM-style migration runner driven by ordered SQL or Java files and a schema history table.
  • Release only after this guardrail is in place: Use expand-migrate-contract, small idempotent steps, online/index-concurrent options where supported, backfill in bounded batches, validate before enforcing constraints, and treat destructive rollback as an explicit data recovery plan.

Alembic / Flyway / Liquibase - roles, limits, and operating cost

Alembic
Best for: Python SQLAlchemy's revision graph and migration scripting tool for ordered schema and data changes.

Strengths: Python SQLAlchemy's revision graph and migration scripting tool for ordered schema and data changes. Migration tools order versioned DDL/DML changes, record which revisions ran, and provide reviewable upgrade and sometimes downgrade paths across environments.

Watch out: A single NOT NULL/table rewrite migration can lock a large table or fail after old data violates the new rule, and one developer running downgrade can lose data that a code rollback alone cannot reconstruct.

Flyway
Best for: A JVM-style migration runner driven by ordered SQL or Java files and a schema history table.

Strengths: A JVM-style migration runner driven by ordered SQL or Java files and a schema history table. In this workflow it supports: Contract gate - validate constraints and consumer compatibility.

Watch out: The operating cost is real: ALTER duration and blocked sessions must fit SLO.

Liquibase
Best for: A change-set migration format with checksums, contexts, and database-abstracted operations.

Strengths: A change-set migration format with checksums, contexts, and database-abstracted operations. Its output can be checked with Backfill checkpoint.

Watch out: Operating guardrail: Use expand-migrate-contract, small idempotent steps, online/index-concurrent options where supported, backfill in bounded batches, validate before enforcing constraints, and treat destructive rollback as an explicit data recovery plan.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Alembic, and how would Revision checksum reveal it?”
  • “Why is Flyway distinct from this behavior: a change-set migration format with checksums, contexts, and database-abstracted operations?”
  • Production clue: A single NOT NULL/table rewrite migration can lock a large table or fail after old data violates the new rule, and one developer running downgrade can lose data that a code rollback alone cannot reconstruct.
  • Design probe: Use expand-migrate-contract, small idempotent steps, online/index-concurrent options where supported, backfill in bounded batches, validate before enforcing constraints, and treat destructive rollback as an explicit data recovery plan.

Benchmarks & rules of thumb

Revision checksum -
detect modified migrations already applied
Lock time -
ALTER duration and blocked sessions must fit SLO
Backfill checkpoint -
resume after the last committed key or page

Common mistakes & follow-ups

Common interview mistakes

  • A single NOT NULL/table rewrite migration can lock a large table or fail after old data violates the new rule, and one developer running downgrade can lose data that a code rollback alone cannot reconstruct.
  • Reporting success from a job exit code without checking Revision checksum and Lock time.
  • Treating this example as complete without replay, rollback, or access boundaries: Add nullable tenant_id, deploy code that writes it, backfill UPDATE ... WHERE id > $last LIMIT 10_000, add NOT VALID FK/CHECK, validate in a low-lock pass, then enforce and drop the legacy column later.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: Python SQLAlchemy's revision graph and migration scripting tool for ordered schema and data changes.
  • What does the recovery plan do when this occurs? A single NOT NULL/table rewrite migration can lock a large table or fail after old data violates the new rule, and one developer running downgrade can lose data that a code rollback alone cannot reconstruct.
  • When is Alembic insufficient for Lock time, and what does adding Flyway cost in Revision checksum?
Interview tip: Lead with the data path and failure boundary: Migration tools order versioned DDL/DML changes, record which revisions ran, and provide reviewable upgrade and sometimes downgrade paths across environments. Then make the answer operational: Use expand-migrate-contract, small idempotent steps, online/index-concurrent options where supported, backfill in bounded batches, validate before enforcing constraints, and treat destructive rollback as an explicit data recovery plan.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Database Migration Tools (Alembic, Flyway, Liquibase) with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Change Data Capture (CDC) Fundamentals (Logical Replication & Binlogs)
Next topic
OLTP vs OLAP Workloads & Analytical Database Architecture