-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JSON-API] Rework prior work and introduce the object SchemaHandling
changelog_begin - [JSON-API] Schema versioning was introduced to the db schema. Because of this the field `createSchema` in the jdbcConfig was removed and replaced by the field `schemaHandling` which can have the following values: 1. `ForceCreateAndTerminate`: This is equal to the behaviour of `createSchema=true` so the db schema is created and then the application terminates. 2. `CheckAndTerminateIfWrong`: With this the schema version is checked and if no version or an outdated version was found the application terminates. 3. `CreateOrUpdateAndContinue`: With this the schema version is checked and if no version or an outdated version was found the schema will be created/updated and the application proceeds with the startup. 4. `ForceCreateAndContinue`: Similar to the first option but instead of terminating the application proceeds with the startup. changelog_end
- Loading branch information
1 parent
9938b6a
commit f4c6b7c
Showing
12 changed files
with
139 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
ledger-service/http-json-cli/src/main/scala/com/daml/http/SchemaHandling.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.daml.http | ||
import scalaz.std.option._ | ||
import scalaz.syntax.traverse._ | ||
|
||
private[http] sealed trait SchemaHandling | ||
private[http] object SchemaHandling { | ||
private[http] case object ForceCreateAndTerminate extends SchemaHandling | ||
private[http] case object CheckAndTerminateIfWrong extends SchemaHandling | ||
private[http] case object CreateOrUpdateAndContinue extends SchemaHandling | ||
private[http] case object ForceCreateAndContinue extends SchemaHandling | ||
|
||
import scalaz.Validation.{success, failure} | ||
import scalaz.Validation | ||
|
||
private[http] def optionalSchemaHandlingField( | ||
m: Map[String, String] | ||
)(k: String): Either[String, Option[SchemaHandling]] = { | ||
val parse: String => Validation[String, SchemaHandling] = { | ||
case "ForceCreateAndTerminate" => success(ForceCreateAndTerminate) | ||
case "CheckAndTerminateIfWrong" => success(CheckAndTerminateIfWrong) | ||
case "CreateOrUpdateAndContinue" => success(CreateOrUpdateAndContinue) | ||
case "ForceCreateAndContinue" => success(ForceCreateAndContinue) | ||
case opt => failure(s"Unrecognized option $opt") | ||
} | ||
m.get(k).traverse(input => parse(input).disjunction).toEither | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
ledger-service/http-json/src/main/scala/com/digitalasset/http/SchemaHandlingResult.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.daml.http | ||
|
||
import com.daml.http.dbbackend.ContractDao | ||
import com.daml.logging.{ContextualizedLogger, LoggingContextOf} | ||
import cats.effect.IO | ||
import com.daml.http.util.Logging.InstanceUUID | ||
|
||
sealed trait SchemaHandlingResult | ||
object SchemaHandlingResult { | ||
case object Terminate extends SchemaHandlingResult | ||
case object Continue extends SchemaHandlingResult | ||
|
||
def fromBool(shouldTerminate: Boolean): SchemaHandlingResult = | ||
if (shouldTerminate) Terminate else Continue | ||
|
||
private[this] val logger = ContextualizedLogger.get(getClass) | ||
|
||
def fromSchemaHandling(dao: ContractDao, schemaHandling: SchemaHandling)(implicit | ||
lc: LoggingContextOf[InstanceUUID] | ||
): IO[SchemaHandlingResult] = { | ||
import dao.{logHandler, jdbcDriver} | ||
def terminate: IO[SchemaHandlingResult] = IO.pure(Terminate) | ||
def reinit(shouldTerminate: Boolean): IO[SchemaHandlingResult] = { | ||
logger.info("Creating DB schema...") | ||
dao.transact(ContractDao.initialize).map { _ => | ||
logger.info("DB schema created...") | ||
SchemaHandlingResult.fromBool(shouldTerminate) | ||
} | ||
} | ||
def checkVersion(shouldTerminate: Boolean): IO[SchemaHandlingResult] = { | ||
logger.info("Checking for existing schema") | ||
dao.transact(jdbcDriver.queries.version()).flatMap { | ||
case None => | ||
logger.info("No schema version found") | ||
if (shouldTerminate) terminate | ||
else reinit(shouldTerminate = false) | ||
case Some(version) => | ||
logger.info(s"DB schema version $version found") | ||
if (version < jdbcDriver.queries.schemaVersion) { | ||
logger.info("DB schema version is not up to date") | ||
if (shouldTerminate) terminate | ||
else { | ||
logger.info(s"Re-initializing with version ${jdbcDriver.queries.schemaVersion}") | ||
reinit(shouldTerminate = false) | ||
} | ||
} else { | ||
logger.info("DB schema is up-to-date, continuing startup") | ||
IO.pure(Continue) | ||
} | ||
} | ||
} | ||
schemaHandling match { | ||
case SchemaHandling.ForceCreateAndTerminate => reinit(shouldTerminate = true) | ||
case SchemaHandling.ForceCreateAndContinue => reinit(shouldTerminate = false) | ||
case SchemaHandling.CheckAndTerminateIfWrong => | ||
checkVersion(shouldTerminate = true) | ||
case SchemaHandling.CreateOrUpdateAndContinue => | ||
checkVersion(shouldTerminate = false) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters