Skip to content

Commit

Permalink
fix stored script path on reading from filesystem (#103)
Browse files Browse the repository at this point in the history
* fix stored script path on reading from filesystem

* minor refactoring
  • Loading branch information
rolang authored Dec 1, 2024
1 parent a871e24 commit 8651083
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
8 changes: 1 addition & 7 deletions modules/core/shared/src/main/scala/dumbo/Dumbo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,13 @@ class Dumbo[F[_]: Sync: Console](
version = source.versionText,
description = source.scriptDescription,
`type` = "SQL",
script = historyScriptPath(source),
script = resReader.relativeResourcePath(source),
checksum = Some(source.checksum),
executionTimeMs = duration.toMillis.toInt,
success = true,
)
}

private def historyScriptPath(resource: ResourceFile) =
resReader.locationRel match {
case Some(loc) => resource.path.value.stripPrefix(s"/$loc/")
case _ => resource.fileName
}

private def validationGuard(session: Session[F], resources: ResourceFiles) =
if (resources.nonEmpty) {
session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import java.io.File

import cats.effect.Sync
import cats.implicits.*
import dumbo.ResourceFilePath
import dumbo.{ResourceFile, ResourceFilePath}
import fs2.io.file.{Files as Fs2Files, Flags, Path}
import fs2.{Stream, text}

private[dumbo] trait ResourceReader[F[_]] {
// relative location info e.g. "db/migration"
def locationRel: Option[String]
def relativeResourcePath(resource: ResourceFile): String

// relative or absolute location info e.g. "file:/project/resources/db/migration"
def location: Option[String]

def list: fs2.Stream[F, ResourceFilePath]
Expand Down Expand Up @@ -44,8 +42,11 @@ private[dumbo] object ResourceReader {
}

new ResourceReader[F] {
override val locationRel: Option[String] = Some(sourceDir.toString)
override val location: Option[String] = Some(absolutePath(sourceDir).toString)

override def relativeResourcePath(resource: ResourceFile): String =
absolutePath(sourceDir).relativize(absolutePath(Path(resource.path.value))).toString

override val location: Option[String] = Some(absolutePath(sourceDir).toString)
override def list: Stream[F, ResourceFilePath] =
Stream.emits(
listRec(List(new File(absolutePath(sourceDir).toString)), Nil).map(f => ResourceFilePath(f.getPath()))
Expand All @@ -67,7 +68,11 @@ private[dumbo] object ResourceReader {
locationRelative: Option[String] = None,
): ResourceReader[F] =
new ResourceReader[F] {
override val locationRel: Option[String] = locationRelative
override def relativeResourcePath(resource: ResourceFile): String =
locationRelative match {
case Some(l) => resource.path.value.stripPrefix(s"/$l/")
case _ => resource.fileName
}

override val location: Option[String] = locationInfo

Expand Down
17 changes: 16 additions & 1 deletion modules/tests-flyway/src/test/scala/DumboFlywaySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ trait DumboFlywaySpec extends ffstest.FTest {
} yield ()
}

dbTest("Compatible with nested directories") {
dbTest("Compatible with nested directories on reading from resources") {
val schema = "schema_1"

for {
Expand All @@ -151,6 +151,21 @@ trait DumboFlywaySpec extends ffstest.FTest {
} yield ()
}

dbTest("Compatible with nested directories on reading from filesystem") {
val schema = "schema_1"

for {
_ <- flywayMigrate(schema, Path("db/nested")).map(r => assert(r.migrationsExecuted == 6))
historyFlyway <- loadHistory(schema)
_ <- dropSchemas
_ <- dumboMigrate(schema, dumboWithFiles(Path("modules/tests/shared/src/test/resources/db/nested"))).map(r =>
assert(r.migrationsExecuted == 6)
)
historyDumbo <- loadHistory(schema)
_ = assertEqualHistory(historyDumbo, historyFlyway)
} yield ()
}

dbTest("Dumbo updates history entry of latest unsucessfully applied migration by Flyway") {
// run on CockroachDb only just because it was the easiest way to reproduce a history record for an unsuccessfully applied migration with Flyway
if (db == Db.CockroachDb) {
Expand Down

0 comments on commit 8651083

Please sign in to comment.