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 with_migrations(migrations: Vec<Migration>) -> Migrator
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);Sourcepub fn dangerous_set_table_name(
&mut self,
table_name: impl Into<Cow<'static, str>>,
) -> &Migrator
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.
Sourcepub fn create_schema(
&mut self,
schema_name: impl Into<Cow<'static, str>>,
) -> &Migrator
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.
Sourcepub fn set_ignore_missing(&mut self, ignore_missing: bool) -> &mut Migrator
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.
Sourcepub fn set_locking(&mut self, locking: bool) -> &mut Migrator
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.
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).awaitpub async fn run_to<'a, A>( &self, target: i64, migrator: A, ) -> Result<(), MigrateError>
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).awaitTrait 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§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§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