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

fix stored script path on reading from filesystem #103

Merged
merged 2 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading