indexeddb: Pass the db transaction into do_schema_upgrade closures

For some operations (notably: adding an index to an existing object store), we
need access to the database transation during the upgrade operation.
This commit is contained in:
Andy Balaam
2024-08-02 11:44:05 +01:00
committed by Richard van der Hoff
parent b8d90286aa
commit eecd00cd98
6 changed files with 20 additions and 9 deletions

View File

@@ -176,9 +176,20 @@ async fn db_version(name: &str) -> Result<u32, IndexeddbCryptoStoreError> {
type OldVersion = u32;
/// Run a database schema upgrade operation
///
/// # Arguments
///
/// * `name` - name of the indexeddb database to be upgraded.
/// * `version` - version we are upgrading to.
/// * `f` - closure which will be called if the database is below the version
/// given. It will be called with three arguments `(db, txn, oldver)`, where:
/// * `db` - the [`IdbDatabase`]
/// * `txn` - the database transaction: a [`IdbTransaction`]
/// * `oldver` - the version number before the upgrade.
async fn do_schema_upgrade<F>(name: &str, version: u32, f: F) -> Result<(), DomException>
where
F: Fn(&IdbDatabase, OldVersion) -> Result<(), JsValue> + 'static,
F: Fn(&IdbDatabase, IdbTransaction<'_>, OldVersion) -> Result<(), JsValue> + 'static,
{
info!("IndexeddbCryptoStore upgrade schema -> v{version} starting");
let mut db_req: OpenDbRequest = IdbDatabase::open_u32(name, version)?;
@@ -190,7 +201,7 @@ where
let old_version = evt.old_version() as u32;
// Run the upgrade code we were supplied
f(evt.db(), old_version)
f(evt.db(), evt.transaction(), old_version)
}));
let db = db_req.await?;

View File

@@ -26,7 +26,7 @@ use crate::crypto_store::{
/// Perform schema migrations as needed, up to schema version 5.
pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> {
do_schema_upgrade(name, 5, |db, old_version| {
do_schema_upgrade(name, 5, |db, _, old_version| {
// An old_version of 1 could either mean actually the first version of the
// schema, or a completely empty schema that has been created with a
// call to `IdbDatabase::open` with no explicit "version". So, to determine

View File

@@ -57,5 +57,5 @@ pub(crate) async fn data_migrate(
pub(crate) async fn schema_bump(name: &str) -> crate::crypto_store::Result<(), DomException> {
// Just bump the version number to 11 to demonstrate that we have run the data
// changes from data_migrate.
do_schema_upgrade(name, 11, |_, _| Ok(())).await
do_schema_upgrade(name, 11, |_, _, _| Ok(())).await
}

View File

@@ -36,7 +36,7 @@ use crate::{
/// Perform the schema upgrade v5 to v6, creating `inbound_group_sessions2`.
pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> {
do_schema_upgrade(name, 6, |db, _| {
do_schema_upgrade(name, 6, |db, _, _| {
let object_store = db.create_object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?;
add_nonunique_index(
@@ -109,7 +109,7 @@ pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -
/// Perform the schema upgrade v6 to v7, deleting `inbound_group_sessions`.
pub(crate) async fn schema_delete(name: &str) -> Result<(), DomException> {
do_schema_upgrade(name, 7, |db, _| {
do_schema_upgrade(name, 7, |db, _, _| {
db.delete_object_store(old_keys::INBOUND_GROUP_SESSIONS_V1)?;
Ok(())
})

View File

@@ -113,7 +113,7 @@ pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -
/// Perform the schema upgrade v7 to v8, Just bumping the schema version.
pub(crate) async fn schema_bump(name: &str) -> Result<(), DomException> {
do_schema_upgrade(name, 8, |_, _| {
do_schema_upgrade(name, 8, |_, _, _| {
// Just bump the version number to 8 to demonstrate that we have run the data
// changes from prepare_data_for_v8.
Ok(())

View File

@@ -35,7 +35,7 @@ use crate::{
/// Perform the schema upgrade v8 to v9, creating `inbound_group_sessions3`.
pub(crate) async fn schema_add(name: &str) -> Result<(), DomException> {
do_schema_upgrade(name, 9, |db, _| {
do_schema_upgrade(name, 9, |db, _, _| {
let object_store = db.create_object_store(keys::INBOUND_GROUP_SESSIONS_V3)?;
add_nonunique_index(
@@ -128,7 +128,7 @@ pub(crate) async fn data_migrate(name: &str, serializer: &IndexeddbSerializer) -
/// Perform the schema upgrade v8 to v10, deleting `inbound_group_sessions2`.
pub(crate) async fn schema_delete(name: &str) -> Result<(), DomException> {
do_schema_upgrade(name, 10, |db, _| {
do_schema_upgrade(name, 10, |db, _, _| {
db.delete_object_store(old_keys::INBOUND_GROUP_SESSIONS_V2)?;
Ok(())
})