Skip to main content

Migrator

Struct Migrator 

Source
pub struct Migrator<'a> { /* private fields */ }
Expand description

Applies and rolls back migrations, tracking which have run.

Implementations§

Source§

impl<'a> Migrator<'a>

Source

pub fn new(db: &'a Database, migrations: Vec<&'a dyn Migration>) -> Self

A migrator over any database, with any list of migrations.

Both halves are deliberate, and together they are how an application provisions a tenant from its own admin screen rather than from the CLI: create the database, then run into it only the migrations belonging to the modules that tenant enabled.

let tenant = Database::connect(url).await?;

let migrator = Migrator::new(&tenant, wanted);
migrator.prepare().await?;
let report = migrator.run().await?;
println!("{} migrations applied", report.applied.len());

App::migrations(...) is boot-time wiring and rustlavel migrate targets DATABASE_URL; neither can do this, which is why it is worth saying that this can.

Source

pub fn with_table(self, table: &str) -> Result<Self>

Record applied migrations in a different table.

Source

pub fn table(&self) -> &str

Source

pub async fn prepare(&self) -> Result<()>

Create the tracking table if it is not there yet.

The batch number is what makes migrate:rollback undo one deployment’s worth of migrations rather than one migration.

Source

pub async fn applied(&self) -> Result<Vec<String>>

Names already applied, in the order they ran.

Source

pub async fn pending(&self) -> Result<Vec<&'a dyn Migration>>

Migrations that have not run yet.

Source

pub async fn run(&self) -> Result<MigrationReport>

Run every pending migration.

A migration is not wrapped in a transaction, which is also what Laravel does. Wrapping one here would be a lie: the migration’s DDL and the tracking insert are separate statements, a pooled connection is not guaranteed to be the same one twice, and MySQL commits implicitly before and after every DDL statement regardless — a CREATE TABLE cannot be rolled back there at all.

A migration that needs to be atomic should open a transaction itself with db.begin().

Source

pub async fn rollback(&self) -> Result<MigrationReport>

Roll back the most recent batch.

Source

pub async fn fresh(&self, environment: &str) -> Result<MigrationReport>

Drop every table in the schema, then migrate from scratch.

Refuses to run in production: this is the command that would delete a live database, and a confirmation prompt is not available to a library.

Source

pub async fn status(&self) -> Result<Vec<(String, bool)>>

Which migrations have run and which have not.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Migrator<'a>

§

impl<'a> !UnwindSafe for Migrator<'a>

§

impl<'a> Freeze for Migrator<'a>

§

impl<'a> Send for Migrator<'a>

§

impl<'a> Sync for Migrator<'a>

§

impl<'a> Unpin for Migrator<'a>

§

impl<'a> UnsafeUnpin for Migrator<'a>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Same for T

Source§

type Output = T

Should always be Self
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.