Database Migrations & Schema Versioning — Simple Guide for Full-Stack Devs (Mumb

Started by patilj, Aug 27, 2025, 03:14 AM

Previous topic - Next topic

patilj

Advertisement
Changing your database safely is a core full-stack skill. This short guide explains what migrations are, how to run them without breaking your app, and the minimum habits you need for reliable releases. If you want hands-on practice with real projects and reviews, check out full stack training in Mumbai
 or a practical full stack developer course in Mumbai
.

What is a migration?

A migration is a versioned change to your database schema or data (e.g., create table, add column, backfill values). Each migration has:

An up step (apply change)

A down step (rollback)

Think of it like Git for your database.

Minimal migration flow (works for most stacks)

Write migration file
Example: 2025-08-27_add_status_to_tasks.sql

ALTER TABLE tasks ADD COLUMN status TEXT NOT NULL DEFAULT 'open';


Deploy code that can handle both old & new
Read the new column defensively (with a default) so old rows don't crash the app.

Run migration in staging, then production.

Verify with a quick query or smoke test.

Clean up defaults/backfills once traffic is stable.

Tools to use:

Postgres: Prisma Migrate, Knex, Flyway, Liquibase

MongoDB: Mongoose migrations or a small custom runner

Want help setting this up step-by-step? Mentor-led full stack classes in Mumbai
cover this with templates.

Backwards-compatible changes (safe by default)

Prefer changes that don't break running code:

✅ Add a column with a default or nullable

✅ Add an index

✅ Add a new table

❌ Rename/drop a column used by current code (unsafe without a plan)

Two-step pattern for risky changes

Add-new (e.g., new_status), write code to use it, backfill values.

Cut-over code to only new_status.

Remove-old column in a later release.

Data backfills (do them gently)

When adding a new required column:

Backfill in small batches (e.g., 1k rows/iteration).

Schedule during low traffic.

Monitor p95 latency and locks.

If it's heavy, run via background jobs instead of blocking a request.

Indexes: big wins, small risks

Adding indexes speeds up reads but can lock on very large tables.

Create concurrently in Postgres:
CREATE INDEX CONCURRENTLY idx_tasks_user_created ON tasks(user_id, created_at);

Measure before/after; document the improvement in your README.

Transactions & rollbacks

Wrap schema changes in a transaction when the engine supports it (not all operations do). Always provide a down migration for fast rollback:

-- up
ALTER TABLE tasks ADD COLUMN archived_at TIMESTAMPTZ;
-- down
ALTER TABLE tasks DROP COLUMN archived_at;

CI & release checklist (copy/paste)

 Migration compiles/applies in CI against a test DB

 App code is compatible with pre-migration and post-migration states

 Staging deploy runs migration + smoke test (login → create → list)

 Production: read-only window considered for heavy ops

 Rollback command tested; down migration works

 README updated with the change and any operator notes

Need a working CI template that applies migrations on every PR? A mentor in full stack course in Mumbai
 can review and set it up with you.

Common mistakes (and quick fixes)

Locking the table during peak hours → run off-peak; use concurrent index creation.

Dropping columns too early → use the two-step pattern and remove later.

No backups → take a snapshot before risky changes.

Backfills via API requests → move to a queue/worker.

Tiny example plan (add a "priority" field)

Add column (nullable): ALTER TABLE tasks ADD COLUMN priority INT;

Deploy code that treats null as 3 (normal).

Backfill: set priority based on rules (batch job).

Make non-null with default: ALTER TABLE tasks ALTER COLUMN priority SET DEFAULT 3; then SET NOT NULL;

Remove fallback logic later.

Final note

Safe migrations are about small steps, compatibility, and verification. Treat schema like code: version it, test it, and roll it out with guardrails. For hands-on practice with Postgres/Mongo migrations, CI runners, and rollback drills, join full stack classes in Mumbai
 or enroll in a mentor-guided full stack course in Mumbai
.