migrate serialized amount string in transaction

This commit is contained in:
julian 2023-03-24 15:46:12 -06:00
parent b2b9accee1
commit 66919ec4b8
2 changed files with 51 additions and 1 deletions

View file

@ -42,7 +42,7 @@ abstract class Constants {
// Enable Logger.print statements // Enable Logger.print statements
static const bool disableLogger = false; static const bool disableLogger = false;
static const int currentHiveDbVersion = 7; static const int currentHiveDbVersion = 8;
static const int rescanV1 = 1; static const int rescanV1 = 1;

View file

@ -279,6 +279,17 @@ class DbVersionMigrator with WalletDB {
// try to continue migrating // try to continue migrating
return await migrate(7, secureStore: secureStore); return await migrate(7, secureStore: secureStore);
case 7:
// migrate
await _v7(secureStore);
// update version
await DB.instance.put<dynamic>(
boxName: DB.boxNameDBInfo, key: "hive_data_version", value: 7);
// try to continue migrating
return await migrate(8, secureStore: secureStore);
default: default:
// finally return // finally return
return; return;
@ -396,4 +407,43 @@ class DbVersionMigrator with WalletDB {
} }
} }
} }
Future<void> _v7(SecureStorageInterface secureStore) async {
await Hive.openBox<dynamic>(DB.boxNameAllWalletsData);
final walletsService = WalletsService(secureStorageInterface: secureStore);
final walletInfoList = await walletsService.walletNames;
await MainDB.instance.initMainDB();
for (final walletId in walletInfoList.keys) {
final info = walletInfoList[walletId]!;
assert(info.walletId == walletId);
final count = await MainDB.instance.getTransactions(walletId).count();
for (var i = 0; i < count; i += 50) {
final txns = await MainDB.instance
.getTransactions(walletId)
.offset(i)
.limit(50)
.findAll();
// migrate amount to serialized amount string
final txnsData = txns
.map(
(tx) => Tuple2(
tx
..amountString = Amount(
rawValue: BigInt.from(tx.amount),
fractionDigits: info.coin.decimals,
).toJsonString(),
tx.address.value,
),
)
.toList();
// update db records
await MainDB.instance.addNewTransactionData(txnsData, walletId);
}
}
}
} }