mirror of
				https://github.com/TheAnachronism/docspell.git
				synced 2025-10-31 17:50:11 +00:00 
			
		
		
		
	Apply fixup migration only from previous version
This commit is contained in:
		| @@ -6,6 +6,7 @@ | ||||
|  | ||||
| package docspell.store.migrate | ||||
|  | ||||
| import cats.data.OptionT | ||||
| import cats.effect.Sync | ||||
| import cats.implicits._ | ||||
|  | ||||
| @@ -59,11 +60,16 @@ class FlywayMigrate[F[_]: Sync](jdbc: JdbcConfig, xa: Transactor[F]) { | ||||
|       case true => | ||||
|         ().pure[F] | ||||
|       case false => | ||||
|         for { | ||||
|           fw <- createFlyway(MigrationKind.Fixups) | ||||
|           _ <- logger.info(s"!!! Running fixup migrations") | ||||
|           _ <- Sync[F].blocking(fw.migrate()) | ||||
|         } yield () | ||||
|         (for { | ||||
|           current <- OptionT(getSchemaVersion) | ||||
|           _ <- OptionT | ||||
|             .fromOption[F](versionComponents(current)) | ||||
|             .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] = | ||||
| @@ -73,6 +79,19 @@ class FlywayMigrate[F[_]: Sync](jdbc: JdbcConfig, xa: Transactor[F]) { | ||||
|       .attemptSql | ||||
|       .transact(xa) | ||||
|       .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 { | ||||
|   | ||||
										
											
												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) | ||||
|       _ <- Resource.eval(store.migrate) | ||||
|     } 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 | ||||
|     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