Skip to main content

Migrator

Struct Migrator 

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

Runs an ordered list of Migrations, skipping any already applied.

Implementations§

Source§

impl Migrator

Source

pub fn new() -> Self

An empty migrator.

Source

pub fn add(self, migration: impl Migration) -> Self

Append a migration.

Source

pub fn add_arc(self, migration: Arc<dyn Migration>) -> Self

Append an already-shared migration.

Source

pub fn create_table<E: Entity>(self, name: impl Into<String>) -> Self

Append a “create this entity’s table” step, whose down drops it.

Source

pub fn step( self, name: impl Into<String>, up: impl Fn(Dialect) -> Vec<String> + Send + Sync + 'static, down: impl Fn(Dialect) -> Down + Send + Sync + 'static, ) -> Self

Append a step from up and down rendering closures.

Source

pub fn raw( self, name: impl Into<String>, up: Vec<String>, down: Vec<String>, ) -> Self

Append a step running fixed SQL, with the SQL that undoes it.

Source

pub fn raw_irreversible( self, name: impl Into<String>, up: Vec<String>, reason: impl Into<String>, ) -> Self

Append a step that cannot be undone, with the reason.

Source

pub fn merge(self, other: Migrator) -> Self

Append every step of other, keeping its order.

How a component that owns tables contributes them to the application’s one migrator — DatabaseQueue::migrations() being the case that ships. Without this, a driver’s tables could only be created by running a second migrator, which the migrate command has no way to find.

Source

pub fn names(&self) -> Vec<&str>

Every registered step’s name, in order.

Source

pub fn len(&self) -> usize

How many steps are registered.

Source

pub fn is_empty(&self) -> bool

Whether no steps are registered.

Source

pub fn irreversible(&self, dialect: Dialect) -> Vec<&str>

Which registered steps declare themselves irreversible on dialect.

Worth printing before a deploy: it is exactly the list of things a rollback will refuse to undo.

Source

pub async fn run(&self, db: &Database) -> Result<Vec<String>>

Apply every step that has not run yet, in order. Returns the names of the steps that were applied this time.

They share one batch, which is the unit rollback takes back off.

A step’s statements run before its ledger row is written, so a failure part-way leaves it unrecorded and it is retried next boot. That means a step must be idempotent where it can be — CREATE TABLE IF NOT EXISTS, which is what Rainier ORM’s generated DDL emits.

Source

pub async fn rollback(&self, db: &Database, batches: u32) -> Result<Vec<String>>

Undo the last batches batches, most recent first. Returns the names that were rolled back.

Within a batch, steps are undone in reverse declaration order, because a later step may depend on an earlier one — dropping the table a foreign key points at before dropping the key fails on every backend that enforces them.

Refuses before running anything if any step in range is irreversible, or is in the ledger but no longer in this migrator. A rollback that half-completes leaves the schema in a state no migration describes, which is worse than one that never started.

Source

pub async fn rollback_targets( &self, db: &Database, batches: u32, ) -> Result<Vec<String>>

The names the last batches batches applied, in the order a rollback would undo them.

What migrate:rollback --pretend prints.

Source

pub async fn applied(&self, db: &Database) -> Result<Vec<String>>

Which migrations the ledger says have already run.

Source

pub async fn pending(&self, db: &Database) -> Result<Vec<&str>>

Which registered migrations have not run yet, in order.

Trait Implementations§

Source§

impl Debug for Migrator

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Migrator

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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