From 335c2b999348557e4f5f3665e25acbe833b52287 Mon Sep 17 00:00:00 2001 From: julian Date: Thu, 9 Nov 2023 09:33:51 -0600 Subject: [PATCH] transaction note isar migration --- lib/db/migrate_wallets_to_isar.dart | 36 ++++++++++++++++++ lib/models/isar/models/transaction_note.dart | 6 ++- .../helpers/restore_create_backup.dart | 38 ++++++++++++++----- 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/lib/db/migrate_wallets_to_isar.dart b/lib/db/migrate_wallets_to_isar.dart index 34d5047ca..cac0d8a41 100644 --- a/lib/db/migrate_wallets_to_isar.dart +++ b/lib/db/migrate_wallets_to_isar.dart @@ -1,9 +1,11 @@ import 'dart:convert'; import 'package:hive_flutter/hive_flutter.dart'; +import 'package:isar/isar.dart'; import 'package:stackwallet/db/hive/db.dart'; import 'package:stackwallet/db/isar/main_db.dart'; import 'package:stackwallet/models/isar/models/blockchain_data/address.dart'; +import 'package:stackwallet/models/isar/models/transaction_note.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/wallets/isar/models/wallet_info.dart'; @@ -46,6 +48,7 @@ Future migrateWalletsToIsar({ (await Hive.openBox(DB.boxNameFavoriteWallets)).values.toList(); final List newInfo = []; + final List migratedNotes = []; // // Convert each old info into the new Isar WalletInfo @@ -53,6 +56,33 @@ Future migrateWalletsToIsar({ for (final old in oldInfo) { final walletBox = await Hive.openBox(old.walletId); + // + // First handle transaction notes + // + final newNoteCount = await MainDB.instance.isar.transactionNotes + .where() + .walletIdEqualTo(old.walletId) + .count(); + if (newNoteCount == 0) { + final map = walletBox.get('notes') as Map?; + + if (map != null) { + final notes = Map.from(map); + + for (final txid in notes.keys) { + final note = notes[txid]; + if (note != null && note.isNotEmpty) { + final newNote = TransactionNote( + walletId: old.walletId, + txid: txid, + value: note, + ); + migratedNotes.add(newNote); + } + } + } + } + // // Set other data values // @@ -110,6 +140,12 @@ Future migrateWalletsToIsar({ newInfo.add(info); } + if (migratedNotes.isNotEmpty) { + await MainDB.instance.isar.writeTxn(() async { + await MainDB.instance.isar.transactionNotes.putAll(migratedNotes); + }); + } + await MainDB.instance.isar.writeTxn(() async { await MainDB.instance.isar.walletInfo.putAll(newInfo); }); diff --git a/lib/models/isar/models/transaction_note.dart b/lib/models/isar/models/transaction_note.dart index b85e78eda..22349adc9 100644 --- a/lib/models/isar/models/transaction_note.dart +++ b/lib/models/isar/models/transaction_note.dart @@ -25,7 +25,11 @@ class TransactionNote { @Index() late String walletId; - @Index(unique: true, composite: [CompositeIndex("walletId")]) + @Index( + unique: true, + replace: true, + composite: [CompositeIndex("walletId")], + ) late String txid; late String value; diff --git a/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart b/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart index cc721231b..3c21f0aa4 100644 --- a/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart +++ b/lib/pages/settings_views/global_settings_view/stack_backup_views/helpers/restore_create_backup.dart @@ -20,13 +20,13 @@ import 'package:stackwallet/db/isar/main_db.dart'; import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart'; import 'package:stackwallet/models/exchange/response_objects/trade.dart'; import 'package:stackwallet/models/isar/models/contact_entry.dart'; +import 'package:stackwallet/models/isar/models/transaction_note.dart'; import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/stack_restoring_ui_state.dart'; import 'package:stackwallet/models/trade_wallet_lookup.dart'; import 'package:stackwallet/models/wallet_restore_state.dart'; import 'package:stackwallet/services/address_book_service.dart'; import 'package:stackwallet/services/node_service.dart'; -import 'package:stackwallet/services/notes_service.dart'; import 'package:stackwallet/services/trade_notes_service.dart'; import 'package:stackwallet/services/trade_sent_from_stack_service.dart'; import 'package:stackwallet/services/trade_service.dart'; @@ -310,8 +310,15 @@ abstract class SWB { backupWallet['restoreHeight'] = wallet.info.restoreHeight; - NotesService notesService = NotesService(walletId: wallet.walletId); - var notes = await notesService.notes; + final isarNotes = await MainDB.instance.isar.transactionNotes + .where() + .walletIdEqualTo(wallet.walletId) + .findAll(); + + final notes = isarNotes + .asMap() + .map((key, value) => MapEntry(value.txid, value.value)); + backupWallet['notes'] = notes; backupWallets.add(backupWallet); @@ -423,15 +430,28 @@ abstract class SWB { } // restore notes - NotesService notesService = NotesService(walletId: info.walletId); - final notes = walletbackup["notes"] as Map?; - if (notes != null) { - for (final note in notes.entries) { - await notesService.editOrAddNote( - txid: note.key as String, note: note.value as String); + final notesMap = + Map.from(walletbackup["notes"] as Map? ?? {}); + final List notes = []; + + for (final key in notesMap.keys) { + if (notesMap[key] != null && notesMap[key]!.isNotEmpty) { + notes.add( + TransactionNote( + walletId: info.walletId, + txid: key, + value: notesMap[key]!, + ), + ); } } + if (notes.isNotEmpty) { + await MainDB.instance.isar.writeTxn(() async { + await MainDB.instance.isar.transactionNotes.putAll(notes); + }); + } + if (_shouldCancelRestore) { return false; }