mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-02-22 22:36:34 +01:00
Implement CRUDAction.deleteAction() (#3877)
This commit is contained in:
parent
6e7af37ca0
commit
085b8b1910
3 changed files with 42 additions and 16 deletions
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Reference in a new issue