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§
- Alter
Table Builder - Collects changes inside
alter_table. Each change is applied as its ownALTER TABLEstatement, so this works uniformly across backends (including SQLite, which permits only one alteration per statement). - Table
Builder - Collects column definitions inside
create_table.
Functions§
- add_
column add_column :table, :name, :type—fconfigures the column type/modifiers.- add_
foreign_ key add_foreign_key :from_table, :to_table— constraintfk_<from_table>_<from_column>linkingfrom_table.from_columntoto_table.to_column.- add_
index add_index :table, [columns]— index namedidx_<table>_<cols>.- alter_
table alter_table :name do |t| ... end— applies the column changes collected inf(add/drop/rename), each as its ownALTER TABLEstatement.- create_
table create_table :name do |t| ... end— creates a table with an implicit auto-incrementingidprimary key plus the columns added inf.- 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]— dropsidx_<table>_<cols>.- rename_
column rename_column :table, :from, :to.- rename_
table rename_table :from, :to.