mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 10:28:27 +00:00
Apply fixup migration only from previous version
This commit is contained in:
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
package docspell.store.migrate
|
package docspell.store.migrate
|
||||||
|
|
||||||
|
import cats.data.OptionT
|
||||||
import cats.effect.Sync
|
import cats.effect.Sync
|
||||||
import cats.implicits._
|
import cats.implicits._
|
||||||
|
|
||||||
@ -59,11 +60,16 @@ class FlywayMigrate[F[_]: Sync](jdbc: JdbcConfig, xa: Transactor[F]) {
|
|||||||
case true =>
|
case true =>
|
||||||
().pure[F]
|
().pure[F]
|
||||||
case false =>
|
case false =>
|
||||||
for {
|
(for {
|
||||||
fw <- createFlyway(MigrationKind.Fixups)
|
current <- OptionT(getSchemaVersion)
|
||||||
_ <- logger.info(s"!!! Running fixup migrations")
|
_ <- OptionT
|
||||||
_ <- Sync[F].blocking(fw.migrate())
|
.fromOption[F](versionComponents(current))
|
||||||
} yield ()
|
.filter(v => v._1 >= 1 && v._2 >= 33)
|
||||||
|
fw <- OptionT.liftF(createFlyway(MigrationKind.Fixups))
|
||||||
|
_ <- OptionT.liftF(logger.info(s"!!! Running fixup migrations"))
|
||||||
|
_ <- OptionT.liftF(Sync[F].blocking(fw.migrate()))
|
||||||
|
} yield ())
|
||||||
|
.getOrElseF(logger.info(s"Fixup migrations not applied."))
|
||||||
}
|
}
|
||||||
|
|
||||||
private def isSchemaEmpty: F[Boolean] =
|
private def isSchemaEmpty: F[Boolean] =
|
||||||
@ -73,6 +79,19 @@ class FlywayMigrate[F[_]: Sync](jdbc: JdbcConfig, xa: Transactor[F]) {
|
|||||||
.attemptSql
|
.attemptSql
|
||||||
.transact(xa)
|
.transact(xa)
|
||||||
.map(_.isLeft)
|
.map(_.isLeft)
|
||||||
|
|
||||||
|
private def getSchemaVersion: F[Option[String]] =
|
||||||
|
sql"select version from flyway_schema_history where success = true order by installed_rank desc limit 1"
|
||||||
|
.query[String]
|
||||||
|
.option
|
||||||
|
.transact(xa)
|
||||||
|
|
||||||
|
private def versionComponents(v: String): Option[(Int, Int, Int)] =
|
||||||
|
v.split('.').toList.map(_.toIntOption) match {
|
||||||
|
case Some(a) :: Some(b) :: Some(c) :: Nil =>
|
||||||
|
Some((a, b, c))
|
||||||
|
case _ => None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object FlywayMigrate {
|
object FlywayMigrate {
|
||||||
|
File diff suppressed because one or more lines are too long
@ -97,4 +97,20 @@ object StoreFixture {
|
|||||||
store = new StoreImpl[IO](fr, jdbc, ds, xa)
|
store = new StoreImpl[IO](fr, jdbc, ds, xa)
|
||||||
_ <- Resource.eval(store.migrate)
|
_ <- Resource.eval(store.migrate)
|
||||||
} yield store
|
} yield store
|
||||||
|
|
||||||
|
def restoreH2Dump(resourceName: String, ds: DataSource): IO[Unit] =
|
||||||
|
Option(getClass.getResource(resourceName)).map(_.getFile) match {
|
||||||
|
case Some(file) =>
|
||||||
|
IO {
|
||||||
|
org.log4s.getLogger.info(s"Restoring dump from $file")
|
||||||
|
val stmt = ds.getConnection.createStatement()
|
||||||
|
val sql = s"RUNSCRIPT FROM '$file'"
|
||||||
|
stmt.execute(sql)
|
||||||
|
stmt.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
case None =>
|
||||||
|
IO.raiseError(new Exception(s"Resource not found: $resourceName"))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -29,4 +29,21 @@ class H2MigrateTest extends FunSuite with TestLoggingConfig {
|
|||||||
// a second time to apply fixup migrations
|
// a second time to apply fixup migrations
|
||||||
assert(result.unsafeRunSync().migrationsExecuted == 0)
|
assert(result.unsafeRunSync().migrationsExecuted == 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test("h2 upgrade db from 0.24.0") {
|
||||||
|
val dump = "/docspell-0.24.0-dump-h2-1.24.0-2021-07-13-2307.sql"
|
||||||
|
|
||||||
|
val jdbc = StoreFixture.memoryDB("h2test2")
|
||||||
|
val ds = StoreFixture.dataSource(jdbc)
|
||||||
|
|
||||||
|
ds.use(StoreFixture.restoreH2Dump(dump, _)).unsafeRunSync()
|
||||||
|
|
||||||
|
val result =
|
||||||
|
ds.flatMap(StoreFixture.makeXA).use { xa =>
|
||||||
|
FlywayMigrate[IO](jdbc, xa).run
|
||||||
|
}
|
||||||
|
|
||||||
|
result.unsafeRunSync()
|
||||||
|
result.unsafeRunSync()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user