Skip to content

Commit

Permalink
Merge pull request #4411 from Spxg/wasm32
Browse files Browse the repository at this point in the history
Add wasm32-unknown-unknown target support for sqlite
  • Loading branch information
weiznich authored Jan 10, 2025
2 parents 7bfa883 + a030a2e commit a24aad1
Show file tree
Hide file tree
Showing 102 changed files with 2,045 additions and 1,394 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ concurrency:
jobs:
check_and_test:
name: Check
needs: [sqlite_bundled, rustfmt_and_clippy, postgres_bundled, mysql_bundled]
needs: [sqlite_bundled, sqlite_wasm, rustfmt_and_clippy, postgres_bundled, mysql_bundled]
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -278,6 +278,26 @@ jobs:
- name: Run clippy + rustfmt
run: cargo xtask tidy --keep-going

sqlite_wasm:
name: Check sqlite wasm
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: "rust-src"
targets: "wasm32-unknown-unknown"
- name: Cache cargo registry
uses: Swatinem/rust-cache@v2
with:
key: sqlite_wasm-cargo-${{ hashFiles('**/Cargo.toml') }}

- name: Install wasm-pack
run: cargo install wasm-pack

- name: Test unit and integration tests
run: cargo xtask run-tests --wasm sqlite

sqlite_bundled:
name: Check sqlite bundled + Sqlite with asan
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ in a way that makes the pools suitable for use in parallel tests.
* Added `Json` and `Jsonb` support for the SQLite backend.
* Fixed diesel thinking `a.eq_any(b)` was non-nullable even if `a` and `b` were nullable.
* Generate `InstrumentationEvent::BeginTransaction` for immediate and exclusive transactions in SQLite
* Added `wasm32-unknown-unknown` target support for sqlite backend.

### Fixed

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"diesel_cli",
"diesel_derives",
"diesel_tests",
"diesel_test_helper",
"diesel_migrations",
"diesel_migrations/migrations_internals",
"diesel_migrations/migrations_macros",
Expand Down
15 changes: 13 additions & 2 deletions diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ include = [
byteorder = { version = "1.0", optional = true }
chrono = { version = "0.4.20", optional = true, default-features = false, features = ["clock", "std"] }
libc = { version = "0.2.0", optional = true }
libsqlite3-sys = { version = ">=0.17.2, <0.31.0", optional = true, features = ["bundled_bindings"] }
mysqlclient-sys = { version = ">=0.2.5, <0.5.0", optional = true }
mysqlclient-src = { version = "0.1.0", optional = true }
pq-sys = { version = ">=0.4.0, <0.8.0", optional = true }
Expand All @@ -50,12 +49,24 @@ downcast-rs = "1.2.1"
version = "~2.2.0"
path = "../diesel_derives"

[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dependencies]
libsqlite3-sys = { version = ">=0.17.2, <0.31.0", optional = true, features = ["bundled_bindings"] }

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dependencies]
sqlite-wasm-rs = { version = ">=0.1.3, <0.2.0", optional = true }

[target.'cfg(all(target_family = "wasm", target_os = "unknown"))'.dev-dependencies]
# Something is dependent on it, so we use feature to override it.
getrandom = { version = "0.2", features = ["js"] }
wasm-bindgen-test = "0.3.49"

[dev-dependencies]
cfg-if = "1"
dotenvy = "0.15"
ipnetwork = ">=0.12.2, <0.22.0"
quickcheck = "1.0.3"
tempfile = "3.10.1"
diesel_test_helper = { path = "../diesel_test_helper" }

[features]
default = ["with-deprecated", "32-column-tables"]
Expand All @@ -67,7 +78,7 @@ huge-tables = ["64-column-tables"]
64-column-tables = ["32-column-tables", "diesel_derives/64-column-tables"]
128-column-tables = ["64-column-tables", "diesel_derives/128-column-tables"]
postgres = ["dep:pq-sys", "postgres_backend"]
sqlite = ["dep:libsqlite3-sys", "diesel_derives/sqlite", "time?/formatting", "time?/parsing"]
sqlite = ["dep:libsqlite3-sys", "dep:sqlite-wasm-rs", "diesel_derives/sqlite", "time?/formatting", "time?/parsing"]
mysql = ["dep:mysqlclient-sys", "dep:url", "dep:percent-encoding", "dep:bitflags", "mysql_backend"]
without-deprecated = ["diesel_derives/without-deprecated"]
with-deprecated = ["diesel_derives/with-deprecated"]
Expand Down
34 changes: 17 additions & 17 deletions diesel/src/connection/statement_cache/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ mod tests_pg {
conn
}

