Skip to content

Commit

Permalink
Update Flyway and Elastic4s
Browse files Browse the repository at this point in the history
  • Loading branch information
adpi2 committed Nov 28, 2022
1 parent 8411dee commit 217d2ee
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 39 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ lazy val infra = project
libraryDependencies ++= Seq(
"com.sksamuel.elastic4s" %% "elastic4s-client-esjava" % V.elastic4sVersion,
"org.json4s" %% "json4s-native" % V.json4s,
"org.flywaydb" % "flyway-core" % "8.5.13", // for database migration
"org.flywaydb" % "flyway-core" % "9.8.3", // for database migration
"com.typesafe.akka" %% "akka-stream" % V.akkaVersion,
"com.typesafe.akka" %% "akka-http" % V.akkaHttpVersion,
"de.heikoseeberger" %% "akka-http-circe" % "1.39.2",
Expand Down Expand Up @@ -244,7 +244,7 @@ lazy val V = new {
val playJsonVersion = "2.9.3"
val akkaVersion = "2.6.18"
val akkaHttpVersion = "10.2.10"
val elastic4sVersion = "8.4.4"
val elastic4sVersion = "8.5.0"
val nscalaTimeVersion = "2.32.0"
val scalatest = "3.2.14"
val circeVersion = "0.14.3"
Expand Down
6 changes: 4 additions & 2 deletions modules/infra/src/main/scala/scaladex/infra/SqlDatabase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ import scaladex.infra.sql.ReleaseDependenciesTable
import scaladex.infra.sql.ReleaseTable
import scaladex.infra.sql.UserSessionsTable

class SqlDatabase(datasource: HikariDataSource, xa: doobie.Transactor[IO]) extends SchedulerDatabase with LazyLogging {
private val flyway = DoobieUtils.flyway(datasource)
class SqlDatabase(datasource: HikariDataSource, xa: doobie.Transactor[IO], testMode: Boolean = false)
extends SchedulerDatabase
with LazyLogging {
private val flyway = DoobieUtils.flyway(datasource, testMode)
def migrate: IO[Unit] = IO(flyway.migrate())
def dropTables: IO[Unit] = IO(flyway.clean())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,17 @@ class V7_2__edit_platform_and_language extends BaseJavaMigration with ScaladexBa

import V7_2__edit_platform_and_language._

override def migrate(context: Context): Unit =
try {
(for {
oldArtifacts <- run(xa)(selectArtifact.to[Seq])
groupedArtifacts = oldArtifacts.grouped(10000).toSeq
_ <- groupedArtifacts
.map(artifacts => run(xa)(updatePlatformAndLanguage.updateMany(artifacts.map(_.update))))
.sequence
_ <- run(xa)(sql"ALTER TABLE artifacts DROP COLUMN binary_version".update.run)
} yield ())
.unsafeRunSync()

} catch {
case e: Throwable =>
logger.info("failed to migrate the database")
throw new Exception(s"failed to migrate the database because of ${e.getMessage}")
}
override def migrate(context: Context): Unit = {
val migration = for {
oldArtifacts <- run(xa)(selectArtifact.to[Seq])
groupedArtifacts = oldArtifacts.grouped(10000).toSeq
_ <- groupedArtifacts
.map(artifacts => run(xa)(updatePlatformAndLanguage.updateMany(artifacts.map(_.update))))
.sequence
_ <- run(xa)(sql"ALTER TABLE artifacts DROP COLUMN binary_version".update.run)
} yield ()
migration.unsafeRunSync()
}

val selectArtifact: Query0[OldArtifact] = selectRequest("artifacts", Seq("*"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,11 @@ object DoobieUtils {
private implicit val cs: ContextShift[IO] =
IO.contextShift(ExecutionContext.global)

def flyway(conf: PostgreSQLConfig): Flyway = {
val datasource = getHikariDataSource(conf)
flyway(datasource)
}
def flyway(datasource: HikariDataSource): Flyway =
def flyway(datasource: HikariDataSource, testMode: Boolean): Flyway =
Flyway
.configure()
.dataSource(datasource)
.cleanDisabled(!testMode)
.locations("migrations", "scaladex/infra/migrations")
.load()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ trait BaseDatabaseSuite extends IOChecker with BeforeAndAfterEach {
private implicit val cs: ContextShift[IO] =
IO.contextShift(ExecutionContext.global)

private val config: PostgreSQLConfig = PostgreSQLConfig
.load()
.get

private val config: PostgreSQLConfig = PostgreSQLConfig.load().get
override val transactor: Transactor.Aux[IO, Unit] =
Transactor
.fromDriverManager[IO](
Expand All @@ -35,7 +32,9 @@ trait BaseDatabaseSuite extends IOChecker with BeforeAndAfterEach {
config.pass.decode
)

lazy val database = new SqlDatabase(BaseDatabaseSuite.datasource, transactor)
val datasource: HikariDataSource = DoobieUtils.getHikariDataSource(config)

lazy val database = new SqlDatabase(datasource, transactor, testMode = true)

override def beforeEach(): Unit =
Await.result(cleanTables(), Duration.Inf)
Expand All @@ -48,10 +47,3 @@ trait BaseDatabaseSuite extends IOChecker with BeforeAndAfterEach {
reset.unsafeToFuture()
}
}
object BaseDatabaseSuite {
private val config: PostgreSQLConfig = PostgreSQLConfig
.load()
.get

val datasource: HikariDataSource = DoobieUtils.getHikariDataSource(config)
}
2 changes: 1 addition & 1 deletion modules/server/src/it/scala/scaladex/RelevanceTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RelevanceTest extends TestKit(ActorSystem("SbtActorTest")) with AsyncFunSu
val transactor = DoobieUtils.transactor(datasource)
transactor
.use { xa =>
val database = new SqlDatabase(datasource, xa)
val database = new SqlDatabase(datasource, xa, testMode = true)
val filesystem = FilesystemStorage(config.filesystem)

val searchSync = new SearchSynchronizer(database, searchEngine)
Expand Down

0 comments on commit 217d2ee

Please sign in to comment.