Fix inserting contracts in Navigator (#2272)

* Fix inserting contracts in Navigator

Fixes #2271

* Add release notes

* Log all database errors

* Fix typo in release notes

* Better express intent

* NonFatal is not necessary here, as we are not catching exceptions. Bad advice on my part

* remove unused import
This commit is contained in:
Robert Autenrieth 2019-07-25 10:30:43 +02:00 committed by mergify[bot]
parent 8d8b09c45f
commit 24b8a6d10f
3 changed files with 74 additions and 60 deletions

View File

@ -9,6 +9,7 @@ import cats.effect.{ContextShift, IO}
import cats.implicits._
import com.digitalasset.ledger.api.refinements.ApiTypes
import com.digitalasset.navigator.model._
import com.typesafe.scalalogging.LazyLogging
import doobie._
import doobie.implicits._
import scalaz.syntax.tag._
@ -21,7 +22,7 @@ import scala.util.{Failure, Success, Try}
* and make the transformation between Scala and data store types
*/
@SuppressWarnings(Array("org.wartremover.warts.Any"))
class DatabaseActions {
class DatabaseActions extends LazyLogging {
/**
* Uncomment the log handler to enable query logging
@ -95,11 +96,19 @@ class DatabaseActions {
} yield SqlQueryResult(colNames, data.map(_.map(Option(_).map(_.toString).getOrElse("null"))))
}
def runQuery(query: String): Try[SqlQueryResult] = {
/** Returns the given value, logging failures */
private def logErrors[T](result: Try[T]): Try[T] = {
result.failed.foreach { t =>
logger.error("Error executing database action, Navigator may be in a corrupted state", t)
}
result
}
def runQuery(query: String): Try[SqlQueryResult] = logErrors {
Try(Queries.query(query).execWith(exec).transact(xa).unsafeRunSync())
}
def insertEvent(event: Event): Try[Int] = {
def insertEvent(event: Event): Try[Int] = logErrors {
Try {
Queries
.insertEvent(EventRow.fromEvent(event))
@ -110,7 +119,7 @@ class DatabaseActions {
}
}
def eventById(id: ApiTypes.EventId, types: PackageRegistry): Try[Option[Event]] = {
def eventById(id: ApiTypes.EventId, types: PackageRegistry): Try[Option[Event]] = logErrors {
Try {
Queries
.eventById(id.unwrap)
@ -125,23 +134,24 @@ class DatabaseActions {
}.flatMap(_.sequence)
}
def eventsByParentId(parentId: ApiTypes.EventId, types: PackageRegistry): Try[List[Event]] = {
Try {
Queries
.eventsByParentId(parentId.unwrap)
.query[EventRow]
.to[List]
.transact(xa)
.unsafeRunSync()
.map { data =>
data.toEvent(types)
}
}.flatMap(_.sequence)
}
def eventsByParentId(parentId: ApiTypes.EventId, types: PackageRegistry): Try[List[Event]] =
logErrors {
Try {
Queries
.eventsByParentId(parentId.unwrap)
.query[EventRow]
.to[List]
.transact(xa)
.unsafeRunSync()
.map { data =>
data.toEvent(types)
}
}.flatMap(_.sequence)
}
def createEventByContractId(
id: ApiTypes.ContractId,
types: PackageRegistry): Try[ContractCreated] = {
types: PackageRegistry): Try[ContractCreated] = logErrors {
Try {
Queries
.eventByTypeAndContractId("ContractCreated", id.unwrap)
@ -161,7 +171,7 @@ class DatabaseActions {
def archiveEventByContractId(
id: ApiTypes.ContractId,
types: PackageRegistry): Try[Option[ChoiceExercised]] = {
types: PackageRegistry): Try[Option[ChoiceExercised]] = logErrors {
Try {
Queries
.eventByTypeAndContractId("ContractArchived", id.unwrap)
@ -182,7 +192,7 @@ class DatabaseActions {
def choiceExercisedEventByContractById(
id: ApiTypes.ContractId,
types: PackageRegistry): Try[List[ChoiceExercised]] = {
types: PackageRegistry): Try[List[ChoiceExercised]] = logErrors {
Try {
Queries
.eventByTypeAndContractId("ChoiceExercised", id.unwrap)
@ -200,7 +210,7 @@ class DatabaseActions {
}.flatMap(_.sequence)
}
def insertTransaction(tx: Transaction): Try[Int] = {
def insertTransaction(tx: Transaction): Try[Int] = logErrors {
Try {
Queries
.insertTransaction(TransactionRow.fromTransaction(tx))
@ -213,7 +223,7 @@ class DatabaseActions {
def transactionById(
id: ApiTypes.TransactionId,
types: PackageRegistry): Try[Option[Transaction]] = {
types: PackageRegistry): Try[Option[Transaction]] = logErrors {
Try {
Queries
.topLevelEventsByTransactionId(id.unwrap)
@ -240,7 +250,7 @@ class DatabaseActions {
}.flatten
}
def lastTransaction(types: PackageRegistry): Try[Option[Transaction]] = {
def lastTransaction(types: PackageRegistry): Try[Option[Transaction]] = logErrors {
Try {
val txData = Queries
.lastTransaction()
@ -268,7 +278,7 @@ class DatabaseActions {
}.flatMap(_.sequence)
}
def upsertCommandStatus(commandId: ApiTypes.CommandId, cs: CommandStatus): Try[Int] = {
def upsertCommandStatus(commandId: ApiTypes.CommandId, cs: CommandStatus): Try[Int] = logErrors {
Try {
Queries
.upsertCommandStatus(CommandStatusRow.fromCommandStatus(commandId, cs))
@ -279,7 +289,7 @@ class DatabaseActions {
}
}
def updateCommandStatus(commandId: ApiTypes.CommandId, cs: CommandStatus): Try[Int] = {
def updateCommandStatus(commandId: ApiTypes.CommandId, cs: CommandStatus): Try[Int] = logErrors {
Try {
Queries
.updateCommandStatus(CommandStatusRow.fromCommandStatus(commandId, cs))
@ -292,7 +302,7 @@ class DatabaseActions {
def commandStatusByCommandId(
commandId: ApiTypes.CommandId,
types: PackageRegistry): Try[Option[CommandStatus]] = {
types: PackageRegistry): Try[Option[CommandStatus]] = logErrors {
Try {
Queries
.commandStatusByCommandId(commandId.unwrap)
@ -307,7 +317,7 @@ class DatabaseActions {
}.flatMap(_.sequence)
}
def insertCommand(cmd: Command): Try[Int] = {
def insertCommand(cmd: Command): Try[Int] = logErrors {
Try {
Queries
.insertCommand(CommandRow.fromCommand(cmd))
@ -318,20 +328,21 @@ class DatabaseActions {
}
}
def commandById(id: ApiTypes.CommandId, types: PackageRegistry): Try[Option[Command]] = {
Try {
Queries
.commandById(id.unwrap)
.query[CommandRow]
.to[List]
.transact(xa)
.unsafeRunSync()
.headOption
.map(_.toCommand(types))
}.flatMap(_.sequence)
}
def commandById(id: ApiTypes.CommandId, types: PackageRegistry): Try[Option[Command]] =
logErrors {
Try {
Queries
.commandById(id.unwrap)
.query[CommandRow]
.to[List]
.transact(xa)
.unsafeRunSync()
.headOption
.map(_.toCommand(types))
}.flatMap(_.sequence)
}
def allCommands(types: PackageRegistry): Try[List[Command]] = {
def allCommands(types: PackageRegistry): Try[List[Command]] = logErrors {
Try {
Queries
.allCommands()
@ -343,7 +354,7 @@ class DatabaseActions {
}.flatMap(_.sequence)
}
def insertContract(contract: Contract): Try[Int] = {
def insertContract(contract: Contract): Try[Int] = logErrors {
Try {
Queries
.insertContract(ContractRow.fromContract(contract))
@ -356,7 +367,7 @@ class DatabaseActions {
def archiveContract(
contractId: ApiTypes.ContractId,
archiveTransactionId: ApiTypes.TransactionId): Try[Int] = {
archiveTransactionId: ApiTypes.TransactionId): Try[Int] = logErrors {
Try {
Queries
.archiveContract(contractId.unwrap, archiveTransactionId.unwrap)
@ -367,7 +378,7 @@ class DatabaseActions {
}
}
def contractCount(): Try[Int] = {
def contractCount(): Try[Int] = logErrors {
Try {
Queries
.contractCount()
@ -378,7 +389,7 @@ class DatabaseActions {
}
}
def activeContractCount(): Try[Int] = {
def activeContractCount(): Try[Int] = logErrors {
Try {
Queries
.activeContractCount()
@ -389,7 +400,7 @@ class DatabaseActions {
}
}
def contract(id: ApiTypes.ContractId, types: PackageRegistry): Try[Option[Contract]] = {
def contract(id: ApiTypes.ContractId, types: PackageRegistry): Try[Option[Contract]] = logErrors {
Try {
Queries
.contract(id.unwrap)
@ -402,7 +413,7 @@ class DatabaseActions {
}.flatMap(_.sequence)
}
def contracts(types: PackageRegistry): Try[List[Contract]] = {
def contracts(types: PackageRegistry): Try[List[Contract]] = logErrors {
Try {
Queries.contracts
.query[ContractRow]
@ -413,7 +424,7 @@ class DatabaseActions {
}.flatMap(_.sequence)
}
def activeContracts(types: PackageRegistry): Try[List[Contract]] = {
def activeContracts(types: PackageRegistry): Try[List[Contract]] = logErrors {
Try {
Queries.activeContracts
.query[ContractRow]
@ -424,21 +435,22 @@ class DatabaseActions {
}.flatMap(_.sequence)
}
def contractsForTemplate(tId: DamlLfIdentifier, types: PackageRegistry): Try[List[Contract]] = {
Try {
Queries
.contractsForTemplate(tId.asOpaqueString)
.query[ContractRow]
.to[List]
.transact(xa)
.unsafeRunSync()
.map(_.toContract(types))
}.flatMap(_.sequence)
}
def contractsForTemplate(tId: DamlLfIdentifier, types: PackageRegistry): Try[List[Contract]] =
logErrors {
Try {
Queries
.contractsForTemplate(tId.asOpaqueString)
.query[ContractRow]
.to[List]
.transact(xa)
.unsafeRunSync()
.map(_.toContract(types))
}.flatMap(_.sequence)
}
def activeContractsForTemplate(
tId: DamlLfIdentifier,
types: PackageRegistry): Try[List[Contract]] = {
types: PackageRegistry): Try[List[Contract]] = logErrors {
Try {
Queries
.activeContractsForTemplate(tId.asOpaqueString)

View File

@ -228,7 +228,7 @@ object Queries {
sql"""
INSERT INTO
contract
(id, template_id, archive_transaction_id, argument, agreement_text, signatories, observers)
(id, template_id, archive_transaction_id, argument, agreement_text, signatories, observers, contract_key)
VALUES
(${row.id}, ${row.templateId}, ${row.archiveTransactionId}, ${row.argument}, ${row.agreementText}, ${row.signatories}, ${row.observers}, ${row.key})
"""

View File

@ -22,3 +22,5 @@ HEAD — ongoing
supported anymore. Use ``module_name`` and ``entity_name`` instead.
- [Documentation] Improved the Maven pom.xml file for ``quickstart-java`` to better integrate with VS Code.
See `#887 <https://github.com/digital-asset/daml/issues/887>`__.
- [Navigator] Fixed an issue when Navigator console did not see any contracts.
See `#2271 <https://github.com/digital-asset/daml/issues/2271 >`__.