0Pricing
Learn Rust Coding · Lesson

Migrations

Evolve the schema.

Migrations is a free Learn Rust Coding lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why migrations?

A migration is a versioned change to your database schema, like adding a table or column. They let your schema evolve safely and reproducibly.

  • Tracked in version control
  • Applied in order, exactly once

The sqlx-cli tool

sqlx ships a CLI for managing migrations. Install it once with cargo.

cargo install sqlx-cli --no-default-features --features postgres

Creating a migration

sqlx migrate add creates a timestamped SQL file in the migrations/ folder. The timestamp defines ordering.

sqlx migrate add create_users

Writing migration SQL

Each migration file holds the SQL to move the schema forward. Keep one logical change per file.

CREATE TABLE users (
    id   SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    email TEXT
);

Running migrations

sqlx migrate run applies all pending migrations against DATABASE_URL, recording each in a tracking table.

sqlx migrate run

The tracking table

sqlx records applied migrations in _sqlx_migrations, storing the version and a checksum so it never reapplies or silently changes one.

Reversible migrations

Add the -r flag for separate up and down files so you can roll back. sqlx migrate revert runs the most recent down file.

sqlx migrate add -r create_posts

Modeling migration tracking

A migration runner applies only versions newer than the last applied one. This runnable example models that decision.

fn pending(all: &[u32], applied: u32) -> Vec<u32> {
    all.iter().cloned().filter(|v| *v > applied).collect()
}

fn main() {
    let migrations = [1, 2, 3, 4];
    let last_applied = 2;
    println!("{:?}", pending(&migrations, last_applied));
}

Embedding migrations in the binary

The migrate! macro bakes the migrations folder into your binary so the app can apply them at startup, no CLI needed in production.

use sqlx::{Pool, Postgres};

async fn migrate(pool: &Pool<Postgres>) {
    sqlx::migrate!("./migrations")
        .run(pool)
        .await
        .unwrap();
}

Checksums and immutability

Never edit a migration that has already run. sqlx stores a checksum and will refuse to proceed if a past migration's content changed. Add a new migration instead.

Idempotent SQL

Guard statements with IF NOT EXISTS so re-running in a fresh environment is safe and predictable.

CREATE TABLE IF NOT EXISTS tags (
    id   SERIAL PRIMARY KEY,
    name TEXT NOT NULL UNIQUE
);

Quick Check

Test your understanding of migrations.

Recap

You learned schema migrations:

  • Migrations are ordered, versioned schema changes
  • sqlx migrate add/run/revert manage them
  • Applied versions and checksums live in _sqlx_migrations
  • migrate! embeds them for startup application
  • Never edit an applied migration; add a new one

That completes the sqlx course.

Frequently asked questions

Is the “Migrations” lesson free?

Yes — the full text of “Migrations” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Migrations”?

Evolve the schema. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Migrations” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Connecting to a Database
  2. Compile-Time Checked Queries
  3. CRUD Operations
  4. Migrations
← Back to Learn Rust Coding