Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal_macros/src/generic_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ make_opts!(Opts => {
db_name|index_name|store_name|key_path => ::core::convert::AsRef<str>,
db_version => crate::factory::DBVersion,
blocked_cb => ::core::ops::FnOnce(crate::database::VersionChangeEvent) -> crate::Result<()> + 'static,
upgrade_cb => ::core::ops::FnOnce(crate::database::VersionChangeEvent, crate::database::Database) -> crate::Result<()> + 'static,
upgrade_cb => ::core::ops::FnOnce(crate::database::VersionChangeEvent, &crate::transaction::Transaction<'_>) -> crate::Result<()> + 'static,
[custom] => {
upgrade_async_cb => UpgradeAsyncCb,
},
Expand Down
14 changes: 11 additions & 3 deletions src/future/open_db/listener.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::database::{Database, VersionChangeEvent};
use crate::error::{Error, UnexpectedDataError};
use crate::transaction::{OnTransactionDrop, Transaction};
use internal_macros::generic_bounds;
use std::fmt::{Debug, Display, Formatter};
use std::mem;
Expand Down Expand Up @@ -56,9 +57,16 @@ impl OpenDbListener {
#[cfg(feature = "async-upgrade")]
async_notify: Self::fake_rx(),
listener: Closure::once(move |evt: web_sys::IdbVersionChangeEvent| {
let res = Database::from_event(&evt)
.and_then(move |db| callback(VersionChangeEvent::new(evt), db));

let res = Database::from_event(&evt).and_then(|db| {
Transaction::from_raw_version_change_event(&db, &evt).and_then(|mut tx| {
callback(VersionChangeEvent::new(evt), &tx).inspect(|_| {
// If the callback succeeded, we want to ensure that
// the transaction is committed when dropped and not
// aborted.
tx.on_drop(OnTransactionDrop::Commit);
})
})
});
Self::handle_result(LBL_UPGRADE, &status, res)
}),
}
Expand Down
1 change: 0 additions & 1 deletion src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ impl<'a> Transaction<'a> {
///
/// This is useful for extracting the transaction being used to upgrade
/// the database.
#[allow(unused)]
pub(crate) fn from_raw_version_change_event(
db: &'a Database,
event: &web_sys::IdbVersionChangeEvent,
Expand Down
6 changes: 4 additions & 2 deletions tests/tests/database/delete_obj_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub async fn invalid_state_error() {
#[wasm_bindgen_test]
pub async fn not_found_error() {
let err = Database::open(random_str())
.with_on_upgrade_needed(move |_, db| {
.with_on_upgrade_needed(move |_, tx| {
let db = tx.db();
db.delete_object_store(&db.name())?;
Ok(())
})
Expand All @@ -28,7 +29,8 @@ pub async fn happy_path() {
let n1_clone = n1.clone();

let db = Database::open(&n1)
.with_on_upgrade_needed(move |_, db| {
.with_on_upgrade_needed(move |_, tx| {
let db = tx.db();
db.create_object_store(&n1_clone).build()?;
db.create_object_store(&random_str())
.build()?
Expand Down
6 changes: 4 additions & 2 deletions tests/tests/database/obj_store_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub async fn happy_path() {
#[wasm_bindgen_test]
pub async fn constraint_error() {
let err = Database::open(random_str())
.with_on_upgrade_needed(move |_, db| {
.with_on_upgrade_needed(move |_, tx| {
let db = tx.db();
let name = random_str();
db.create_object_store(&name).build()?;
db.create_object_store(&name).build()?;
Expand All @@ -27,7 +28,8 @@ pub async fn constraint_error() {
#[wasm_bindgen_test]
pub async fn invalid_access_error() {
let err = Database::open(random_str())
.with_on_upgrade_needed(move |_, db| {
.with_on_upgrade_needed(move |_, tx| {
let db = tx.db();
db.create_object_store(&db.name())
.with_auto_increment(true)
.with_key_path("".into())
Expand Down
15 changes: 10 additions & 5 deletions tests/tests/example_reproductions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ pub async fn multi_threaded_executor() {
}

let db = Database::open("my_db_multi_threaded_executor")
.with_on_upgrade_needed(|_, db| {
.with_on_upgrade_needed(|_, tx| {
let db = tx.db();
db.create_object_store("my_store")
.with_auto_increment(true)
.build()?;
Expand Down Expand Up @@ -111,7 +112,8 @@ pub async fn rw_serde() {
}

let db = Database::open("example_rw_serde")
.with_on_upgrade_needed(|_, db| {
.with_on_upgrade_needed(|_, tx| {
let db = tx.db();
db.create_object_store("users")
.with_key_path("id".into())
.build()?;
Expand Down Expand Up @@ -159,7 +161,8 @@ pub async fn readme_example() {
async fn main() -> indexed_db_futures::OpenDbResult<()> {
let db = Database::open("my_db_readme_example")
.with_version(2u8)
.with_on_upgrade_needed(|event, db| {
.with_on_upgrade_needed(|event, tx| {
let db = tx.db();
// Convert versions from floats to integers to allow using them in match expressions
let old_version = event.old_version() as u64;
let new_version = event.new_version().map(|v| v as u64);
Expand Down Expand Up @@ -262,7 +265,8 @@ pub async fn iterating_a_cursor() {

let db = Database::open("example_iterating_a_cursor")
.with_version(2u8)
.with_on_upgrade_needed(|_, db| {
.with_on_upgrade_needed(|_, tx| {
let db = tx.db();
db.create_object_store("my_store").build()?;
Ok(())
})
Expand Down Expand Up @@ -329,7 +333,8 @@ pub async fn iterating_index_as_a_stream() {
}

let db = Database::open("example_iterating_index_as_a_stream")
.with_on_upgrade_needed(|_, db| {
.with_on_upgrade_needed(|_, tx| {
let db = tx.db();
let store = db
.create_object_store("my_store")
.with_key_path("id".into())
Expand Down
3 changes: 2 additions & 1 deletion tests/tests/index/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use idb_fut::database::Database;
#[wasm_bindgen_test]
pub async fn constraint_error() {
let err = Database::open(random_str())
.with_on_upgrade_needed(move |_, db| {
.with_on_upgrade_needed(move |_, tx| {
let db = tx.db();
let name = db.name();
let store = db
.create_object_store(&name)
Expand Down
6 changes: 4 additions & 2 deletions tests/tests/object_store/add_put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ macro_rules! common_tests {

#[wasm_bindgen_test]
pub async fn data_error_inline_key() {
let db = random_db_with_init(move |_, db| {
let db = random_db_with_init(move |_, tx| {
let db = tx.db();
db.create_object_store(&db.name())
.with_key_path("foo".into())
.build()?;
Expand Down Expand Up @@ -64,7 +65,8 @@ macro_rules! common_tests {
#[cfg(feature = "serde")]
#[wasm_bindgen_test]
pub async fn serde_object_nesting() {
let db = random_db_with_init(move |_, db| {
let db = random_db_with_init(move |_, tx| {
let db = tx.db();
db.create_object_store(&db.name())
.with_key_path("foo".into())
.build()?;
Expand Down
9 changes: 6 additions & 3 deletions tests/tests/object_store/query_source/key_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use idb_fut::KeyPath;
#[wasm_bindgen_test]
pub async fn auto_incremented() {
let db = Database::open(random_str())
.with_on_upgrade_needed(move |_, db| {
.with_on_upgrade_needed(move |_, tx| {
let db = tx.db();
db.create_object_store(&db.name())
.with_auto_increment(true)
.build()?;
Expand All @@ -21,7 +22,8 @@ pub async fn auto_incremented() {
#[wasm_bindgen_test]
pub async fn none() {
let db = Database::open(random_str())
.with_on_upgrade_needed(move |_, db| {
.with_on_upgrade_needed(move |_, tx| {
let db = tx.db();
db.create_object_store(&db.name()).build()?;
Ok(())
})
Expand All @@ -35,7 +37,8 @@ pub async fn none() {
#[wasm_bindgen_test]
pub async fn explicit() {
let db = Database::open(random_str())
.with_on_upgrade_needed(move |_, db| {
.with_on_upgrade_needed(move |_, tx| {
let db = tx.db();
db.create_object_store(&db.name())
.with_key_path(Key::KEY_PATH)
.build()?;
Expand Down
3 changes: 2 additions & 1 deletion tests/tests/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ pub mod on_done;

#[wasm_bindgen_test]
pub async fn multi_store() {
let db = random_db_with_init(move |_, db| {
let db = random_db_with_init(move |_, tx| {
let db = tx.db();
db.create_object_store("s1").build()?;
db.create_object_store("s2").build()?;
Ok(())
Expand Down
12 changes: 8 additions & 4 deletions tests/tests/utils/init.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::prelude::*;
use idb_fut::database::{Database, VersionChangeEvent};
use indexed_db_futures::transaction::Transaction;

pub async fn random_db_with_init<F>(on_upgrade_needed: F) -> Database
where
F: Fn(VersionChangeEvent, Database) -> idb_fut::Result<()> + 'static,
F: Fn(VersionChangeEvent, &Transaction<'_>) -> idb_fut::Result<()> + 'static,
{
Database::open(random_str())
.with_on_upgrade_needed(on_upgrade_needed)
Expand All @@ -13,7 +14,8 @@ where

/// Crate a DB with and an object store with a matching name and default params.
pub async fn random_db_with_store() -> Database {
random_db_with_init(move |_, db| {
random_db_with_init(move |_, tx| {
let db = tx.db();
db.create_object_store(&db.name()).build()?;
Ok(())
})
Expand All @@ -22,7 +24,8 @@ pub async fn random_db_with_store() -> Database {

/// Create a random DB and a store with a matching name that expect [`KeyVal`] inputs.
pub async fn random_db_keyval() -> Database {
random_db_with_init(move |_, db| {
random_db_with_init(move |_, tx| {
let db = tx.db();
db.create_object_store(&db.name())
.with_auto_increment(false)
.with_key_path(Key::KEY_PATH)
Expand All @@ -35,7 +38,8 @@ pub async fn random_db_keyval() -> Database {
/// [`random_db_keyval`] + an index with default params.
#[cfg(feature = "indices")]
pub async fn random_db_idx_keyval() -> Database {
random_db_with_init(move |_, db| {
random_db_with_init(move |_, tx| {
let db = tx.db();
let name = db.name();
let store = db
.create_object_store(&name)
Expand Down