Skip to main content

Config

Struct Config 

Source
pub struct Config {
    pub create_schemas: BTreeSet<Box<str>>,
    pub table_name: Option<Box<str>>,
    pub migrations_dir: Option<Box<str>>,
    pub ignored_chars: BTreeSet<char>,
    pub defaults: MigrationDefaults,
}
Available on crate feature _unstable-docs only.
Expand description

Configuration for migrations when executed using sqlx::migrate!() or through sqlx-cli.

ยงNote

A manually constructed Migrator will not be aware of these configuration options. We recommend using sqlx::migrate!() instead.

ยงWarning: Potential Data Loss or Corruption!

Many of these options, if changed after migrations are set up, can result in data loss or corruption of a production database if the proper precautions are not taken.

Be sure you know what you are doing and that you read all relevant documentation thoroughly.

Fieldsยง

ยงcreate_schemas: BTreeSet<Box<str>>

Specify the names of schemas to create if they donโ€™t already exist.

This is done before checking the existence of the migrations table (_sqlx_migrations or overridden table_name below) so that it may be placed in one of these schemas.

ยงExample

sqlx.toml:

[migrate]
create-schemas = ["foo"]
ยงtable_name: Option<Box<str>>

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.

ยงExample

sqlx.toml:

[migrate]
# Put `_sqlx_migrations` in schema `foo`
table-name = "foo._sqlx_migrations"
ยงmigrations_dir: Option<Box<str>>

Override the directory used for migrations files.

Relative to the crate root for sqlx::migrate!(), or the current directory for sqlx-cli.

ยงignored_chars: BTreeSet<char>

Specify characters that should be ignored when hashing migrations.

Any characters contained in the given array will be dropped when a migration is hashed.

ยงWarning: May Change Hashes for Existing Migrations

Changing the characters considered in hashing migrations will likely change the output of the hash.

This may require manual rectification for deployed databases.

ยงExample: Ignore Carriage Return (<CR> | \r)

Line ending differences between platforms can result in migrations having non-repeatable hashes. The most common culprit is the carriage return (<CR> | \r), which Windows uses in its line endings alongside line feed (<LF> | \n), often written CRLF or \r\n, whereas Linux and macOS use only line feeds.

sqlx.toml:

[migrate]
ignored-chars = ["\r"]

For projects using Git, this can also be addressed using .gitattributes:

# Force newlines in migrations to be line feeds on all platforms
migrations/*.sql text eol=lf

This may require resetting or re-checking out the migrations files to take effect.

ยงExample: Ignore all Whitespace Characters

To make your migrations amenable to reformatting, you may wish to tell SQLx to ignore all whitespace characters in migrations.

ยงWarning: Beware Syntactically Significant Whitespace!

If your migrations use string literals or quoted identifiers which contain whitespace, this configuration will cause the migration machinery to ignore some changes to these. This may result in a mismatch between the development and production versions of your database.

sqlx.toml:

[migrate]
# Ignore common whitespace characters when hashing
ignored-chars = [" ", "\t", "\r", "\n"]  # Space, tab, CR, LF
ยงdefaults: MigrationDefaults

Specify default options for new migrations created with sqlx migrate add.

Implementationsยง

Sourceยง

impl Config

Source

pub fn migrations_dir(&self) -> &str

Available on crate feature migrate only.
Source

pub fn table_name(&self) -> &str

Available on crate feature migrate only.
Source

pub fn to_resolve_config(&self) -> ResolveConfig

Available on crate feature migrate only.

Trait Implementationsยง

Sourceยง

impl Debug for Config

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Sourceยง

impl Default for Config

Sourceยง

fn default() -> Config

Returns the โ€œdefault valueโ€ for a type. Read more

Auto Trait Implementationsยง

Blanket Implementationsยง

Sourceยง

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

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

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

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

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

Sourceยง

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

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

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
Sourceยง

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

Sourceยง

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
Sourceยง

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

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

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

Performs the conversion.
Sourceยง

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

Sourceยง

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

The type returned in the event of a conversion error.
Sourceยง

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