#[test]
#[diesel_test_helper::test]
fn prepared_statements_are_cached() {
let connection = &mut connection();

Expand All @@ -183,7 +183,7 @@ mod tests_pg {
assert_eq!(1, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn queries_with_identical_sql_but_different_types_are_cached_separately() {
let connection = &mut connection();

Expand All @@ -196,7 +196,7 @@ mod tests_pg {
assert_eq!(2, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn queries_with_identical_types_and_sql_but_different_bind_types_are_cached_separately() {
let connection = &mut connection();

Expand All @@ -211,7 +211,7 @@ mod tests_pg {

define_sql_function!(fn lower(x: VarChar) -> VarChar);

#[test]
#[diesel_test_helper::test]
fn queries_with_identical_types_and_binds_but_different_sql_are_cached_separately() {
let connection = &mut connection();

Expand All @@ -225,7 +225,7 @@ mod tests_pg {
assert_eq!(2, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn queries_with_sql_literal_nodes_are_not_cached() {
let connection = &mut connection();
let query = crate::select(sql::<Integer>("1"));
Expand All @@ -234,7 +234,7 @@ mod tests_pg {
assert_eq!(0, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn inserts_from_select_are_cached() {
let connection = &mut connection();
connection.begin_test_transaction().unwrap();
Expand All @@ -260,7 +260,7 @@ mod tests_pg {
assert_eq!(2, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn single_inserts_are_cached() {
let connection = &mut connection();
connection.begin_test_transaction().unwrap();
Expand All @@ -278,7 +278,7 @@ mod tests_pg {
assert_eq!(1, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn dynamic_batch_inserts_are_not_cached() {
let connection = &mut connection();
connection.begin_test_transaction().unwrap();
Expand All @@ -296,7 +296,7 @@ mod tests_pg {
assert_eq!(0, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn static_batch_inserts_are_cached() {
let connection = &mut connection();
connection.begin_test_transaction().unwrap();
Expand All @@ -314,7 +314,7 @@ mod tests_pg {
assert_eq!(1, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn queries_containing_in_with_vec_are_cached() {
let connection = &mut connection();
let one_as_expr = 1.into_sql::<Integer>();
Expand All @@ -324,7 +324,7 @@ mod tests_pg {
assert_eq!(1, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn disabling_the_cache_works() {
let connection = &mut connection();
connection.set_prepared_statement_cache_size(CacheSize::Disabled);
Expand Down Expand Up @@ -356,7 +356,7 @@ mod tests_sqlite {
conn
}

#[test]
#[diesel_test_helper::test]
fn prepared_statements_are_cached_when_run() {
let connection = &mut connection();
let query = crate::select(1.into_sql::<Integer>());
Expand All @@ -367,7 +367,7 @@ mod tests_sqlite {
assert_eq!(1, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn sql_literal_nodes_are_not_cached() {
let connection = &mut connection();
let query = crate::select(sql::<Integer>("1"));
Expand All @@ -376,7 +376,7 @@ mod tests_sqlite {
assert_eq!(0, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn queries_containing_sql_literal_nodes_are_not_cached() {
let connection = &mut connection();
let one_as_expr = 1.into_sql::<Integer>();
Expand All @@ -386,7 +386,7 @@ mod tests_sqlite {
assert_eq!(0, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn queries_containing_in_with_vec_are_not_cached() {
let connection = &mut connection();
let one_as_expr = 1.into_sql::<Integer>();
Expand All @@ -396,7 +396,7 @@ mod tests_sqlite {
assert_eq!(0, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn queries_containing_in_with_subselect_are_cached() {
let connection = &mut connection();
let one_as_expr = 1.into_sql::<Integer>();
Expand All @@ -406,7 +406,7 @@ mod tests_sqlite {
assert_eq!(1, count_cache_calls(connection));
}

#[test]
#[diesel_test_helper::test]
fn disabling_the_cache_works() {
let connection = &mut connection();
connection.set_prepared_statement_cache_size(CacheSize::Disabled);
Expand Down
22 changes: 11 additions & 11 deletions diesel/src/connection/transaction_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ mod test {
}
}

#[test]
#[diesel_test_helper::test]
#[cfg(feature = "postgres")]
fn transaction_manager_returns_an_error_when_attempting_to_commit_outside_of_a_transaction() {
use crate::connection::transaction_manager::AnsiTransactionManager;
Expand All @@ -629,7 +629,7 @@ mod test {
assert!(matches!(result, Err(Error::NotInTransaction)))
}

#[test]
#[diesel_test_helper::test]
#[cfg(feature = "postgres")]
fn transaction_manager_returns_an_error_when_attempting_to_rollback_outside_of_a_transaction() {
use crate::connection::transaction_manager::AnsiTransactionManager;
Expand All @@ -648,7 +648,7 @@ mod test {
assert!(matches!(result, Err(Error::NotInTransaction)))
}

#[test]
#[diesel_test_helper::test]
fn transaction_manager_enters_broken_state_when_connection_is_broken() {
use crate::connection::transaction_manager::AnsiTransactionManager;
use crate::connection::transaction_manager::TransactionManager;
Expand Down Expand Up @@ -702,7 +702,7 @@ mod test {
assert!(matches!(result, Err(Error::BrokenTransactionManager)))
}

#[test]
#[diesel_test_helper::test]
#[cfg(feature = "mysql")]
fn mysql_transaction_is_rolled_back_upon_syntax_error() {
use crate::connection::transaction_manager::AnsiTransactionManager;
Expand Down Expand Up @@ -737,7 +737,7 @@ mod test {
);
}

#[test]
#[diesel_test_helper::test]
#[cfg(feature = "sqlite")]
fn sqlite_transaction_is_rolled_back_upon_syntax_error() {
use crate::connection::transaction_manager::AnsiTransactionManager;
Expand Down Expand Up @@ -772,7 +772,7 @@ mod test {
);
}

#[test]
#[diesel_test_helper::test]
#[cfg(feature = "mysql")]
fn nested_mysql_transaction_is_rolled_back_upon_syntax_error() {
use crate::connection::transaction_manager::AnsiTransactionManager;
Expand Down Expand Up @@ -824,7 +824,7 @@ mod test {
);
}

#[test]
#[diesel_test_helper::test]
#[cfg(feature = "mysql")]
// This function uses a collect with side effects (spawning threads)
// so clippy is wrong here
Expand Down Expand Up @@ -930,7 +930,7 @@ mod test {
);
}

#[test]
#[diesel_test_helper::test]
#[cfg(feature = "mysql")]
// This function uses a collect with side effects (spawning threads)
// so clippy is wrong here
Expand Down Expand Up @@ -1038,7 +1038,7 @@ mod test {
);
}

#[test]
#[diesel_test_helper::test]
#[cfg(feature = "sqlite")]
fn sqlite_transaction_is_rolled_back_upon_deferred_constraint_failure() {
use crate::connection::transaction_manager::AnsiTransactionManager;
Expand Down Expand Up @@ -1079,7 +1079,7 @@ mod test {

// regression test for #3470
// crates.io depends on this behaviour
#[test]
#[diesel_test_helper::test]
#[cfg(feature = "postgres")]
fn some_libpq_failures_are_recoverable_by_rolling_back_the_savepoint_only() {
use crate::connection::{AnsiTransactionManager, TransactionManager};
Expand Down Expand Up @@ -1128,7 +1128,7 @@ mod test {
);
}

#[test]
#[diesel_test_helper::test]
#[cfg(feature = "postgres")]
fn other_libpq_failures_are_not_recoverable_by_rolling_back_the_savepoint_only() {
use crate::connection::{AnsiTransactionManager, TransactionManager};
Expand Down
11 changes: 11 additions & 0 deletions diesel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@
#![deny(unsafe_code)]
#![cfg_attr(test, allow(clippy::map_unwrap_or, clippy::unwrap_used))]

// Running wasm tests on dedicated_worker
#[cfg(test)]
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_dedicated_worker);

extern crate diesel_derives;

#[macro_use]
Expand Down Expand Up @@ -773,6 +778,12 @@ pub mod prelude {
#[cfg(feature = "sqlite")]
#[doc(inline)]
pub use crate::sqlite::SqliteConnection;

// These exported API from `sqlite-wasm-rs` are stable:
#[cfg(feature = "sqlite")]
#[cfg(all(target_family = "wasm", target_os = "unknown"))]
#[doc(inline)]
pub use sqlite_wasm_rs::export::init_sqlite;
}

#[doc(inline)]
Expand Down
Loading

0 comments on commit a24aad1

Please sign in to comment.