Skip to content

Commit

Permalink
Add support for tokio-rusqlite
Browse files Browse the repository at this point in the history
  • Loading branch information
czocher committed Feb 16, 2023
1 parent a42c84a commit 4af63d8
Show file tree
Hide file tree
Showing 20 changed files with 520 additions and 150 deletions.
42 changes: 36 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
with:
command: check

test:
name: Test Suite
test_no_features:
name: Test no features
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
Expand All @@ -35,6 +35,36 @@ jobs:
with:
command: test

test_all_features:
name: Test all features
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --all-features

test_async_tokio_rusqlite:
name: Test feature async-tokio-rusqlite
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --features async-tokio-rusqlite

min_rust_version:
name: Check with minimal Rust version
runs-on: ubuntu-latest
Expand Down Expand Up @@ -68,6 +98,9 @@ jobs:
readme-sync:
name: Sync Readme
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./rusqlite_migration
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
Expand All @@ -77,10 +110,7 @@ jobs:
override: true
- uses: Swatinem/rust-cache@v1
- run: cargo install cargo-sync-readme
- uses: actions-rs/cargo@v1
with:
command: sync-readme
args: --check
- run: cargo sync-readme --check
clippy:
name: Clippy
runs-on: ubuntu-latest
Expand Down
6 changes: 4 additions & 2 deletions .gitignore
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/
32 changes: 2 additions & 30 deletions Cargo.toml
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 <l@131719.xyz>"]
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"]
110 changes: 0 additions & 110 deletions README.md

This file was deleted.

1 change: 1 addition & 0 deletions README.md
4 changes: 4 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = ["*"]
exclude = ["target"]
resolver = "2"
24 changes: 24 additions & 0 deletions examples/async/Cargo.toml
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 = []
91 changes: 91 additions & 0 deletions examples/async/src/main.rs
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();
}
19 changes: 19 additions & 0 deletions examples/simple/Cargo.toml
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 = []
2 changes: 1 addition & 1 deletion examples/quick_start.rs → examples/simple/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod tests {
lazy_static! {
static ref MIGRATIONS: Migrations<'static> =
Migrations::new(vec![
M::up(include_str!("friend_car.sql")),
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;
Expand Down
Loading

0 comments on commit 4af63d8

Please sign in to comment.