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
impl Migrator
sourcepub async fn new<'s, S>(source: S) -> Result<Self, MigrateError>where
S: MigrationSource<'s>,
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.
sourcepub fn set_ignore_missing(&mut self, ignore_missing: bool) -> &Self
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.
sourcepub fn set_locking(&mut self, locking: bool) -> &Self
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.
sourcepub async fn run<'a, A>(&self, migrator: A) -> Result<(), MigrateError>where
A: Acquire<'a>,
<A::Connection as Deref>::Target: Migrate,
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
sourcepub async fn undo<'a, A>(
&self,
migrator: A,
target: i64
) -> Result<(), MigrateError>where
A: Acquire<'a>,
<A::Connection as Deref>::Target: Migrate,
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