Skip to content

Commit 4af63d8

Browse files
committed
Add support for tokio-rusqlite
1 parent a42c84a commit 4af63d8

File tree

20 files changed

+520
-150
lines changed

20 files changed

+520
-150
lines changed

.github/workflows/ci.yml

+36-6
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ jobs:
2121
with:
2222
command: check
2323

24-
test:
25-
name: Test Suite
24+
test_no_features:
25+
name: Test no features
2626
runs-on: ubuntu-latest
2727
steps:
2828
- uses: actions/checkout@v2
@@ -35,6 +35,36 @@ jobs:
3535
with:
3636
command: test
3737

38+
test_all_features:
39+
name: Test all features
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v2
43+
- uses: actions-rs/toolchain@v1
44+
with:
45+
profile: minimal
46+
toolchain: stable
47+
override: true
48+
- uses: actions-rs/cargo@v1
49+
with:
50+
command: test
51+
args: --all-features
52+
53+
test_async_tokio_rusqlite:
54+
name: Test feature async-tokio-rusqlite
55+
runs-on: ubuntu-latest
56+
steps:
57+
- uses: actions/checkout@v2
58+
- uses: actions-rs/toolchain@v1
59+
with:
60+
profile: minimal
61+
toolchain: stable
62+
override: true
63+
- uses: actions-rs/cargo@v1
64+
with:
65+
command: test
66+
args: --features async-tokio-rusqlite
67+
3868
min_rust_version:
3969
name: Check with minimal Rust version
4070
runs-on: ubuntu-latest
@@ -68,6 +98,9 @@ jobs:
6898
readme-sync:
6999
name: Sync Readme
70100
runs-on: ubuntu-latest
101+
defaults:
102+
run:
103+
working-directory: ./rusqlite_migration
71104
steps:
72105
- uses: actions/checkout@v2
73106
- uses: actions-rs/toolchain@v1
@@ -77,10 +110,7 @@ jobs:
77110
override: true
78111
- uses: Swatinem/rust-cache@v1
79112
- run: cargo install cargo-sync-readme
80-
- uses: actions-rs/cargo@v1
81-
with:
82-
command: sync-readme
83-
args: --check
113+
- run: cargo sync-readme --check
84114
clippy:
85115
name: Clippy
86116
runs-on: ubuntu-latest

.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
/target
2-
Cargo.lock
1+
**/target
2+
**/Cargo.lock
33

44
# Output of some examples
55
my_db.db3
6+
my_db.wal
7+
my_db.shm
68

79
# Local IDE & editor files
810
.idea/

Cargo.toml

+2-30
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,2 @@
1-
[package]
2-
name = "rusqlite_migration"
3-
version = "1.1.0-alpha.2"
4-
authors = ["Clément Joly <l@131719.xyz>"]
5-
edition = "2018"
6-
license = "Apache-2.0"
7-
description = "Simple schema migration library for rusqlite using user_version instead of an SQL table to maintain the current schema version."
8-
keywords = ["rusqlite", "sqlite", "user_version", "database", "migration"]
9-
categories = ["database"]
10-
readme = "README.md"
11-
homepage = "https://cj.rs/rusqlite_migration"
12-
repository = "https://github.com/cljoly/rusqlite_migration"
13-
rust-version = "1.61"
14-
15-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
16-
17-
[dependencies]
18-
log = "0.4"
19-
20-
[dependencies.rusqlite]
21-
version = ">=0.23.0"
22-
default-features = false
23-
features = []
24-
25-
[dev-dependencies]
26-
simple-logging = "2.0.2"
27-
env_logger = "0.10"
28-
anyhow = "1"
29-
lazy_static = "1.4.0"
30-
mktemp = "0.5"
1+
[workspace]
2+
members = ["rusqlite_migration", "rusqlite_migration_tests"]

README.md

-110
This file was deleted.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rusqlite_migration/README.md

examples/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[workspace]
2+
members = ["*"]
3+
exclude = ["target"]
4+
resolver = "2"

examples/async/Cargo.toml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
name = "example-async"
3+
version = "0.1.0"
4+
edition = "2018"
5+
publish = false
6+
7+
[dependencies]
8+
log = "0.4"
9+
simple-logging = "2.0.2"
10+
env_logger = "0.10"
11+
anyhow = "1"
12+
lazy_static = "1.4.0"
13+
mktemp = "0.5"
14+
tokio-rusqlite = "0.3.0"
15+
tokio = { version = "1.25.0", features = ["full"] }
16+
17+
[dependencies.rusqlite_migration]
18+
path = "../../rusqlite_migration"
19+
features = ["async-tokio-rusqlite"]
20+
21+
[dependencies.rusqlite]
22+
version = ">=0.23.0"
23+
default-features = false
24+
features = []

