Advanced level · Module 1: Databases for Web Applications · Lesson 3 of 4
Migrations and Schema Changes
Change the database schema safely over time with versioned migration scripts.
In this lesson you will
- Write up and down migrations
- Apply migrations in development and production
Key concepts
migrationschema versionrollbackseed data
The lesson
A migration is a versioned script that changes the schema, for example adding a phone column. Migrations live in the repository and run in order, so every environment reaches the same schema.
Write changes that keep existing data valid: add a column as nullable, fill it, then make it required. A down migration reverses the change when something goes wrong.
Seed scripts insert sample data for development and tests. Never seed real personal data, and never run development seeds against production.
Step by step
- Set up a migration tool or simple migration runner.
- Write the initial schema as migration 001.
- Add a column in migration 002.
- Roll back and re-apply the migration.
Practice activity
Convert the booking platform schema into migrations and add a seed script.
Expected result: A fresh database reaches the full schema by running the migrations, and the seed script fills it with sample data.
Check your understanding
-
Why keep migrations in the repository?
Show answer
So every environment applies the same schema changes in the same order.
-
How should a required column be added to a table with existing rows?
Show answer
Add it as nullable, fill it, then make it required.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.