Skip to main content

Module schema

Module schema 

Source
Expand description

A database-neutral schema builder, after Laravel’s Schema::create, Schema::table and Blueprint.

A migration describes a table once and the same code runs on SQLite, MySQL and MariaDB, and PostgreSQL:

use orbit_orm::schema::{self, Action};

let posts = schema::create("posts", |t| {
    t.id();
    t.string("title");
    t.text("body").nullable();
    t.big_integer("user_id");
    t.foreign("user_id").references("users", "id").on_delete(Action::Restrict);
    t.unique(["title"]);
});

let rename = schema::table("posts", |t| {
    t.string("subtitle").nullable();
    t.rename_column("body", "content");
});

A Statement is only a description. Statement::to_sql renders it for a Driver as a list of statements to run in order: PostgreSQL and SQLite create a plain index with a separate CREATE INDEX, and sqlx does not run several statements in one string on every database.

statement runs SQL as written, as Laravel’s DB::statement in a migration, and Statement::only limits any statement to one database family.

Structs§

Blueprint
The columns and commands (indexes, keys, renames, drops) of a table, in the order they were added.
Column
A column of a table being created or changed. Chain nullable, default, use_current, length, precision, primary and change on it.
ForeignKey
A foreign key under construction. Chain references, on_delete, on_update and name.
Index
A plain or unique index under construction. Chain name to choose its name.
Statement
A schema change, ready to render for any database.

Enums§

Action
What a foreign key does to this table’s rows when the referenced row is deleted or updated.
Dialect
A database family. A Driver maps to one of these. Unlike Driver, all three variants exist whatever Cargo features are on, so a migration can name any of them, for example in Statement::only.

Functions§

create
Creates a table. The closure describes its columns, indexes and foreign keys.
create_if_not_exists
Creates a table unless it exists: CREATE TABLE IF NOT EXISTS. A plain index that PostgreSQL and SQLite create with a separate statement takes CREATE INDEX IF NOT EXISTS.
drop
Drops a table. It is an error in the database if the table does not exist.
drop_if_exists
Drops a table if it exists.
statement
Runs sql as written, as Laravel’s DB::statement inside a migration.
table
Changes an existing table, as Laravel’s Schema::table. A column described in the closure is added, or modified when it ends with .change(); rename_column and drop_column rename and drop columns; index, unique, primary and foreign add keys; drop_index and drop_foreign drop them.