examples/async/src/main.rs

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
use anyhow::Result;
2+
use lazy_static::lazy_static;
3+
use rusqlite::params;
4+
use rusqlite_migration::{AsyncMigrations, M};
5+
use tokio_rusqlite::Connection;
6+
7+
// Test that migrations are working
8+
#[cfg(test)]
9+
mod tests {
10+
use super::*;
11+
12+
#[test]
13+
fn migrations_test() {
14+
assert!(MIGRATIONS.validate().is_ok());
15+
}
16+
}
17+
18+
// Define migrations. These are applied atomically.
19+
lazy_static! {
20+
static ref MIGRATIONS: AsyncMigrations =
21+
AsyncMigrations::new(vec![
22+
M::up(include_str!("../../friend_car.sql")),
23+
// PRAGMA are better applied outside of migrations, see below for details.
24+
M::up(r#"
25+
ALTER TABLE friend ADD COLUMN birthday TEXT;
26+
ALTER TABLE friend ADD COLUMN comment TEXT;
27+
"#),
28+
29+
// This migration can be reverted
30+
M::up("CREATE TABLE animal(name TEXT);")
31+
.down("DROP TABLE animal;")
32+
33+
// In the future, if the need to change the schema arises, put
34+
// migrations here, like so:
35+
// M::up("CREATE INDEX UX_friend_email ON friend(email);"),
36+
// M::up("CREATE INDEX UX_friend_name ON friend(name);"),
37+
]);
38+
}
39+
40+
pub async fn init_db() -> Result<Connection> {
41+
let mut async_conn = Connection::open("./my_db.db3").await?;
42+
43+
// Update the database schema, atomically
44+
MIGRATIONS.to_latest(&mut async_conn).await?;
45+
46+
Ok(async_conn)
47+
}
48+
49+
#[tokio::main]
50+
async fn main() {
51+
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
52+
53+
let mut async_conn = init_db().await.unwrap();
54+
55+
// Apply some PRAGMA. These are often better applied outside of migrations, as some needs to be
56+
// executed for each connection (like `foreign_keys`) or to be executed outside transactions
57+
// (`journal_mode` is a noop in a transaction).
58+
async_conn
59+
.call(|conn| conn.pragma_update(None, "journal_mode", "WAL"))
60+
.await
61+
.unwrap();
62+
async_conn
63+
.call(|conn| conn.pragma_update(None, "foreign_keys", "ON"))
64+
.await
65+
.unwrap();
66+
67+
// Use the db 🥳
68+
async_conn
69+
.call(|conn| {
70+
conn.execute(
71+
"INSERT INTO friend (name, birthday) VALUES (?1, ?2)",
72+
params!["John", "1970-01-01"],
73+
)
74+
})
75+
.await
76+
.unwrap();
77+
78+
async_conn
79+
.call(|conn| conn.execute("INSERT INTO animal (name) VALUES (?1)", params!["dog"]))
80+
.await
81+
.unwrap();
82+
83+
// If we want to revert the last migration
84+
MIGRATIONS.to_version(&mut async_conn, 2).await.unwrap();
85+
86+
// The table was removed
87+
async_conn
88+
.call(|conn| conn.execute("INSERT INTO animal (name) VALUES (?1)", params!["cat"]))
89+
.await
90+
.unwrap_err();
91+
}

examples/simple/Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "example-simple"
3+
version = "0.1.0"
4+
edition = "2018"
5+
publish = false
6+
7+
[dependencies]
8+
rusqlite_migration = { path = "../../rusqlite_migration" }
9+
log = "0.4"
10+
simple-logging = "2.0.2"
11+
env_logger = "0.10"
12+
anyhow = "1"
13+
lazy_static = "1.4.0"
14+
mktemp = "0.5"
15+
16+
[dependencies.rusqlite]
17+
version = ">=0.23.0"
18+
default-features = false
19+
features = []

examples/quick_start.rs examples/simple/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod tests {
1818
lazy_static! {
1919
static ref MIGRATIONS: Migrations<'static> =
2020
Migrations::new(vec![
21-
M::up(include_str!("friend_car.sql")),
21+
M::up(include_str!("../../friend_car.sql")),
2222
// PRAGMA are better applied outside of migrations, see below for details.
2323
M::up(r#"
2424
ALTER TABLE friend ADD COLUMN birthday TEXT;

0 commit comments

Comments
 (0)