pub struct Migration<'a, T>{ /* private fields */ }
Expand description

Migration and rollback of database definition with SQL

Requires feature migration.

Feature migration_embed provides macro embed_migrations! to embed a directory structure with SQL-files from filesystem into the application binary.

§Usage

Constructing Migration from code

use rqlite_client::migration::{Downgrade, Migration, Upgrade, M};
use rqlite_client::Connection;

#[cfg(feature = "ureq")]
{
    let migration = Migration::default().push(M(
        Upgrade::from("CREATE TEMP TABLE tmp"),
        Some(Downgrade::from("DROP TABLE temp.tmp")),
    ));

    let con = Connection::new("http://localhost:4001");
    #[cfg(feature = "url")]
    let con = con.unwrap();

    let version = migration.migrate(&con).expect("migration fail");
    let version = migration
        .rollback_to(&con, &(version - 1))
        .expect("rollback fail");
}

If you prefer to provide some SQL-files for upgrade and downgrade steps with your application there is Migration::from_path().

And you can also include these files in your application with the combination of embed_migrations! and Migration::from_embed().

It is allowed to put single-line comments with #, ;;, //, -- at the start of the line (after whitespaces) in your SQL data.
It is also possible to insert line-breaks in SQL statements with \ at the line end.

Implementations§

source§

impl<'a, T> Migration<'a, T>

source

pub fn max(&self) -> SchemaVersion

Maximum of available SchemaVersion

source

pub fn migrate( &self, connection: &Connection ) -> Result<SchemaVersion, MigrationError>

Migrate provided Migration

§Return

SchemaVersion after Migration

§Errors

MigrationError on failed Migration

§Panics

If there is no RequestBuilder provided with Migration::set_request_builder

source

pub fn migrate_to( &self, connection: &Connection, to_version: Option<&SchemaVersion> ) -> Result<SchemaVersion, MigrationError>

Migrate provided Migration to provided SchemaVersion

§Return

SchemaVersion after Migration

§Errors

MigrationError on failed Migration

§Panics

If there is no RequestBuilder provided with Migration::set_request_builder

source

pub fn pop(self) -> Option<M<'a>>

Removes the last migration and returns it, or None if there is no migration.

source

pub fn push(self, migration: M<'a>) -> Self

Add single migration M

source

pub fn rollback_to( &self, connection: &Connection, to_version: &SchemaVersion ) -> Result<SchemaVersion, MigrationError>

Rollback provided Migration to provided SchemaVersion

§Return

SchemaVersion after Migration rollback

§Errors

MigrationError on failed Migration rollback

§Panics

If there is no RequestBuilder provided with Migration::set_request_builder

source

pub fn truncate(self, len: usize) -> Self

Shortens the migrations, keeping the first len elements and dropping the rest.

If len is greater than the vector’s current length, this has no effect.

source§

impl<'a> Migration<'a, Request<Post>>

source

pub fn from_embed<S>() -> Self
where S: RustEmbed,

Create Migration from rust_embed::RustEmbed

The migrations are ordered like the sorted sub-directories in path. So one possibility is to start dir-names with fixed length numbers.

In sub-directories you have to provide a file upgrade.sql (case sensitive). And optional a file downgrade.sql (case sensitive).

All files and directories need to have correct permissions to be readable during build of the crate.

See Migration documentation how to embed the data.

Requires feature migration_embed.

source

pub fn from_path<'p, P>(path: P) -> Self
where P: Into<&'p Path>,

Create Migration with migrations from directory structure in path

The migrations are ordered like the sorted sub-directories in path. So one possibility is to start dir-names with fixed length numbers.

In sub-directories you have to provide a file upgrade.sql (case sensitive). And optional a file downgrade.sql (case sensitive).

All files and directories need to have correct permissions or Migration will fail with MigrationError.

source

pub fn new<S>(migrations: S) -> Self
where S: Into<Vec<M<'a>>>,

Create Migration with Into<Vec<M>>

Trait Implementations§

source§

impl<'a, T> Add for Migration<'a, T>

§

type Output = Migration<'a, T>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl<T> AddAssign for Migration<'_, T>

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl<'a, T> Clone for Migration<'a, T>

source§

fn clone(&self) -> Migration<'a, T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, T> Debug for Migration<'a, T>

source§

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

Formats the value using the given formatter. Read more
source§

impl Default for Migration<'_, Request<Post>>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'a> From<&'a M<'a>> for Migration<'a, Request<Post>>

Create Migration from single M reference

source§

fn from(value: &'a M<'a>) -> Self

Converts to this type from the input type.
source§

impl<'a> From<M<'a>> for Migration<'a, Request<Post>>

Create Migration from single M

source§

fn from(value: M<'a>) -> Self

Converts to this type from the input type.
source§

impl<'p, P> From<P> for Migration<'_, Request<Post>>
where P: Into<&'p Path>,

Create Migration with migrations from directory structure in path

The migrations are ordered like the sorted sub-directories in path. So one possibility is to start dir-names with fixed length numbers.

In sub-directories you have to provide a file upgrade.sql (case sensitive). And optional a file downgrade.sql (case sensitive).

All files and directories need to have correct permissions or Migration will fail with MigrationError.

source§

fn from(path: P) -> Self

Converts to this type from the input type.
source§

impl<'a, T> PartialEq for Migration<'a, T>

source§

fn eq(&self, other: &Migration<'a, T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, T> Eq for Migration<'a, T>

source§

impl<'a, T> StructuralPartialEq for Migration<'a, T>

Auto Trait Implementations§

§

impl<'a, T> RefUnwindSafe for Migration<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Migration<'a, T>
where T: Send,

§

impl<'a, T> Sync for Migration<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Migration<'a, T>
where T: Unpin,

§

impl<'a, T> UnwindSafe for Migration<'a, T>
where T: UnwindSafe,

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> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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<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