transaction note isar migration

This commit is contained in:
julian 2023-11-09 09:33:51 -06:00
parent e20d16436d
commit 335c2b9993
3 changed files with 70 additions and 10 deletions

View file

@ -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<void> migrateWalletsToIsar({
(await Hive.openBox<String>(DB.boxNameFavoriteWallets)).values.toList();
final List<WalletInfo> newInfo = [];
final List<TransactionNote> migratedNotes = [];
//
// Convert each old info into the new Isar WalletInfo
@ -53,6 +56,33 @@ Future<void> migrateWalletsToIsar({
for (final old in oldInfo) {
final walletBox = await Hive.openBox<dynamic>(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<String, String>.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<void> 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);
});

View file

@ -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;

View file

@ -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<String, String>.from(walletbackup["notes"] as Map? ?? {});
final List<TransactionNote> 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;
}