Migrator

Struct Migrator 

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

A resolved set of migrations, ready to be run.

Can be constructed statically using migrate!() or at runtime using Migrator::new().

Implementations§

Source§

impl Migrator

Source

pub async fn new<'s, S>(source: S) -> Result<Migrator, 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 with_migrations(migrations: Vec<Migration>) -> Migrator

Creates a new instance with the given migrations.

§Examples
use sqlx::{ SqlSafeStr, migrate::{Migration, MigrationType::*, Migrator}};

// Define your migrations.
// You can also use include_str!("./xxx.sql") instead of hard-coded SQL statements.
let migrations = vec![
    Migration::new(1, "user".into(), ReversibleUp, "create table uesrs ( ... )".into_sql_str(), false),
    Migration::new(2, "post".into(), ReversibleUp, "create table posts ( ... )".into_sql_str(), false),
    // add more...
 ];
 let m = Migrator::with_migrations(migrations);
Source

pub fn dangerous_set_table_name( &mut self, table_name: impl Into<Cow<'static, str>>, ) -> &Migrator

Override the name of the table used to track executed migrations.

May be schema-qualified and/or contain quotes. Defaults to _sqlx_migrations.

Potentially useful for multi-tenant databases.

§Warning: Potential Data Loss or Corruption!

Changing this option for a production database will likely result in data loss or corruption as the migration machinery will no longer be aware of what migrations have been applied and will attempt to re-run them.

You should create the new table as a copy of the existing migrations table (with contents!), and be sure all instances of your application have been migrated to the new table before deleting the old one.

Source

pub fn create_schema( &mut self, schema_name: impl Into<Cow<'static, str>>, ) -> &Migrator

Add a schema name to be created if it does not already exist.

May be used with Self::dangerous_set_table_name() to place the migrations table in a new schema without requiring it to exist first.

§Note: Support Depends on Database

SQLite cannot create new schemas without attaching them to a database file, the path of which must be specified separately in an ATTACH DATABASE command.

Source

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

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

Source

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

Specify whether or not to lock the 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 fn version_exists(&self, version: i64) -> bool

Check if a migration version exists.

Source

pub async fn run<'a, A>(&self, migrator: A) -> Result<(), MigrateError>
where A: Acquire<'a>, <<A as Acquire<'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
use sqlx::migrate::Migrator;
use sqlx::sqlite::SqlitePoolOptions;

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

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

Source

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

Run down migrations against the database until a specific version.

§Examples
use sqlx::migrate::Migrator;
use sqlx::sqlite::SqlitePoolOptions;

let m = Migrator::new(std::path::Path::new("./migrations")).await?;
let pool = 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<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

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
§

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

§

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

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.
Source§

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

Source§

fn vzip(self) -> V

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