Skip to content

Commit

Permalink
change version check and add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Sep 15, 2024
1 parent df164d4 commit 041f99c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
14 changes: 13 additions & 1 deletion mm2src/coins/nft/storage/wasm/wasm_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use num_traits::ToPrimitive;
use serde_json::{self as json, Value as Json};
use std::collections::HashSet;
use std::num::NonZeroUsize;
use wasm_bindgen::JsValue;
use web_sys::console;

const CHAIN_TOKEN_ADD_TOKEN_ID_INDEX: &str = "chain_token_add_token_id_index";
const CHAIN_BLOCK_NUMBER_INDEX: &str = "chain_block_number_index";
Expand Down Expand Up @@ -1012,9 +1014,11 @@ impl TableSignature for NftTransferHistoryTable {
table.create_multi_index(CHAIN_IMAGE_DOMAIN_INDEX, &["chain", "image_domain"], false)?;
table.create_index("block_number", false)?;
table.create_index("chain", false)?;
} else if old_version == 1 && new_version == 2 {
} else if new_version == 2 {
// Migration from version 1 to version 2

console::log_1(&JsValue::from_str("Migrating from version 1 to version 2."));

// Step 1: Create a temporary table to hold data with the old schema
let temp_table_name = format!("{}_temp", Self::TABLE_NAME);
let temp_table = upgrader.create_table(&temp_table_name)?;
Expand Down Expand Up @@ -1042,9 +1046,13 @@ impl TableSignature for NftTransferHistoryTable {
// TODO copy data from old_store to temp_store
copy_store_data_sync(&old_store.object_store, &temp_store.object_store)?;

console::log_1(&JsValue::from_str("Copied data from old store to temp store"));

// Step 3: Delete the old object store
upgrader.delete_table(Self::TABLE_NAME)?;

console::log_1(&JsValue::from_str("Deleted old object store"));

// Step 4: Recreate the original object store with the new schema
let new_table = upgrader.create_table(Self::TABLE_NAME)?;
new_table.create_multi_index(
Expand All @@ -1068,8 +1076,12 @@ impl TableSignature for NftTransferHistoryTable {
// TODO copy data from temp_store to new_table
copy_store_data_sync(&temp_store.object_store, &new_table.object_store)?;

console::log_1(&JsValue::from_str("Copied data from temp store to new store"));

// Step 6: Delete the temporary store
upgrader.delete_table(&temp_table_name)?;

console::log_1(&JsValue::from_str("Deleted temp store"));
}
Ok(())
}
Expand Down
3 changes: 0 additions & 3 deletions mm2src/mm2_db/src/indexed_db/drivers/upgrader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,5 @@ pub fn copy_store_data_sync(
// Prevent the closure from being dropped
onsuccess_callback.forget();

// Note: We cannot block the function here to wait for completion.
// The transaction will remain open until all requests are completed.

Ok(())
}

0 comments on commit 041f99c

Please sign in to comment.