Crates.io | schemamama_rusqlite |
lib.rs | schemamama_rusqlite |
version | 0.17.0 |
source | src |
created_at | 2015-09-19 12:06:37.553124 |
updated_at | 2024-08-25 19:56:21.973354 |
description | Rusqlite SQLite3 adapter for the Schemamama migration system |
homepage | |
repository | https://github.com/cmsd2/schemamama_rusqlite |
max_upload_size | |
id | 3069 |
size | 38,847 |
A Rusqlite SQLite3 adapter for the lightweight database migration system
Schemamama. Depends on the
rusqlite
crate.
It is based on schemamama_postgres.
Rusqlite requires sqlite3 dev library to be installed.
Then add Schemamama to your Cargo.toml
:
[dependencies]
schemamama = "*"
schemamama_rusqlite = "*"
rusqlite = "0.2.0"
You may need to pass in a custom value for the PKG_CONFIG_PATH if rust is unable to locate your sqlite3 installation.
This package | Rusqlite | libqlite3-sys |
---|---|---|
0.10 | 0.25 | 0.22 |
0.11 | 0.26 | 0.23 |
0.12 | 0.27 | 0.24 |
0.13 | 0.28 | 0.25 |
0.14 | 0.29 | 0.26 |
0.15 | 0.30 | 0.27 |
0.16 | 0.31 | 0.28 |
0.17 | 0.32 | 0.30 |
First, define some migrations:
#[macro_use]
extern crate schemamama;
extern crate schemamama_rusqlite;
extern crate rusqlite;
use schemamama::{Migration, Migrator};
use schemamama_rusqlite::{SqliteAdapter, SqliteMigration};
struct CreateUsers;
// Instead of using sequential numbers (1, 2, 3...), you may instead choose to use a global
// versioning scheme, such as epoch timestamps.
migration!(CreateUsers, 1, "create users table");
impl SqliteMigration for CreateUsers {
fn up(&self, conn: &rusqlite::Connection) -> SqliteResult<()> {
conn.execute("CREATE TABLE users (id BIGINT PRIMARY KEY);", []).map(|_| ())
}
fn down(&self, transaction: &rusqlite::Connection) -> SqliteResult<()> {
transaction.execute("DROP TABLE users;", []).map(|_| ())
}
}
struct CreateProducts;
migration!(CreateProducts, 2, "create products table");
impl SqliteMigration for CreateProducts {
// ...
}
Then, run the migrations!
let conn = Rc::new(RefCell::new(SqliteConnection::open_in_memory().expect("open db")));
let adapter = SqliteAdapter::new(conn);
// Create the metadata tables necessary for tracking migrations. This is safe to call more than
// once (`CREATE TABLE IF NOT EXISTS schemamama` is used internally):
adapter.setup_schema();
let mut migrator = Migrator::new(adapter);
migrator.register(Box::new(CreateUsers));
migrator.register(Box::new(CreateProducts));
// Execute migrations up to and including version 2:
migrator.up(Some(2));
assert_eq!(migrator.current_version().expect("current version"), Some(2));
// Reverse all migrations:
migrator.down(None);
assert_eq!(migrator.current_version().expect("current version"), None);
Run cargo test
schemamama
).Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.