Struct sqlx_core::migrate::Migrator

source ·
pub struct Migrator {
    pub migrations: Cow<'static, [Migration]>,
    pub ignore_missing: bool,
    pub locking: bool,
}

Fields§

§migrations: Cow<'static, [Migration]>§ignore_missing: bool§locking: bool

Implementations§

source§

impl Migrator

source

pub async fn new<'s, S>(source: S) -> Result<Self, MigrateError>where S: MigrationSource<'s>,

Creates a new instance with the given source.

Examples
use std::path::Path;

// Read migrations from a local folder: ./migrations
let m = Migrator::new(Path::new("./migrations")).await?;

See MigrationSource for details on structure of the ./migrations directory.

source

pub fn set_ignore_missing(&mut self, ignore_missing: bool) -> &Self

Specify whether applied migrations that are missing from the resolved migrations should be ignored.

source

pub fn set_locking(&mut self, locking: bool) -> &Self

Specify whether or not to lock database during migration. Defaults to true.

Warning

Disabling locking can lead to errors or data loss if multiple clients attempt to apply migrations simultaneously without some sort of mutual exclusion.

This should only be used if the database does not support locking, e.g. CockroachDB which talks the Postgres protocol but does not support advisory locks used by SQLx’s migrations support for Postgres.

source

pub fn iter(&self) -> Iter<'_, Migration>

Get an iterator over all known migrations.

source

pub async fn run<'a, A>(&self, migrator: A) -> Result<(), MigrateError>where A: Acquire<'a>, <A::Connection as Deref>::Target: Migrate,

Run any pending migrations against the database; and, validate previously applied migrations against the current migration source to detect accidental changes in previously-applied migrations.

Examples
let m = Migrator::new(std::path::Path::new("./migrations")).await?;
let pool = sqlx_core::sqlite::SqlitePoolOptions::new().connect("sqlite::memory:").await?;
m.run(&pool).await
source

pub async fn undo<'a, A>( &self, migrator: A, target: i64 ) -> Result<(), MigrateError>where A: Acquire<'a>, <A::Connection as Deref>::Target: Migrate,

Run down migrations against the database until a specific version.

Examples
let m = Migrator::new(std::path::Path::new("./migrations")).await?;
let pool = sqlx_core::sqlite::SqlitePoolOptions::new().connect("sqlite::memory:").await?;
m.undo(&pool, 4).await

Trait Implementations§

source§

impl Debug for Migrator

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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<T> for T

§

type Output = T

Should always be Self
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V