From 085b8b191032ae595dc747f940a4a8edb4abcbdb Mon Sep 17 00:00:00 2001 From: Chris Stewart Date: Mon, 6 Dec 2021 07:19:31 -0600 Subject: [PATCH] Implement CRUDAction.deleteAction() (#3877) --- .../src/main/scala/org/bitcoins/db/CRUD.scala | 14 ++++++----- .../scala/org/bitcoins/db/CRUDAction.scala | 19 ++++++++++++++ .../scala/org/bitcoins/wallet/Wallet.scala | 25 +++++++++++-------- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/db-commons/src/main/scala/org/bitcoins/db/CRUD.scala b/db-commons/src/main/scala/org/bitcoins/db/CRUD.scala index 7ece20fd24..d12498d729 100644 --- a/db-commons/src/main/scala/org/bitcoins/db/CRUD.scala +++ b/db-commons/src/main/scala/org/bitcoins/db/CRUD.scala @@ -91,19 +91,21 @@ abstract class CRUD[T, PrimaryKeyType](implicit */ def delete(t: T): Future[Int] = { logger.debug("Deleting record: " + t) - val query: Query[Table[_], T, Seq] = find(t) - safeDatabase.run(query.delete) + val action = deleteAction(t) + safeDatabase.run(action.transactionally) } def deleteAll(ts: Vector[T]): Future[Int] = { - val query: Query[Table[_], T, Seq] = findAll(ts) - safeDatabase.run(query.delete) + val action = deleteAllAction(ts).transactionally + safeDatabase.run(action) } /** delete all records from the table */ - def deleteAll(): Future[Int] = - safeDatabase.run(table.delete.transactionally) + def deleteAll(): Future[Int] = { + val action = deleteAllAction().transactionally + safeDatabase.run(action) + } /** insert the record if it does not exist, update it if it does * diff --git a/db-commons/src/main/scala/org/bitcoins/db/CRUDAction.scala b/db-commons/src/main/scala/org/bitcoins/db/CRUDAction.scala index 45d4f01012..219655ac93 100644 --- a/db-commons/src/main/scala/org/bitcoins/db/CRUDAction.scala +++ b/db-commons/src/main/scala/org/bitcoins/db/CRUDAction.scala @@ -62,4 +62,23 @@ abstract class CRUDAction[T, PrimaryKeyType](implicit //thus cannot be updated sequencedA.map(_.flatten) } + + def deleteAction(t: T): DBIOAction[Int, NoStream, Effect.Write] = { + deleteAllAction(Vector(t)) + } + + def deleteAllAction( + ts: Vector[T]): DBIOAction[Int, NoStream, Effect.Write] = { + val query = findAll(ts) + query.delete + } + + /** WARNING: Deletes all rows in table, use with care */ + def deleteAllAction(): DBIOAction[ + Int, + NoStream, + Effect.Write with Effect.Transactional] = { + table.delete.transactionally + } + } diff --git a/wallet/src/main/scala/org/bitcoins/wallet/Wallet.scala b/wallet/src/main/scala/org/bitcoins/wallet/Wallet.scala index d284d0491e..b588699310 100644 --- a/wallet/src/main/scala/org/bitcoins/wallet/Wallet.scala +++ b/wallet/src/main/scala/org/bitcoins/wallet/Wallet.scala @@ -32,12 +32,13 @@ import org.bitcoins.core.wallet.utxo.TxoState._ import org.bitcoins.core.wallet.utxo._ import org.bitcoins.crypto._ import org.bitcoins.db.models.MasterXPubDAO -import org.bitcoins.db.{DatabaseDriver, SQLiteUtil} +import org.bitcoins.db.{DatabaseDriver, SQLiteUtil, SafeDatabase} import org.bitcoins.keymanager.bip39.BIP39KeyManager import org.bitcoins.wallet.config.WalletAppConfig import org.bitcoins.wallet.internal._ import org.bitcoins.wallet.models._ import scodec.bits.ByteVector +import slick.dbio.{DBIOAction, Effect, NoStream} import java.nio.file.Path import java.time.Instant @@ -86,6 +87,7 @@ abstract class Wallet private[bitcoins] val stateDescriptorDAO: WalletStateDescriptorDAO = WalletStateDescriptorDAO() + private val safeDatabase: SafeDatabase = spendingInfoDAO.safeDatabase val nodeApi: NodeApi val chainQueryApi: ChainQueryApi val creationTime: Instant = keyManager.creationTime @@ -257,22 +259,25 @@ abstract class Wallet } override def clearAllUtxosAndAddresses(): Future[Wallet] = { - val resultedF = for { - _ <- addressTagDAO.deleteAll() - _ <- spendingInfoDAO.deleteAll() - _ <- addressDAO.deleteAll() - _ <- scriptPubKeyDAO.deleteAll() + val aggregatedActions: DBIOAction[ + Unit, + NoStream, + Effect.Write with Effect.Transactional] = for { + _ <- addressTagDAO.deleteAllAction() + _ <- spendingInfoDAO.deleteAllAction() + _ <- addressDAO.deleteAllAction() + _ <- scriptPubKeyDAO.deleteAllAction() } yield { - logger.info( - s"Done clearing all utxos, addresses and scripts from the database") - this + () } + + val resultedF = safeDatabase.run(aggregatedActions) resultedF.failed.foreach(err => logger.error( s"Failed to clear utxos, addresses and scripts from the database", err)) - resultedF + resultedF.map(_ => this) } /** Sums up the value of all unspent