Implement CRUDAction.deleteAction() (#3877)

This commit is contained in:
Chris Stewart 2021-12-06 07:19:31 -06:00 committed by GitHub
parent 6e7af37ca0
commit 085b8b1910
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 16 deletions

View file

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

View file

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

View file

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