Free — no signup required

The Schema-As-Code Approach (Declarative)

2 min read

Describing the Destination, Not the Journey

If migration-based tools are like giving someone turn-by-turn directions, declarative schema tools are like giving them the destination address and letting them figure out the route. You define what the schema should look like, and the tool figures out what SQL to run to get there.

Atlas is the leading open-source tool in this space. Instead of writing ALTER TABLE statements, you write a schema definition file that describes the desired end state — declaring each table's columns with their types and nullability, a primary key, and any indexes (including uniqueness constraints), all in one declarative file rather than a sequence of imperative steps.

The Diff Engine: Plan Before You Apply

When the pipeline runs, Atlas performs a three-step process:

1. Inspect: Atlas connects to the live database and reads its actual current schema.

2. Diff: Atlas compares the actual schema to your desired schema file and computes the delta.

3. Plan: Atlas generates the SQL migration plan — for example, adding a new column and its supporting index — that you can review before applying it.

4. Apply: Once reviewed, the same plan is applied against the live database.

Handling Schema Drift

The declarative approach's killer feature is drift detection. If a developer manually ran ALTER TABLE directly against the staging database (a common, dangerous practice), Atlas will detect the discrepancy between the actual state and the desired state and include a correction in the next plan. Migration-based tools are blind to this — they only track changes made through their own scripts.

Interview Tip

Interviewers often ask: "What's the difference between Flyway and Atlas?" The nuanced answer: Flyway is imperative — you write the SQL steps. Atlas is declarative — you write the desired state and Atlas generates the steps. The deeper trade-off is control vs. convenience: Flyway gives you exact control over every SQL statement (important for complex migrations), while Atlas handles drift and eliminates boilerplate but requires trust in its diff engine. Most mature teams use migration-based tools for complex changes and declarative tools for simpler schema management.

Pros: No manual ALTER TABLE writing. Drift detection. Schema is always readable as a single source of truth. Excellent for greenfield projects.

Cons: Newer paradigm with a smaller ecosystem. The auto-generated SQL occasionally needs manual tuning for complex cases (e.g., renaming a column — the diff engine may generate DROP + ADD instead of RENAME, causing data loss). Always review the plan before applying in production.

This is one of 18 chapters

Every chapter is free — Kubernetes, Terraform, SRE, distributed systems, and more — with fast daily review built in.

Sign up free