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
impl Migrator
sourcepub async fn new<'s, S>(source: S) -> Result<Migrator, MigrateError>where
S: MigrationSource<'s>,
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.
sourcepub fn set_ignore_missing(&mut self, ignore_missing: bool) -> &Migrator
pub fn set_ignore_missing(&mut self, ignore_missing: bool) -> &Migrator
Specify whether applied migrations that are missing from the resolved migrations should be ignored.
sourcepub fn set_locking(&mut self, locking: bool) -> &Migrator
pub fn set_locking(&mut self, locking: bool) -> &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.
sourcepub fn version_exists(&self, version: i64) -> bool
pub fn version_exists(&self, version: i64) -> bool
Check if a migration version exists.
sourcepub async fn run<'a, A>(&self, migrator: A) -> Result<(), MigrateError>
pub async fn run<'a, A>(&self, migrator: A) -> Result<(), MigrateError>
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
sourcepub async fn undo<'a, A>(
&self,
migrator: A,
target: i64,
) -> Result<(), MigrateError>
pub async fn undo<'a, A>( &self, migrator: A, target: i64, ) -> Result<(), MigrateError>
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§
Auto Trait Implementations§
impl Freeze for Migrator
impl RefUnwindSafe for Migrator
impl Send for Migrator
impl Sync for Migrator
impl Unpin for Migrator
impl UnwindSafe for Migrator
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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