liveBook · Manning

Migration

As explained in Code Like a Pro in Rust

A database migration is a versioned change that transforms a database schema or stored data from one state to another.

A database migration is a versioned SQL change that moves a database to a new schema state. In a Rust service, migrations run during database-pool initialization before routes start. section 9.5 section 9.5.3

Implement migrations with a deliberate workflow:

  • Write the migration yourself, then let SQLx apply it with sqlx::migrate!().run(&dbpool). section 9.5.3
  • Keep migrations correct and idempotent; provide both up and down directions when backward migrations are required. section 9.5.3
  • Treat database creation and migration as stateful, destructive operations: verify the target database before running them. section 9.5.3
  • Choose a database driver deliberately because SQL statements and database behavior vary despite shared SQL syntax. section 9.5.3

Creating a todos schema

Run sqlx migrate add todos, then populate the generated migrations/20230701202642_todos.sql file with a CREATE TABLE statement. The migration creates a todos table with a non-null integer primary key, body, completion flag, and creation and update timestamps. section 9.6.1

How Code Like a Pro in Rust covers it

How deep it goes
Explains migration authoring, application, idempotence, reversibility, and operational risk, but does not detail SQLx internals.
Examples
Uses a SQLx CLI-generated todos migration and a Rust initializer that applies migrations to an optionally created SQLite database.
What it assumes
Assumes Rust service and SQL familiarity while demonstrating database initialization, schema migration files, and SQLx CLI usage.
What you can do after
Operate SQLx migrations safely by generating files, applying them during initialization, and checking database targets before execution.
How it connects
Connects migrations with SQLx, SQLite, SQL schema, database drivers, and database connection pools.

Also explained in

Compare how 19 books explain migration →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.