Struct rqlite_client::migration::Migration
source · pub struct Migration<'a, T>where
T: RequestBuilder<NoLevelMulti>,{ /* 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>where
T: RequestBuilder<NoLevelMulti>,
impl<'a, T> Migration<'a, T>where
T: RequestBuilder<NoLevelMulti>,
sourcepub fn max(&self) -> SchemaVersion
pub fn max(&self) -> SchemaVersion
Maximum of available SchemaVersion
sourcepub fn migrate(
&self,
connection: &Connection
) -> Result<SchemaVersion, MigrationError>
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
sourcepub fn migrate_to(
&self,
connection: &Connection,
to_version: Option<&SchemaVersion>
) -> Result<SchemaVersion, MigrationError>
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
sourcepub fn pop(self) -> Option<M<'a>>
pub fn pop(self) -> Option<M<'a>>
Removes the last migration and returns it, or None if there is no migration.
sourcepub fn rollback_to(
&self,
connection: &Connection,
to_version: &SchemaVersion
) -> Result<SchemaVersion, MigrationError>
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§impl<'a> Migration<'a, Request<Post>>
impl<'a> Migration<'a, Request<Post>>
sourcepub fn from_embed<S>() -> Selfwhere
S: RustEmbed,
pub fn from_embed<S>() -> Selfwhere
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
.
sourcepub fn from_path<'p, P>(path: P) -> Self
pub fn from_path<'p, P>(path: P) -> Self
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
.
Trait Implementations§
source§impl<T> AddAssign for Migration<'_, T>where
T: RequestBuilder<NoLevelMulti>,
impl<T> AddAssign for Migration<'_, T>where
T: RequestBuilder<NoLevelMulti>,
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moresource§impl<'p, P> From<P> for Migration<'_, Request<Post>>
impl<'p, P> From<P> for Migration<'_, Request<Post>>
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
.