Ingot (database)
Migrations
Migrations live in database/migrations/, one file each, registered in order in database/migrations/mod.rs and
wired with .migrations(database::migrations::register) in bootstrap/app.rs:
use smeltery::db::migration::{Migration, Schema};
use smeltery::Result;
/// Creates `posts`.
pub struct CreatePostsTable;
impl Migration for CreatePostsTable {
fn name(&self) -> &'static str {
"2026_10_03_120000_create_posts_table"
}
async fn up(&self, schema: &Schema) -> Result<()> {
schema
.create("posts", |t| {
t.id();
t.foreign_id("user_id").constrained("users").cascade_on_delete();
t.string("title");
t.text("body").nullable();
t.boolean("published").default(false);
t.timestamps();
})
.await
}
async fn down(&self, schema: &Schema) -> Result<()> {
schema.drop_if_exists("posts").await
}
}
use smeltery::db::migration::Migrator;
pub fn register(m: &mut Migrator) {
m.add(m2026_10_03_120000_create_posts_table::CreatePostsTable);
// smeltery:migrations
}
The table blueprint (t):
| Method | Column |
|---|---|
t.id() |
id, 64-bit auto-increment primary key (i64) |
t.string(name), t.string_len(name, n) |
varchar(255) / varchar(n) (String) |
t.text(name) |
text (String) |
t.integer(name), t.big_integer(name) |
32-bit / 64-bit integer (i32 / i64) |
t.boolean(name) |
boolean (bool) |
t.float(name), t.double(name) |
f32 / f64 |
t.decimal(name, precision, scale) |
fixed-point decimal |
t.date(name) |
date (Date) |
t.datetime(name) |
timestamp, with time zone where the backend has one (DateTimeUtc) |
t.json(name) |
JSON, jsonb on PostgreSQL (Json) |
t.uuid(name) |
UUID (Uuid) |
t.foreign_id(name) |
64-bit integer (i64); .constrained("users") adds a foreign key to users.id, .cascade_on_delete() makes it ON DELETE CASCADE |
t.timestamps() |
nullable created_at and updated_at |
Columns are NOT NULL unless .nullable(). Every column also takes .unique(), .default(value) and .index()
(an index named <table>_<column>_index). The schema builder writes the SQL for the connected backend.
Schema has create(table, |t| …), table(table, |t| …) (add columns, and with t.drop_column(name) drop them),
drop(table), drop_if_exists(table), rename(from, to), has_table(name) and raw(sql). drop_column drops the
column's <table>_<column>_index / _unique index first; SQLite (the bundled one supports DROP COLUMN) refuses a
column that is a primary key, UNIQUE in its CREATE TABLE, part of a foreign key or used by another index, view or
trigger, and MySQL refuses a column of a foreign key.
The migrations table records each migration's name and batch. On SQLite and PostgreSQL each migration runs in a
transaction, so one that fails leaves neither its changes nor its record; MySQL commits schema changes as they run.
| Command | Does |
|---|---|
smeltery migrate |
run every pending migration, as one new batch |
smeltery migrate:rollback |
revert the last batch; --step N reverts the last N migrations |
smeltery migrate:fresh |
drop every table in the database (on SQLite virtual tables such as an FTS5 index first), then run every migration; --seed then runs the seeders |
smeltery migrate:status |
list each migration as Ran (with its batch) or Pending |
With APP_ENV=production, migrate, migrate:rollback, migrate:fresh and db:seed refuse to run unless
--force is given.