diesel
TLDR
Setup database (create if not exists)
SYNOPSIS
diesel command [options]
DESCRIPTION
Diesel CLI is the command-line tool for the Diesel ORM, a safe, extensible ORM and query builder for Rust. It manages database migrations and generates Rust code representing the database schema.
Migrations are SQL files in a migrations directory, with "up" and "down" files for applying and reverting changes. The CLI tracks applied migrations in a database table.
The print-schema command introspects the database and generates Rust code (schema.rs) that Diesel uses for compile-time query validation. This ensures queries are type-checked against the actual database structure.
PARAMETERS
setup
Create database and run migrations.migration generate name
Create new migration files.migration run
Run all pending migrations.migration revert
Revert the latest migration.migration redo
Revert and re-run latest migration.migration list
List all migrations and their status.print-schema
Output database schema as Rust code.database setup
Create the database.database reset
Drop and recreate database.--database-url url
Database connection URL.--config-file file
Path to diesel.toml.--migration-dir dir
Directory containing migrations.
CONFIGURATION
diesel.toml:
file = "src/schema.rs"
with_docs = true
[migrations_directory]
dir = "migrations"
CAVEATS
Requires database driver libraries (libpq for PostgreSQL, libmysqlclient for MySQL). The DATABASE_URL environment variable or --database-url must be set. Schema generation requires an accessible database. Migration SQL varies by database backend.
HISTORY
Diesel was created by Sean Griffin (also a Ruby on Rails contributor) and first released in 2015. It was designed to leverage Rust's type system for database safety. The CLI tool provides migration management similar to ActiveRecord or Knex. Diesel has become one of the most popular Rust database libraries.


