Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check trigger dao migrations digests #7908

Merged
merged 7 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use abstract migrations test in trigger service tests
  • Loading branch information
aherrmann committed Nov 5, 2020
commit b1670ab4db70fb916fed74918c0e923ef2ef9d2b
2 changes: 2 additions & 0 deletions libs-scala/flyway-testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ load("//bazel_tools:scala.bzl", "da_scala_library")
da_scala_library(
name = "flyway-testing",
srcs = glob(["src/main/scala/**/*.scala"]),
tags = ["maven_coordinates=com.daml:flyway:__VERSION__"],
visibility = ["//visibility:public"],
deps = [
"@maven//:org_flywaydb_flyway_core",
"@maven//:org_scalactic_scalactic_2_12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import org.scalatest.WordSpec

import scala.collection.JavaConverters._

abstract class ImmutableMigrationsSpec extends WordSpec {
abstract class AbstractImmutableMigrationsSpec extends WordSpec {
protected def migrationsResourcePath: String
protected def hashMigrationsScriptPath: String

Expand Down
1 change: 1 addition & 0 deletions triggers/service/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ da_scala_test(
"//ledger/participant-state",
"//ledger/sandbox-classic",
"//ledger/sandbox-common",
"//libs-scala/flyway-testing",
"//libs-scala/ports",
"//libs-scala/postgresql-testing",
"//libs-scala/resources",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,9 @@

package com.daml.lf.engine.trigger

import java.io.{BufferedReader, FileNotFoundException}
import java.math.BigInteger
import java.nio.charset.Charset
import java.security.MessageDigest
import java.util
import com.daml.flyway.AbstractImmutableMigrationsSpec

import com.daml.lf.engine.trigger.ImmutableMigrationsSpec._
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.configuration.FluentConfiguration
import org.flywaydb.core.internal.resource.LoadableResource
import org.flywaydb.core.internal.scanner.{LocationScannerCache, ResourceNameCache, Scanner}
import org.scalatest.Matchers._
import org.scalatest.WordSpec

import scala.collection.JavaConverters._

class ImmutableMigrationsSpec extends WordSpec {
"migration files" should {
"never change, according to their accompanying digest file" in {
val configuration = Flyway
.configure()
.locations(s"classpath:/$migrationsResourcePath")
val resourceScanner = flywayScanner(configuration)
val resources = resourceScanner.getResources("", ".sql").asScala.toSeq

resources.foreach { resource =>
val migrationFile = resource.getRelativePath
val digestFile = migrationFile + ".sha256"
val expectedDigest = readExpectedDigest(digestFile, resourceScanner)
val currentDigest = computeCurrentDigest(resource, configuration.getEncoding)
assert(
currentDigest == expectedDigest,
s"""The contents of the migration file "$migrationFile" have changed! Migrations are immutable; you must not change their contents or their digest.""",
)
}
}
}
}

object ImmutableMigrationsSpec {
private val migrationsResourcePath = "com/daml/lf/engine/trigger/db/migration/postgres"
private val hashMigrationsScriptPath = "trigger/service/hash-migrations.sh"

private def flywayScanner(configuration: FluentConfiguration) =
new Scanner(
classOf[Object],
util.Arrays.asList(configuration.getLocations: _*),
getClass.getClassLoader,
configuration.getEncoding,
new ResourceNameCache,
new LocationScannerCache,
)

private def readExpectedDigest(
digestFile: String,
resourceScanner: Scanner[_],
): String = {
val resource = Option(resourceScanner.getResource(digestFile))
.getOrElse(throw new FileNotFoundException(
s"""\"$digestFile\" is missing. If you are introducing a new Flyway migration step, you need to create an SHA-256 digest file by running $hashMigrationsScriptPath."""))
new BufferedReader(resource.read()).readLine()
}

private def computeCurrentDigest(resource: LoadableResource, encoding: Charset): String = {
val sha256 = MessageDigest.getInstance("SHA-256")
new BufferedReader(resource.read())
.lines()
.forEach(line => sha256.update((line + "\n").getBytes(encoding)))
val digest = sha256.digest()
String.format(s"%0${digest.length * 2}x", new BigInteger(1, digest))
}
class ImmutableMigrationsSpec extends AbstractImmutableMigrationsSpec {
protected override val migrationsResourcePath = "com/daml/lf/engine/trigger/db/migration/postgres"
protected override val hashMigrationsScriptPath = "trigger/service/hash-migrations.sh"
}