-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
614 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
/target | ||
Cargo.lock | ||
**/target | ||
**/Cargo.lock | ||
|
||
# Output of some examples | ||
my_db.db3 | ||
my_db.wal | ||
my_db.shm | ||
|
||
# Local IDE & editor files | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,2 @@ | ||
[package] | ||
name = "rusqlite_migration" | ||
version = "1.1.0-alpha.2" | ||
authors = ["Clément Joly <[email protected]>"] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
description = "Simple schema migration library for rusqlite using user_version instead of an SQL table to maintain the current schema version." | ||
keywords = ["rusqlite", "sqlite", "user_version", "database", "migration"] | ||
categories = ["database"] | ||
readme = "README.md" | ||
homepage = "https://cj.rs/rusqlite_migration" | ||
repository = "https://github.com/cljoly/rusqlite_migration" | ||
rust-version = "1.61" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
log = "0.4" | ||
|
||
[dependencies.rusqlite] | ||
version = ">=0.23.0" | ||
default-features = false | ||
features = [] | ||
|
||
[dev-dependencies] | ||
simple-logging = "2.0.2" | ||
env_logger = "0.10" | ||
anyhow = "1" | ||
lazy_static = "1.4.0" | ||
mktemp = "0.5" | ||
[workspace] | ||
members = ["rusqlite_migration", "rusqlite_migration_tests"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rusqlite_migration/README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[workspace] | ||
members = ["*"] | ||
exclude = ["target"] | ||
resolver = "2" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
[package] | ||
name = "example-async" | ||
version = "0.1.0" | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
log = "0.4" | ||
simple-logging = "2.0.2" | ||
env_logger = "0.10" | ||
anyhow = "1" | ||
lazy_static = "1.4.0" | ||
mktemp = "0.5" | ||
tokio-rusqlite = "0.3.0" | ||
tokio = { version = "1.25.0", features = ["full"] } | ||
|
||
[dependencies.rusqlite_migration] | ||
path = "../../rusqlite_migration" | ||
features = ["async-tokio-rusqlite"] | ||
|
||
[dependencies.rusqlite] | ||
version = ">=0.23.0" | ||
default-features = false | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use anyhow::Result; | ||
use lazy_static::lazy_static; | ||
use rusqlite::params; | ||
use rusqlite_migration::{AsyncMigrations, M}; | ||
use tokio_rusqlite::Connection; | ||
|
||
// Test that migrations are working | ||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn migrations_test() { | ||
assert!(MIGRATIONS.validate().is_ok()); | ||
} | ||
} | ||
|
||
// Define migrations. These are applied atomically. | ||
lazy_static! { | ||
static ref MIGRATIONS: AsyncMigrations = | ||
AsyncMigrations::new(vec![ | ||
M::up(include_str!("../../friend_car.sql")), | ||
// PRAGMA are better applied outside of migrations, see below for details. | ||
M::up(r#" | ||
ALTER TABLE friend ADD COLUMN birthday TEXT; | ||
ALTER TABLE friend ADD COLUMN comment TEXT; | ||
"#), | ||
|
||
// This migration can be reverted | ||
M::up("CREATE TABLE animal(name TEXT);") | ||
.down("DROP TABLE animal;") | ||
|
||
// In the future, if the need to change the schema arises, put | ||
// migrations here, like so: | ||
// M::up("CREATE INDEX UX_friend_email ON friend(email);"), | ||
// M::up("CREATE INDEX UX_friend_name ON friend(name);"), | ||
]); | ||
} | ||
|
||
pub async fn init_db() -> Result<Connection> { | ||
let mut async_conn = Connection::open("./my_db.db3").await?; | ||
|
||
// Update the database schema, atomically | ||
MIGRATIONS.to_latest(&mut async_conn).await?; | ||
|
||
Ok(async_conn) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init(); | ||
|
||
let mut async_conn = init_db().await.unwrap(); | ||
|
||
// Apply some PRAGMA. These are often better applied outside of migrations, as some needs to be | ||
// executed for each connection (like `foreign_keys`) or to be executed outside transactions | ||
// (`journal_mode` is a noop in a transaction). | ||
async_conn | ||
.call(|conn| conn.pragma_update(None, "journal_mode", "WAL")) | ||
.await | ||
.unwrap(); | ||
async_conn | ||
.call(|conn| conn.pragma_update(None, "foreign_keys", "ON")) | ||
.await | ||
.unwrap(); | ||
|
||
// Use the db 🥳 | ||
async_conn | ||
.call(|conn| { | ||
conn.execute( | ||
"INSERT INTO friend (name, birthday) VALUES (?1, ?2)", | ||
params!["John", "1970-01-01"], | ||
) | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
async_conn | ||
.call(|conn| conn.execute("INSERT INTO animal (name) VALUES (?1)", params!["dog"])) | ||
.await | ||
.unwrap(); | ||
|
||
// If we want to revert the last migration | ||
MIGRATIONS.to_version(&mut async_conn, 2).await.unwrap(); | ||
|
||
// The table was removed | ||
async_conn | ||
.call(|conn| conn.execute("INSERT INTO animal (name) VALUES (?1)", params!["cat"])) | ||
.await | ||
.unwrap_err(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "example-simple" | ||
version = "0.1.0" | ||
edition = "2018" | ||
publish = false | ||
|
||
[dependencies] | ||
rusqlite_migration = { path = "../../rusqlite_migration" } | ||
log = "0.4" | ||
simple-logging = "2.0.2" | ||
env_logger = "0.10" | ||
anyhow = "1" | ||
lazy_static = "1.4.0" | ||
mktemp = "0.5" | ||
|
||
[dependencies.rusqlite] | ||
version = ">=0.23.0" | ||
default-features = false | ||
features = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.