Backend development

Stage 2 · Lesson 10 of 11

Schema migrations with sqlx-cli

Relational databases and SQL

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

Practise

Keep handy

  • Query macros, pools, transactions, migrations and offline mode, with PostgreSQL examples

Also worth your time

Mark this stage as not done?