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 ledger on SQL
  • Loading branch information
aherrmann committed Nov 5, 2020
commit a477b994f8fa57a8b2d22d6a2871d90b2053f0ac
1 change: 1 addition & 0 deletions ledger/ledger-on-sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ da_scala_test_suite(
"//ledger/participant-state/kvutils",
"//ledger/participant-state/kvutils:kvutils-tests-lib",
"//libs-scala/contextualized-logging",
"//libs-scala/flyway-testing",
"//libs-scala/postgresql-testing",
"//libs-scala/resources",
"@maven//:com_typesafe_akka_akka_actor_2_12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,76 +3,9 @@

package com.daml.ledger.on.sql

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.ledger.on.sql.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.size should be >= 3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the purpose of the >= check here but have kept it alive in the refactored version. cc @SamirTalwar-DA


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/ledger/on/sql/migrations"
private val hashMigrationsScriptPath = "ledger/ledger-on-sql/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/ledger/on/sql/migrations"
protected override val hashMigrationsScriptPath = "ledger/ledger-on-sql/hash-migrations.sh"
}
2 changes: 1 addition & 1 deletion libs-scala/flyway-testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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__"],
tags = ["maven_coordinates=com.daml:flyway-testing:__VERSION__"],
visibility = ["//visibility:public"],
deps = [
"@maven//:org_flywaydb_flyway_core",
Expand Down