pub struct TableChanges { /* private fields */ }Expand description
A change to a table that already exists.
TableChanges::to("posts", |table| {
table.string("subtitle").nullable();
table.index(["author_id", "published"]);
table.rename_column("body", "content");
});§Reversal
Each change knows its own opposite, so the migration’s down is derived
rather than written twice: an added column is dropped, a created index is
dropped, a rename is renamed back.
Two are not reversible, and say so rather than pretending:
drop_column, because the type and the data are gone;
and raw, because nothing here knows what it did.
Implementations§
Source§impl TableChanges
impl TableChanges
Sourcepub fn new(table: impl Into<String>) -> TableChanges
pub fn new(table: impl Into<String>) -> TableChanges
Changes to table.
Sourcepub fn to(
table: impl Into<String>,
build: impl FnOnce(&mut TableChanges),
) -> TableChanges
pub fn to( table: impl Into<String>, build: impl FnOnce(&mut TableChanges), ) -> TableChanges
Describe changes to table with build.
Sourcepub fn string_len(&mut self, name: impl Into<String>, len: u32) -> &mut Column
pub fn string_len(&mut self, name: impl Into<String>, len: u32) -> &mut Column
Add a VARCHAR(len).
Sourcepub fn big_integer(&mut self, name: impl Into<String>) -> &mut Column
pub fn big_integer(&mut self, name: impl Into<String>) -> &mut Column
Add a 64-bit signed integer.
Sourcepub fn unsigned_big_integer(&mut self, name: impl Into<String>) -> &mut Column
pub fn unsigned_big_integer(&mut self, name: impl Into<String>) -> &mut Column
Add a 64-bit unsigned integer.
Sourcepub fn foreign_id(&mut self, name: impl Into<String>) -> &mut Column
pub fn foreign_id(&mut self, name: impl Into<String>) -> &mut Column
Add a foreign key column.
The constraint is not added: no engine adds one to an existing
table portably (SQLite cannot at all), and a column that silently gained
no constraint would be worse than one that never claimed to. Declare it
in the CREATE TABLE, or accept the index this gives you.
Sourcepub fn drop_column(&mut self, name: impl Into<String>) -> &mut TableChanges
pub fn drop_column(&mut self, name: impl Into<String>) -> &mut TableChanges
Drop a column.
Makes the whole step irreversible: the type and the data are gone,
and a down that recreated the column empty would report success while
having restored nothing.
Sourcepub fn rename_column(
&mut self,
from: impl Into<String>,
to: impl Into<String>,
) -> &mut TableChanges
pub fn rename_column( &mut self, from: impl Into<String>, to: impl Into<String>, ) -> &mut TableChanges
Rename a column. Reverses by renaming it back.
Sourcepub fn index<I, S>(&mut self, columns: I) -> &mut IndexDef
pub fn index<I, S>(&mut self, columns: I) -> &mut IndexDef
Create an index, named by the same convention a CREATE TABLE uses.
Sourcepub fn drop_index<I, S>(&mut self, columns: I) -> &mut IndexDef
pub fn drop_index<I, S>(&mut self, columns: I) -> &mut IndexDef
Drop an index over columns.
The columns rather than the name, so the reversal can recreate it —
and because the name is derivable from them anyway. Use
IndexDef::named on the returned value if it was named by hand.
Sourcepub fn drop_unique<I, S>(&mut self, columns: I) -> &mut IndexDef
pub fn drop_unique<I, S>(&mut self, columns: I) -> &mut IndexDef
Drop a UNIQUE index over columns.
Sourcepub fn raw(
&mut self,
dialect: Option<Dialect>,
up: impl Into<String>,
down: Option<String>,
) -> &mut TableChanges
pub fn raw( &mut self, dialect: Option<Dialect>, up: impl Into<String>, down: Option<String>, ) -> &mut TableChanges
Raw SQL, for one dialect or all of them.
The escape hatch for what no portable form covers — a MODIFY COLUMN,
a generated column, a partial index. Pass down when you can undo it;
None makes the step irreversible and says why.
Reach for a typed change first. This one is the reason a schema stops being portable.
Sourcepub fn to_sql(&self, dialect: Dialect) -> Vec<String>
pub fn to_sql(&self, dialect: Dialect) -> Vec<String>
Every statement that applies these changes on dialect, in order.
Sourcepub fn irreversible_because(&self) -> Option<String>
pub fn irreversible_because(&self) -> Option<String>
Why these changes cannot be undone, or None if they can.
Checked before a rollback runs, so a batch that contains one irreversible step refuses up front rather than half-unwinding.
Sourcepub fn to_reverse_sql(&self, dialect: Dialect) -> Vec<String>
pub fn to_reverse_sql(&self, dialect: Dialect) -> Vec<String>
The statements that undo these changes on dialect, in reverse order.
Empty when irreversible_because has an
answer — ask that first.
Trait Implementations§
Source§impl Clone for TableChanges
impl Clone for TableChanges
Source§fn clone(&self) -> TableChanges
fn clone(&self) -> TableChanges
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more