Skip to main content

Module migration

Module migration 

Source
Expand description

Rails-style schema migration helpers.

Every operation is a free function taking the migration’s SchemaManager as its first argument — table helpers (create_table, drop_table, rename_table, alter_table), column helpers (add_column, remove_column, rename_column), index helpers (add_index, remove_index) and foreign-key helpers (add_foreign_key, remove_foreign_key). They are meant to be called straight from a MigrationTrait::up/down:

use doido_model::migration::{create_table, drop_table, alter_table, add_index, add_foreign_key};

create_table(manager, "users", |t| {
    t.string("email").not_null().unique_key();
    t.string("name");
    t.timestamps();
})
.await?;

alter_table(manager, "users", |t| {
    t.add_column("age", |c| {
        c.integer();
    });
    t.rename_column("name", "full_name");
})
.await?;

add_index(manager, "users", &["email"]).await?;
add_foreign_key(manager, "posts", "user_id", "users", "id").await?;

drop_table(manager, "users").await?;

Structs§

AlterTableBuilder
Collects changes inside alter_table. Each change is applied as its own ALTER TABLE statement, so this works uniformly across backends (including SQLite, which permits only one alteration per statement).
TableBuilder
Collects column definitions inside create_table.

Functions§

add_column
add_column :table, :name, :typef configures the column type/modifiers.
add_foreign_key
add_foreign_key :from_table, :to_table — constraint fk_<from_table>_<from_column> linking from_table.from_column to to_table.to_column.
add_index
add_index :table, [columns] — index named idx_<table>_<cols>.
alter_table
alter_table :name do |t| ... end — applies the column changes collected in f (add/drop/rename), each as its own ALTER TABLE statement.
create_table
create_table :name do |t| ... end — creates a table with an implicit auto-incrementing id primary key plus the columns added in f.
drop_table
drop_table :name — drops the table if it exists.
remove_column
remove_column :table, :name.
remove_foreign_key
remove_foreign_key :from_table, column: :from_column.
remove_index
remove_index :table, [columns] — drops idx_<table>_<cols>.
rename_column
rename_column :table, :from, :to.
rename_table
rename_table :from, :to.