Stage 2 · Lesson 10 of 11
Schema migrations with sqlx-cli
comes with its own tool, sqlx-cli. sqlx migrate add create_orders creates a timestamped file in migrations/, and the -r flag gives each migration a matching down file to undo it. sqlx migrate run applies the pending ones and records them in a table, so every environment reaches the same .
You can also embed the files in your binary with sqlx::migrate!() and run them from code. The query macros read the live schema, so apply migrations before you build. Run them as a separate deploy step rather than at every app start, so several copies of the app do not race each other. Never edit a migration that has already run elsewhere.
Common trapa in one step, such as renaming a column the running code still reads. Add the new column, copy the data, switch the code, then drop the old one.
A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- CoursePaidGrokking Relational Database Design and Modeling
Design Gurus · Intermediate · $98 one-time
The modelling and normalisation decisions that stay with a codebase for years
- CourseFreeCS50's Introduction to Databases with SQL
Harvard University · Beginner
Free course from querying through schema design, indexes, transactions and scaling
Practise
Keep handy
Also worth your time
- Database Indexing Explained: B-Tree, Hash, Bitmap, R-Tree, and When to Use Each (opens in a new tab)Read
B-tree and composite indexes, plus the leftmost-prefix rule that trips people up
What sits under your queries: storage engines, transactions and isolation levels