Course contentsShow
← Databases & SQL
Lesson 73 of 1,9531. Relational FoundationsFree lesson

Database Migration Lifecycle

Version control your database schema changes to ensure consistent deployments across environments.

In this lesson, you will learn how to use version control to manage your database schema changes so that your production, testing, and development environments stay perfectly in sync.

The Problem: The "Drift"

Imagine you are working on a project where you need to add a new email column to your Users table. You run the ALTER TABLE command on your laptop. A week later, your colleague does the same thing on their machine. Finally, you try to deploy to the production server, but you realize you forgot the exact syntax you used, or worse, the server is missing a secondary index you added earlier.

This state of inconsistency is called schema drift. It leads to broken applications and failed deployments.

The Solution: Version Control

Instead of manually typing SQL commands into a database console, you treat your schema like source code. You create small, numbered migration files—plain text files containing the SQL needed to transition the database from one version to the next.

For example, a file named 001_create_users_table.sql might contain:

CREATE TABLE Users (
    UserID INT PRIMARY KEY,
    Username VARCHAR(50)
);

Then, a follow-up file, 002_add_email_to_users.sql, contains:

ALTER TABLE Users ADD Email VARCHAR(100);

Keeping Environments Consistent

By storing these files in a version control system (like Git), you ensure that every developer is working with the same structure. When it comes time to deploy, your migration tool reads these files in order and applies them to the database. If a migration fails, you can use your knowledge of Commit and Rollback (learned in lesson 2) to ensure the database isn't left in a partial state.

This lifecycle—creating a script, checking it into version control, and executing it systematically—transforms database management from a chaotic manual task into a repeatable, reliable process. It ensures that if your code works in your local environment, it is guaranteed to be structured correctly for your production environment as well.

Key Takeaway: Version control for schema changes prevents environment drift by treating every database modification as a documented, repeatable, and reversible script.