Skip to content

Commit

Permalink
xtask task for custom wasm-bindgen build
Browse files Browse the repository at this point in the history
  • Loading branch information
insipx committed Oct 15, 2024
1 parent eac6c90 commit e5056e6
Show file tree
Hide file tree
Showing 53 changed files with 354 additions and 92 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
xtask = "run --package xtask --"

[target.wasm32-unknown-unknown]
runner = "wasm-bindgen-test-runner"
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ jobs:
with:
workspaces: |
.
- run: cargo build --target wasm32-unknown-unknown --tests --features unsafe-debug-query
- run: wasm-pack test --headless --chrome --features unsafe-debug-query
- run: cargo build --target wasm32-unknown-unknown --tests --features test-utils
- run: wasm-pack test --headless --chrome --features test-utils
working-directory: ./
- run: wasm-pack test --headless --firefox --features unsafe-debug-query
- run: wasm-pack test --headless --firefox --features test-utils
working-directory: ./
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ Cargo.lock
!.yarn/releases
!.yarn/sdks
!.yarn/versions
node_modules/**
**/node_modules/**
.direnv/**
.envrc
71 changes: 25 additions & 46 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,45 +1,34 @@
[package]
name = "diesel-wasm-sqlite"
version = "0.0.1"
edition = "2021"
[workspace]
members = [
"diesel-wasm-sqlite",
"xtask"
]
resolver = "2"
authors = ["XMTP Labs <[email protected]>"]
description = "SQLite WebAssembly backend for Diesel"
homepage = "https://xmtp.org/"
repository = "https://github.com/xmtp/libxmtp"
license = "MIT"
keywords = ["orm", "database", "sql", "wasm"]
categories = ["database", "wasm"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
targets = ["wasm32-unknown-unknown"]
[workspace.package]
version = "0.0.1"

[dependencies]
talc = { version = "4.4", default-features = false, features = ["lock_api"] }
diesel = { version = "2.2", features = ["i-implement-a-third-party-backend-and-opt-into-breaking-changes"] }
diesel_derives = "2.2"
[workspace.dependencies]
wasm-bindgen = "=0.2.95"
wasm-bindgen-test = "=0.3.45"
wasm-bindgen-futures = "0.4"
serde-wasm-bindgen = "0.6"
talc = { version = "4.4", default-features = false }
web-sys = { version = "0.3", features = ["console"] }
js-sys = { version = "0.3" }
diesel = { version = "2.2" }
diesel_derives = { version = "2.2", default-features = false }
diesel_migrations = { version = "2.2", default-features = false }
tracing = { version = "0.1", default-features = false }
tokio = { version = "1.38", default-features = false, features = ["sync"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde-wasm-bindgen = "0.6"
thiserror = "1"

[dev-dependencies]
wasm-bindgen-test = "=0.3.45"
tracing-wasm = { version = "0.2", default-features = false }
tracing-subscriber = { version = "0.3", default-features = false }
console_error_panic_hook = { version = "0.1"}
tokio = { version = "1.38", default-features = false }
serde = { version = "1.0", default-features = false }
thiserror = { version = "1" }
chrono = { version = "0.4", default-features = false }
rand = "0.8"
getrandom = { version = "0.2", features = ["js"] }
web-sys = { version = "0.3", features = ["console"] }
chrono = { version = "0.4", features = ["wasmbind", "serde"] }
diesel_migrations = "2.2"
diesel = { version = "2.2", features = ["chrono"]}
tracing-wasm = { version = "0.2" }
tracing-subscriber = { version = "0.3", features = ["env-filter", "tracing-log"] }
getrandom = "0.2"

[patch.crates-io]
diesel = { git = "https://github.com/diesel-rs/diesel", branch = "master" }
Expand All @@ -50,16 +39,6 @@ diesel_migrations = { git = "https://github.com/diesel-rs/diesel", branch = "mas
opt-level = "s"
lto = true

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = []
r2d2 = ["diesel/r2d2"]
# enables a `DebugQueryWrapper` for diesel
# but is unsafe because of mem::transmute
unsafe-debug-query = []

[build]
target = "wasm32-unknown-unknown"

# for color-eyre to build faster in debug
[profile.dev.package.backtrace]
opt-level = 3
58 changes: 58 additions & 0 deletions diesel-wasm-sqlite/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[package]
name = "diesel-wasm-sqlite"
edition = "2021"
resolver = "2"
authors = ["XMTP Labs <[email protected]>"]
description = "SQLite WebAssembly backend for Diesel"
homepage = "https://xmtp.org/"
repository = "https://github.com/xmtp/diesel-wasm-sqlite"
license = "MIT"
keywords = ["orm", "database", "sql", "wasm"]
categories = ["database", "wasm"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
targets = ["wasm32-unknown-unknown"]

[dependencies]
talc = { workspace = true, default-features = false, features = ["lock_api"] }
diesel = { workspace = true, features = ["i-implement-a-third-party-backend-and-opt-into-breaking-changes"] }
diesel_derives.workspace = true
wasm-bindgen.workspace = true
wasm-bindgen-futures.workspace = true
js-sys.workspace = true
tracing.workspace = true
tokio = { workspace = true, default-features = false, features = ["sync"] }
serde = { workspace = true, default-features = false, features = ["derive"] }
serde-wasm-bindgen.workspace = true
thiserror.workspace = true

tracing-wasm = { workspace = true, optional = true }
tracing-subscriber = { workspace = true, features = ["env-filter", "tracing-log"], optional = true }
web-sys = { workspace = true, features = ["console"], optional = true }
console_error_panic_hook = { workspace = true, optional = true}

[dev-dependencies]
wasm-bindgen-test.workspace = true
console_error_panic_hook = { workspace = true}
rand.workspace = true
getrandom = { workspace = true, features = ["js"] }
web-sys = { workspace = true, features = ["console"] }
chrono = { workspace = true, features = ["wasmbind", "serde"] }
diesel_migrations.workspace = true
diesel = { workspace = true, features = ["chrono"]}
tracing-wasm = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter", "tracing-log"] }

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = []
r2d2 = ["diesel/r2d2"]
# enables a `DebugQueryWrapper` for diesel
# but is unsafe because of mem::transmute
unsafe-debug-query = []
test-utils = ["unsafe-debug-query", "tracing-wasm", "tracing-subscriber", "console_error_panic_hook", "web-sys"]

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/ffi.rs → diesel-wasm-sqlite/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub unsafe fn raw_copy_from_sqlite(src: *mut u8, len: u32, buf: &mut [u8]) {
pub async fn init_sqlite() {
SQLITE
.get_or_init(|| async {
#[cfg(any(feature = "test-utils", test))]
crate::utils::init().await;
let mem = serde_wasm_bindgen::to_value(&MemoryOpts {
initial: 16_777_216 / 65_536,
maximum: 2_147_483_648 / 65_536,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions src/lib.rs → diesel-wasm-sqlite/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod ffi;
pub mod query_builder;
pub mod sqlite_fixes;
pub mod sqlite_types;
pub mod utils;

#[global_allocator]
static ALLOCATOR: talc::TalckWasm = unsafe { talc::TalckWasm::new_global() };
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions diesel-wasm-sqlite/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#[cfg(any(feature = "test-utils", test))]
pub use test::*;

#[cfg(any(feature = "test-utils", test))]
mod test {
use tokio::sync::OnceCell;
static INIT: OnceCell<()> = OnceCell::const_new();

pub async fn init() {
INIT.get_or_init(|| async {
web_sys::console::log_1(&"INIT".into());
let config = tracing_wasm::WASMLayerConfigBuilder::default()
.set_console_config(tracing_wasm::ConsoleConfig::ReportWithoutConsoleColor)
.build();
tracing_wasm::set_as_global_default_with_config(config);
console_error_panic_hook::set_once();
})
.await;
}
}
28 changes: 5 additions & 23 deletions tests/common/mod.rs → diesel-wasm-sqlite/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
//! Common utilities/imports amongst WebAssembly tests
use prelude::*;

use tokio::sync::OnceCell;

static INIT: OnceCell<()> = OnceCell::const_new();

pub async fn init() {
INIT.get_or_init(|| async {
console::log_1(&"INIT".into());
let config = tracing_wasm::WASMLayerConfigBuilder::default()
.set_console_config(tracing_wasm::ConsoleConfig::ReportWithoutConsoleColor)
.build();
tracing_wasm::set_as_global_default_with_config(config);
console_error_panic_hook::set_once();
diesel_wasm_sqlite::init_sqlite().await;
})
.await;
}

pub async fn connection() -> WasmSqliteConnection {
diesel_wasm_sqlite::init_sqlite().await;
WasmSqliteConnection::establish(":memory:").unwrap()
Expand All @@ -26,7 +9,6 @@ pub async fn connection() -> WasmSqliteConnection {
// re-exports used in tests
#[allow(unused)]
pub mod prelude {
pub(crate) use super::init;
pub(crate) use diesel::{
connection::{Connection, LoadConnection},
debug_query,
Expand All @@ -36,12 +18,12 @@ pub mod prelude {
sql_types::{Integer, Nullable, Text},
};
pub(crate) use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
pub(crate) use diesel_wasm_sqlite::{
connection::WasmSqliteConnection, WasmSqlite,
};
#[cfg(any(feature = "test-utils"))]
pub(crate) use diesel_wasm_sqlite::utils::init;
#[cfg(feature = "unsafe-debug-query")]
pub(crate) use diesel_wasm_sqlite::DebugQueryWrapper;
pub(crate) use diesel_wasm_sqlite::{connection::WasmSqliteConnection, WasmSqlite};
pub(crate) use serde::Deserialize;
pub(crate) use wasm_bindgen_test::*;
pub(crate) use web_sys::console;
#[cfg(feature = "unsafe-debug-query")]
pub(crate) use diesel_wasm_sqlite::DebugQueryWrapper;
}
File renamed without changes.
File renamed without changes.
40 changes: 23 additions & 17 deletions tests/test/web.rs → diesel-wasm-sqlite/tests/test/web.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! General tests for migrations/diesel ORM/persistant databases
use crate::common::prelude::*;
use diesel_wasm_sqlite::dsl::RunQueryDsl;
use diesel_wasm_sqlite::{dsl::RunQueryDsl, init_sqlite};

pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./tests/migrations/");

Expand Down Expand Up @@ -80,7 +80,8 @@ fn examine_sql_from_insert_default_values() {

#[wasm_bindgen_test]
async fn test_orm_insert() {
init().await;
init_sqlite().await;
diesel_wasm_sqlite::init_sqlite().await;
let mut conn = establish_connection().await;
let new_books = vec![
BookForm {
Expand Down Expand Up @@ -197,7 +198,8 @@ fn insert_or_ignore(updates: &[Item], conn: &mut WasmSqliteConnection) {

#[wasm_bindgen_test]
async fn can_insert_or_ignore() {
init().await;
init_sqlite().await;
diesel_wasm_sqlite::init_sqlite().await;
let mut conn = establish_connection().await;
let updates = vec![
Item {
Expand All @@ -218,7 +220,9 @@ async fn can_insert_or_ignore() {

#[wasm_bindgen_test]
async fn can_retrieve_blob() {
init().await;
init_sqlite().await;
diesel_wasm_sqlite::init_sqlite().await;

let mut conn = establish_connection().await;
let updates = vec![
Item {
Expand All @@ -243,12 +247,13 @@ async fn can_retrieve_blob() {

assert_eq!(res[0].payload, b"testing 1");
assert_eq!(res[1].payload, b"testing 2");

}

#[wasm_bindgen_test]
async fn serializing() {
init().await;
init_sqlite().await;
diesel_wasm_sqlite::init_sqlite().await;

let mut conn = establish_connection().await;
let new_books = vec![BookForm {
title: "Game of Thrones".into(),
Expand Down Expand Up @@ -292,9 +297,11 @@ async fn serializing() {

#[wasm_bindgen_test]
async fn can_find() {
use schema::{books::dsl, self};
use schema::{self, books::dsl};

init_sqlite().await;
diesel_wasm_sqlite::init_sqlite().await;

init().await;
let mut conn = establish_connection().await;
let new_books = vec![
BookForm {
Expand All @@ -304,22 +311,21 @@ async fn can_find() {
BookForm {
title: "1984".into(),
author: Some("George Orwell".into()),
}
},
];

let changed = insert_books(&mut conn, new_books).unwrap();
tracing::info!("{changed} rows changed");

let res: Option<StoredBook> = dsl::books.find(1).first(&mut conn).optional().unwrap();

let res: Vec<StoredBook> = diesel::sql_query("SELECT * FROM books where (id = 1)")
.load::<StoredBook>(&mut conn).unwrap();
.load::<StoredBook>(&mut conn)
.unwrap();
tracing::debug!("SQL_QUERY RES: {:?}", res);
/*
assert_eq!(res, vec![
StoredBook { id: 0, title: "Game of Thrones".into(), author: Some("George R.R".into()) }
]);
*/
/*
assert_eq!(res, vec![
StoredBook { id: 0, title: "Game of Thrones".into(), author: Some("George R.R".into()) }
]);
*/
let stored_books = schema::books::dsl::books
.select(StoredBook::as_select())
.load(&mut conn);
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
curl
wasm-pack
twiggy
wasm-bindgen-cli
# wasm-bindgen-cli
binaryen
linters
cargo-nextest
Expand Down
1 change: 0 additions & 1 deletion src/utils.rs

This file was deleted.

10 changes: 10 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "xtask"
edition = "2021"
version.workspace = true

[dependencies]
color-eyre = "0.6"
xshell = "0.2"
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = [ "env-filter" ] }
Loading

0 comments on commit e5056e6

Please sign in to comment.