Run database migrations by executing the migration command provided by your specific migration tool, which applies pending schema changes in order to your database. Most Rust projects use diesel or sqlx for this, so you simply invoke their CLI tools with the up or migrate flag against your target environment.
If you are using Diesel, ensure your diesel.toml is configured with the correct database URL, then run the migration command. This will apply all unapplied migrations found in the migrations/ directory.
# Apply all pending migrations
diesel migration run
# Or specify a database URL explicitly if not in diesel.toml
diesel migration run --database-url "postgres://user:pass@localhost/mydb"
For SQLx, the process is similar but relies on the sqlx migrate command. You must ensure your migrations are in the migrations/ folder and your DATABASE_URL environment variable is set correctly.
# Apply all pending migrations
sqlx migrate run
# Run migrations against a specific URL without setting an env var
sqlx migrate run --database-url "postgres://user:pass@localhost/mydb"
If you are building a custom solution or need to run migrations programmatically within your Rust binary, you typically query the migration table (often named schema_migrations or migrations) to check which versions have been applied, then execute the SQL scripts for any missing versions. However, relying on the official CLI tools is strongly recommended to avoid race conditions and ensure transactional integrity. Always verify that your migration scripts are idempotent or reversible if you plan to run them in a production environment where manual intervention might be required.