CLI Commands¶
Ralph includes a powerful command-line interface (CLI) to help you manage your database schema and generate code. This guide covers all available commands and their usage.
Overview¶
Ralph does not ship pre-compiled CLI binaries. Instead, you create a small Crystal file in your project that compiles together with your migrations and models. This approach is common in the Crystal ecosystem (used by Micrate, Clear, and others) and provides full type safety for your migrations.
For setup instructions, see the CLI Customization Guide.
Once set up, you can run the CLI with:
crystal run ./ralph.cr -- [command]
# Or build once and run directly
crystal build ralph.cr -o bin/ralph
./bin/ralph [command]
Database Commands¶
These commands manage your database lifecycle, from creation to migrations and seeding.
db:create¶
Creates the database defined in your configuration or specified via the --database flag.
For PostgreSQL, this connects to the default postgres database to execute the CREATE DATABASE command.
Usage:
Example (SQLite):
Example (PostgreSQL):
db:drop¶
!!! danger "Warning" This command will permanently delete your database and all its data. Use with extreme caution.
Drops the database. For PostgreSQL, this command will attempt to terminate all existing connections to the target database before dropping it.
Usage:
Example:
db:migrate¶
Runs all pending migrations in the migrations directory.
Usage:
Options:
-e, --env ENV: Environment (default: development)-d, --database URL: Database URL-m, --migrations DIR: Migrations directory (default:./db/migrations)--models DIR: Models directory (default:./src/models)
db:rollback¶
Rolls back the most recently applied migration.
Usage:
db:status¶
Shows the status of all migrations (applied or pending).
Usage:
Example output:
Migration status:
Status Migration ID
--------------------------------------------------
[ UP ] 20260107000001
[ UP ] 20260107000002
[ DOWN ] 20260107000003
db:version¶
Shows the current migration version (the most recently applied migration).
Usage:
db:seed¶
Loads and executes the seed file (./db/seeds.cr). The seed file is a regular Crystal file that runs independently, so it must require Ralph and configure the database connection.
Usage:
Creating a Seed File:
Create db/seeds.cr in your project:
#!/usr/bin/env crystal
require "ralph"
require "ralph/backends/sqlite" # or postgres
require "../src/models/*"
# Configure database (must match your app's configuration)
Ralph.configure do |config|
config.database = Ralph::Database::SqliteBackend.new("sqlite3://./db/development.sqlite3")
end
# Use find_or_create_by for idempotent seeds
admin = User.find_or_create_by({"email" => "[email protected]"}) do |u|
u.name = "Administrator"
u.role = "admin"
end
puts "Seeded #{User.count} users"
!!! tip "Idempotent Seeds"
Use find_or_create_by or find_or_initialize_by to make your seeds idempotent—safe to run multiple times without creating duplicates. See CRUD Operations for details.
db:reset¶
Drops, creates, migrates, and seeds the database in one command.
Usage:
db:setup¶
Creates the database and runs all migrations.
Usage:
Generator Commands¶
g:migration¶
Creates a new migration file.
Usage:
Example:
$ ./ralph.cr g:migration CreateUsers
Created migration: ./db/migrations/20260108123456_create_users.cr
g:model¶
Generates a model file and an accompanying migration.
Usage:
Example:
Global Options¶
-e, --env ENV: Specifies the environment (e.g.,development,test,production). Default isdevelopment.-d, --database URL: Overrides the database URL from configuration.-m, --migrations DIR: Overrides the migrations directory path. Default is./db/migrations.--models DIR: Overrides the models directory path. Default is./src/models.-h, --help: Shows the help message.version: Shows the Ralph version.
Configuration¶
The CLI looks for database configuration in several places, in order of precedence:
- The
-dor--databaseflag. - The
DATABASE_URLenvironment variable. - The
POSTGRES_URLenvironment variable. - The
SQLITE_URLenvironment variable. - A default SQLite URL based on the environment:
sqlite3://./db/#{environment}.sqlite3.
The CLI automatically detects the database type from the URL scheme (postgres://, postgresql://, or sqlite3://).
Common Workflows¶
Starting a New Project¶
- Set up the CLI (see Customization Guide).
- Run
./ralph.cr db:setupto create the database. - Generate your first model:
./ralph.cr g:model User name:string. - Run
./ralph.cr db:migrate.
Iterating on Schema¶
- Create a migration:
./ralph.cr g:migration AddRoleToUsers. - Edit the generated file in
db/migrations/. - Run
./ralph.cr db:migrate. - If you made a mistake, run
./ralph.cr db:rollback, fix the file, and migrate again.
Troubleshooting¶
"Unknown command"¶
Ensure you are using the correct command name. Check ./ralph.cr help for the list of available commands.
"Database creation not implemented"¶
Ralph supports database creation for SQLite and PostgreSQL. Ensure your database URL scheme is supported (sqlite3://, postgres://, or postgresql://).
"No migrations have been run"¶
This message appears when calling db:version on an empty database. Run db:migrate to apply migrations.