Struct Migrator
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§
§impl Migrator
impl Migrator
pub 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.
pub 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 users ( ... )".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);pub 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.
pub 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.
pub 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.
pub 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.
pub fn version_exists(&self, version: i64) -> bool
pub fn version_exists(&self, version: i64) -> bool
Check if a migration version exists.
pub 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>
pub async fn skip<'a, A>(
&self,
migrator: A,
target: Option<i64>,
) -> Result<(), MigrateError>
pub async fn skip<'a, A>( &self, migrator: A, target: Option<i64>, ) -> Result<(), MigrateError>
Skip any pending migrations until a specific version against the database; Additionally validate previously applied migrations against the current migration source to detect accidental changes in previously-applied migrations.
Skipping entails not executing the SQL of the migrations, but marking them as applied in the [_migrations] table.
§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.skip(&pool, Some(17)).awaitpub 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 UnsafeUnpin 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
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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§impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute] value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi [Quirk] value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);