Skip to main content

TableChanges

Struct TableChanges 

Source
pub struct TableChanges { /* private fields */ }
Expand description

A change to a table that already exists — Laravel’s Schema::table.

ⓘ
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

Source

pub fn new(table: impl Into<String>) -> TableChanges

Changes to table.

Source

pub fn to( table: impl Into<String>, build: impl FnOnce(&mut TableChanges), ) -> TableChanges

Describe changes to table with build.

Source

pub fn table(&self) -> &str

The table being changed.

Source

pub fn is_empty(&self) -> bool

Whether anything was asked for.

Source

pub fn column( &mut self, name: impl Into<String>, kind: ColumnKind, ) -> &mut Column

Add a column of any kind.

A column added to a table that already has rows must either be nullable or carry a default — otherwise the existing rows have no value for it and the engine refuses the change.

Source

pub fn string(&mut self, name: impl Into<String>) -> &mut Column

Add a VARCHAR(255).

Source

pub fn string_len(&mut self, name: impl Into<String>, len: u32) -> &mut Column

Add a VARCHAR(len).

Source

pub fn text(&mut self, name: impl Into<String>) -> &mut Column

Add an unbounded text column.

Source

pub fn integer(&mut self, name: impl Into<String>) -> &mut Column

Add a 32-bit signed integer.

Source

pub fn big_integer(&mut self, name: impl Into<String>) -> &mut Column

Add a 64-bit signed integer.

Source

pub fn unsigned_big_integer(&mut self, name: impl Into<String>) -> &mut Column

Add a 64-bit unsigned integer.

Source

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.

Source

pub fn boolean(&mut self, name: impl Into<String>) -> &mut Column

Add a boolean.

Source

pub fn timestamp(&mut self, name: impl Into<String>) -> &mut Column

Add a timestamp.

Source

pub fn json(&mut self, name: impl Into<String>) -> &mut Column

Add a JSON document column.

Source

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.

Source

pub fn rename_column( &mut self, from: impl Into<String>, to: impl Into<String>, ) -> &mut TableChanges

Rename a column. Reverses by renaming it back.

Source

pub fn index<I, S>(&mut self, columns: I) -> &mut IndexDef
where I: IntoIterator<Item = S>, S: Into<String>,

Create an index, named by the same convention a CREATE TABLE uses.

Source

pub fn unique<I, S>(&mut self, columns: I) -> &mut IndexDef
where I: IntoIterator<Item = S>, S: Into<String>,

Create a UNIQUE index.

Source

pub fn drop_index<I, S>(&mut self, columns: I) -> &mut IndexDef
where I: IntoIterator<Item = S>, S: Into<String>,

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.

Source

pub fn drop_unique<I, S>(&mut self, columns: I) -> &mut IndexDef
where I: IntoIterator<Item = S>, S: Into<String>,

Drop a UNIQUE index over columns.

Source

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.

Source

pub fn to_sql(&self, dialect: Dialect) -> Vec<String>

Every statement that applies these changes on dialect, in order.

Source

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.

Source

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

Source§

fn clone(&self) -> TableChanges

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TableChanges

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Event for T
where T: Send + Sync + 'static,

Source§

fn event_name() -> &'static str
where Self: Sized,

A human-readable name, used by wildcard listeners and log output. Always the Rust type path; see the trait docs.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more