pub struct MIGRATIONS { /* private fields */ }Methods from Deref<Target = Migrations<'static>>§
Sourcepub fn current_version(&self, conn: &Connection) -> Result<SchemaVersion, Error>
pub fn current_version(&self, conn: &Connection) -> Result<SchemaVersion, Error>
Get the current schema version
§Example
use rusqlite_migration::{Migrations, M, SchemaVersion};
use std::num::NonZeroUsize;
let mut conn = rusqlite::Connection::open_in_memory().unwrap();
let migrations = Migrations::new(vec![
M::up("CREATE TABLE animals (name TEXT);"),
M::up("CREATE TABLE food (name TEXT);"),
]);
assert_eq!(SchemaVersion::NoneSet, migrations.current_version(&conn).unwrap());
// Go to the latest version
migrations.to_latest(&mut conn).unwrap();
assert_eq!(SchemaVersion::Inside(NonZeroUsize::new(2).unwrap()), migrations.current_version(&conn).unwrap());Sourcepub fn to_latest(&self, conn: &mut Connection) -> Result<(), Error>
pub fn to_latest(&self, conn: &mut Connection) -> Result<(), Error>
Migrate the database to latest schema version. The migrations are applied atomically.
§Example
use rusqlite_migration::{Migrations, M};
let mut conn = rusqlite::Connection::open_in_memory().unwrap();
let migrations = Migrations::new(vec![
M::up("CREATE TABLE animals (name TEXT);"),
M::up("CREATE TABLE food (name TEXT);"),
]);
// Go to the latest version
migrations.to_latest(&mut conn).unwrap();
// You can then insert values in the database
conn.execute("INSERT INTO animals (name) VALUES ('dog')", []).unwrap();
conn.execute("INSERT INTO food (name) VALUES ('carrot')", []).unwrap();Sourcepub fn to_version(
&self,
conn: &mut Connection,
version: usize,
) -> Result<(), Error>
pub fn to_version( &self, conn: &mut Connection, version: usize, ) -> Result<(), Error>
Migrate the database to a given schema version. The migrations are applied atomically.
§Specifying versions
- Empty database (no migrations run yet) has version
0. - The version increases after each migration, so after the first migration has run, the schema version is
1. For instance, if there are 3 migrations, version3is after all migrations have run.
Note: As a result, the version is the index in the migrations vector starting from 1.
§Example
use rusqlite_migration::{Migrations, M};
let mut conn = rusqlite::Connection::open_in_memory().unwrap();
let migrations = Migrations::new(vec![
// 0: version 0, before having run any migration
M::up("CREATE TABLE animals (name TEXT);").down("DROP TABLE animals;"),
// 1: version 1, after having created the “animals” table
M::up("CREATE TABLE food (name TEXT);").down("DROP TABLE food;"),
// 2: version 2, after having created the food table
]);
migrations.to_latest(&mut conn).unwrap(); // Create all tables
// Go back to version 1, i.e. after running the first migration
migrations.to_version(&mut conn, 1);
conn.execute("INSERT INTO animals (name) VALUES ('dog')", []).unwrap();
conn.execute("INSERT INTO food (name) VALUES ('carrot')", []).unwrap_err();
// Go back to an empty database
migrations.to_version(&mut conn, 0);
conn.execute("INSERT INTO animals (name) VALUES ('cat')", []).unwrap_err();
conn.execute("INSERT INTO food (name) VALUES ('milk')", []).unwrap_err();§Errors
Attempts to migrate to a higher version than is supported will result in an error.
When migrating downwards, all the reversed migrations must have a .down() variant,
otherwise no migrations are run and the function returns an error.
Trait Implementations§
Source§impl Deref for MIGRATIONS
impl Deref for MIGRATIONS
Source§type Target = Migrations<'static>
type Target = Migrations<'static>
The resulting type after dereferencing.
Source§fn deref(&self) -> &Migrations<'static>
fn deref(&self) -> &Migrations<'static>
Dereferences the value.
impl LazyStatic for MIGRATIONS
Auto Trait Implementations§
impl Freeze for MIGRATIONS
impl RefUnwindSafe for MIGRATIONS
impl Send for MIGRATIONS
impl Sync for MIGRATIONS
impl Unpin for MIGRATIONS
impl UnwindSafe for MIGRATIONS
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
Mutably borrows from an owned value. Read more