Skip to content

Commit

Permalink
use sql transaction to ensure atomicity in the nft history migration …
Browse files Browse the repository at this point in the history
…process
  • Loading branch information
laruh committed Sep 13, 2024
1 parent 0f2566e commit 661fc80
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions mm2src/coins/nft/storage/sql_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1409,41 +1409,46 @@ impl NftTransferHistoryStorageOps for AsyncMutexGuard<'_, AsyncConnection> {
}

fn migrate_tx_history_table_schema(
conn: &Connection,
conn: &mut Connection,
history_table: SafeTableName,
schema_table: SafeTableName,
) -> Result<(), AsyncConnError> {
if has_primary_key_duplication(conn, &history_table)? {
return Err(AsyncConnError::Internal(InternalError(
"Primary key duplication occurred in old tx history table".to_string(),
"Primary key duplication occurred in old nft tx history table".to_string(),
)));
}

// Start a transaction to ensure all operations are atomic
let sql_tx = conn.transaction()?;

let temp_table_name = SafeTableName::new(format!("{}_temp", history_table.inner()).as_str())?;
conn.execute(&create_transfer_history_table_sql_custom_name(&temp_table_name)?, [])?;
sql_tx.execute(&create_transfer_history_table_sql_custom_name(&temp_table_name)?, [])?;

let copy_data_sql = format!(
"INSERT INTO {} SELECT * FROM {};",
temp_table_name.inner(),
history_table.inner()
);
conn.execute(&copy_data_sql, [])?;
sql_tx.execute(&copy_data_sql, [])?;

let drop_old_table_sql = format!("DROP TABLE {};", history_table.inner());
conn.execute(&drop_old_table_sql, [])?;
sql_tx.execute(&drop_old_table_sql, [])?;

let rename_table_sql = format!(
"ALTER TABLE {} RENAME TO {};",
temp_table_name.inner(),
history_table.inner()
);
conn.execute(&rename_table_sql, [])?;
sql_tx.execute(&rename_table_sql, [])?;

conn.execute(&update_schema_version_sql(&schema_table), [
sql_tx.execute(&update_schema_version_sql(&schema_table), [
CURRENT_SCHEMA_VERSION_TX_HISTORY.to_string(),
history_table.inner().to_string(),
])?;

sql_tx.commit()?;

Ok(())
}

Expand Down

0 comments on commit 661fc80

Please sign in to comment.