Skip to content

Commit 6bc19a0

Browse files
committed
Update Flyway and Elastic4s
1 parent 8411dee commit 6bc19a0

23 files changed

+150
-207
lines changed

Diff for: build.sbt

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ lazy val infra = project
6767
libraryDependencies ++= Seq(
6868
"com.sksamuel.elastic4s" %% "elastic4s-client-esjava" % V.elastic4sVersion,
6969
"org.json4s" %% "json4s-native" % V.json4s,
70-
"org.flywaydb" % "flyway-core" % "8.5.13", // for database migration
70+
"org.flywaydb" % "flyway-core" % "9.8.3", // for database migration
7171
"com.typesafe.akka" %% "akka-stream" % V.akkaVersion,
7272
"com.typesafe.akka" %% "akka-http" % V.akkaHttpVersion,
7373
"de.heikoseeberger" %% "akka-http-circe" % "1.39.2",
@@ -244,7 +244,7 @@ lazy val V = new {
244244
val playJsonVersion = "2.9.3"
245245
val akkaVersion = "2.6.18"
246246
val akkaHttpVersion = "10.2.10"
247-
val elastic4sVersion = "8.4.4"
247+
val elastic4sVersion = "8.5.0"
248248
val nscalaTimeVersion = "2.32.0"
249249
val scalatest = "3.2.14"
250250
val circeVersion = "0.14.3"

Diff for: modules/infra/src/main/scala/scaladex/infra/SqlDatabase.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ import scaladex.infra.sql.ReleaseDependenciesTable
3434
import scaladex.infra.sql.ReleaseTable
3535
import scaladex.infra.sql.UserSessionsTable
3636

37-
class SqlDatabase(datasource: HikariDataSource, xa: doobie.Transactor[IO]) extends SchedulerDatabase with LazyLogging {
38-
private val flyway = DoobieUtils.flyway(datasource)
37+
class SqlDatabase(datasource: HikariDataSource, xa: doobie.Transactor[IO], testMode: Boolean = false)
38+
extends SchedulerDatabase
39+
with LazyLogging {
40+
private val flyway = DoobieUtils.flyway(datasource, testMode)
3941
def migrate: IO[Unit] = IO(flyway.migrate())
4042
def dropTables: IO[Unit] = IO(flyway.clean())
4143

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package scaladex.infra.migrations
2+
3+
import scala.concurrent.ExecutionContext
4+
5+
import cats.effect.ContextShift
6+
import cats.effect.IO
7+
import com.typesafe.scalalogging.LazyLogging
8+
import doobie.implicits._
9+
import doobie.util.transactor.Transactor
10+
import org.flywaydb.core.api.migration.BaseJavaMigration
11+
import org.flywaydb.core.api.migration.Context
12+
import scaladex.infra.config.PostgreSQLConfig
13+
14+
abstract class FlywayMigration extends BaseJavaMigration with LazyLogging {
15+
def migrationIO: IO[Unit]
16+
17+
def run[A](v: doobie.ConnectionIO[A]): IO[A] =
18+
v.transact(FlywayMigration.transactor)
19+
20+
override def migrate(context: Context): Unit =
21+
migrationIO.unsafeRunSync()
22+
}
23+
24+
object FlywayMigration {
25+
private implicit val cs: ContextShift[IO] = IO.contextShift(ExecutionContext.global)
26+
private val config = PostgreSQLConfig.load().get
27+
private val transactor: Transactor.Aux[IO, Unit] =
28+
Transactor.fromDriverManager[IO](config.driver, config.url, config.user, config.pass.decode)
29+
}

Diff for: modules/infra/src/main/scala/scaladex/infra/migrations/ScaladexBaseMigration.scala

-27
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
11
package scaladex.infra.migrations
22

3-
import com.typesafe.scalalogging.LazyLogging
4-
import org.flywaydb.core.api.migration.BaseJavaMigration
5-
import org.flywaydb.core.api.migration.Context
3+
import cats.effect.IO
64
import scaladex.infra.sql.ArtifactTable
75
import scaladex.infra.sql.ReleaseTable
86

9-
class V11_2__add_data_to_the_new_tables extends BaseJavaMigration with ScaladexBaseMigration with LazyLogging {
10-
override def migrate(context: Context): Unit =
11-
try {
12-
(for {
13-
releases <- run(xa)(ArtifactTable.getReleasesFromArtifacts.to[Seq])
14-
_ <- run(xa)(ReleaseTable.insertIfNotExists.updateMany(releases))
15-
} yield ())
16-
.unsafeRunSync()
17-
18-
} catch {
19-
case e: Throwable =>
20-
logger.info("failed to migrate the database")
21-
throw new Exception(s"failed to migrate the database because of ${e.getMessage}")
22-
}
23-
7+
class V11_2__add_data_to_the_new_tables extends FlywayMigration {
8+
override def migrationIO: IO[Unit] =
9+
for {
10+
releases <- run(ArtifactTable.getReleasesFromArtifacts.to[Seq])
11+
_ <- run(ReleaseTable.insertIfNotExists.updateMany(releases))
12+
} yield ()
2413
}

Diff for: modules/infra/src/main/scala/scaladex/infra/migrations/V13_2__update_new_fields_in_artifacts.scala

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,26 @@
11
package scaladex.infra.migrations
2+
import cats.effect.IO
23
import cats.implicits._
3-
import com.typesafe.scalalogging.LazyLogging
44
import doobie.Query0
55
import doobie.util.update.Update
6-
import org.flywaydb.core.api.migration.BaseJavaMigration
7-
import org.flywaydb.core.api.migration.Context
86
import scaladex.core.model.Artifact
97
import scaladex.core.model.Artifact.MavenReference
108
import scaladex.infra.sql.DoobieUtils.Mappings._
119
import scaladex.infra.sql.DoobieUtils.selectRequest
1210
import scaladex.infra.sql.DoobieUtils.updateRequest
1311

14-
class V13_2__update_new_fields_in_artifacts extends BaseJavaMigration with ScaladexBaseMigration with LazyLogging {
15-
override def migrate(context: Context): Unit =
16-
try {
17-
(for {
18-
oldArtifacts <- run(xa)(selectArtifact.to[Seq])
19-
groupedArtifacts = oldArtifacts.grouped(10000).toSeq
20-
_ <- groupedArtifacts
21-
.map(artifacts => run(xa)(updateNewFields.updateMany(artifacts.map(update))))
22-
.sequence
23-
} yield ())
24-
.unsafeRunSync()
12+
class V13_2__update_new_fields_in_artifacts extends FlywayMigration {
13+
override def migrationIO: IO[Unit] =
14+
for {
15+
oldArtifacts <- run(selectArtifact.to[Seq])
16+
groupedArtifacts = oldArtifacts.grouped(10000).toSeq
17+
_ <- groupedArtifacts
18+
.map(artifacts => run(updateNewFields.updateMany(artifacts.map(update))))
19+
.sequence
20+
} yield ()
2521

26-
} catch {
27-
case e: Throwable =>
28-
logger.info("failed to migrate the database")
29-
throw new Exception(s"failed to migrate the database because of ${e.getMessage}")
30-
}
31-
32-
val selectArtifact: Query0[Artifact] = selectRequest("artifacts", Seq("*"))
33-
val updateNewFields: Update[(Boolean, Boolean, MavenReference)] =
22+
private val selectArtifact: Query0[Artifact] = selectRequest("artifacts", Seq("*"))
23+
private val updateNewFields: Update[(Boolean, Boolean, MavenReference)] =
3424
updateRequest("artifacts", Seq("is_semantic", "is_prerelease"), Seq("group_id", "artifact_id", "version"))
3525

3626
private def update(artifact: Artifact): (Boolean, Boolean, MavenReference) =

Diff for: modules/infra/src/main/scala/scaladex/infra/migrations/V17__add_mill_platform.scala

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
package scaladex.infra.migrations
22

3-
import com.typesafe.scalalogging.LazyLogging
3+
import cats.effect.IO
44
import doobie._
5-
import org.flywaydb.core.api.migration.BaseJavaMigration
6-
import org.flywaydb.core.api.migration.Context
75
import scaladex.core.model._
86
import scaladex.infra.sql.DoobieUtils.Mappings._
97
import scaladex.infra.sql.DoobieUtils._
108

11-
class V17__add_mill_platform extends BaseJavaMigration with ScaladexBaseMigration with LazyLogging {
12-
override def migrate(context: Context): Unit = {
13-
val migrateIO = for {
14-
oldArtifacts <- run(xa)(selectArtifact.to[Seq])
9+
class V17__add_mill_platform extends FlywayMigration {
10+
override def migrationIO: IO[Unit] =
11+
for {
12+
oldArtifacts <- run(selectArtifact.to[Seq])
1513
newArtifacts = oldArtifacts.map { a =>
1614
val newId = Artifact.ArtifactId.parse(a.artifactId).get
1715
a.copy(artifactName = newId.name, platform = newId.binaryVersion.platform)
1816
}
1917
(toUpdate, toDelete) = newArtifacts.partition(a => isValidMillPlugin(a))
20-
_ <- run(xa)(updateNewFields.updateMany(toUpdate.map(update)))
21-
_ <- run(xa)(delete.updateMany(toDelete.map(_.mavenReference)))
18+
_ <- run(updateNewFields.updateMany(toUpdate.map(update)))
19+
_ <- run(delete.updateMany(toDelete.map(_.mavenReference)))
2220
} yield ()
23-
migrateIO.unsafeRunSync()
24-
}
2521

2622
val selectArtifact: Query0[Artifact] =
2723
selectRequest("artifacts", Seq("*"), where = Seq("artifact_name LIKE '%_mill0_%'"))

Diff for: modules/infra/src/main/scala/scaladex/infra/migrations/V7_2__edit_platform_and_language.scala

+11-21
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,30 @@ package scaladex.infra.migrations
22

33
import java.time.Instant
44

5+
import cats.effect.IO
56
import cats.implicits._
6-
import com.typesafe.scalalogging.LazyLogging
77
import doobie.Query0
88
import doobie.implicits._
99
import doobie.util.update.Update
10-
import org.flywaydb.core.api.migration.BaseJavaMigration
11-
import org.flywaydb.core.api.migration.Context
1210
import scaladex.core.model.Artifact.MavenReference
1311
import scaladex.core.model._
1412
import scaladex.infra.sql.DoobieUtils.Mappings._
1513
import scaladex.infra.sql.DoobieUtils.selectRequest
1614
import scaladex.infra.sql.DoobieUtils.updateRequest
1715

18-
class V7_2__edit_platform_and_language extends BaseJavaMigration with ScaladexBaseMigration with LazyLogging {
16+
class V7_2__edit_platform_and_language extends FlywayMigration {
1917

2018
import V7_2__edit_platform_and_language._
2119

22-
override def migrate(context: Context): Unit =
23-
try {
24-
(for {
25-
oldArtifacts <- run(xa)(selectArtifact.to[Seq])
26-
groupedArtifacts = oldArtifacts.grouped(10000).toSeq
27-
_ <- groupedArtifacts
28-
.map(artifacts => run(xa)(updatePlatformAndLanguage.updateMany(artifacts.map(_.update))))
29-
.sequence
30-
_ <- run(xa)(sql"ALTER TABLE artifacts DROP COLUMN binary_version".update.run)
31-
} yield ())
32-
.unsafeRunSync()
33-
34-
} catch {
35-
case e: Throwable =>
36-
logger.info("failed to migrate the database")
37-
throw new Exception(s"failed to migrate the database because of ${e.getMessage}")
38-
}
20+
override def migrationIO: IO[Unit] =
21+
for {
22+
oldArtifacts <- run(selectArtifact.to[Seq])
23+
groupedArtifacts = oldArtifacts.grouped(10000).toSeq
24+
_ <- groupedArtifacts
25+
.map(artifacts => run(updatePlatformAndLanguage.updateMany(artifacts.map(_.update))))
26+
.sequence
27+
_ <- run(sql"ALTER TABLE artifacts DROP COLUMN binary_version".update.run)
28+
} yield ()
3929

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

Diff for: modules/infra/src/main/scala/scaladex/infra/migrations/V9__fix_platform_and_language.scala

+12-22
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
package scaladex.infra.migrations
2-
import com.typesafe.scalalogging.LazyLogging
2+
import cats.effect.IO
33
import doobie.Query0
44
import doobie.util.update.Update
5-
import org.flywaydb.core.api.migration.BaseJavaMigration
6-
import org.flywaydb.core.api.migration.Context
75
import scaladex.core.model.Artifact.MavenReference
86
import scaladex.core.model._
97
import scaladex.infra.sql.DoobieUtils.Mappings._
108
import scaladex.infra.sql.DoobieUtils.selectRequest
119
import scaladex.infra.sql.DoobieUtils.updateRequest
1210

13-
class V9__fix_platform_and_language extends BaseJavaMigration with ScaladexBaseMigration with LazyLogging {
14-
override def migrate(context: Context): Unit =
15-
try {
16-
(for {
17-
artifactToFix <- run(xa)(selectArtifact.to[Seq])
18-
artifactToFixWithIds = artifactToFix.flatMap(a => Artifact.ArtifactId.parse(a.artifactId).map(a -> _))
19-
_ <- run(xa) {
20-
updatePlatformAndLanguage.updateMany(artifactToFixWithIds.map {
21-
case (artifact, id) => (id.binaryVersion.platform, id.binaryVersion.language, artifact.mavenReference)
22-
})
23-
}
24-
} yield ())
25-
.unsafeRunSync()
26-
27-
} catch {
28-
case e: Throwable =>
29-
logger.info("failed to migrate the database")
30-
throw new Exception(s"failed to migrate the database because of ${e.getMessage}")
31-
}
11+
class V9__fix_platform_and_language extends FlywayMigration {
12+
override def migrationIO: IO[Unit] =
13+
for {
14+
artifactToFix <- run(selectArtifact.to[Seq])
15+
artifactToFixWithIds = artifactToFix.flatMap(a => Artifact.ArtifactId.parse(a.artifactId).map(a -> _))
16+
_ <- run {
17+
updatePlatformAndLanguage.updateMany(artifactToFixWithIds.map {
18+
case (artifact, id) => (id.binaryVersion.platform, id.binaryVersion.language, artifact.mavenReference)
19+
})
20+
}
21+
} yield ()
3222

3323
val selectArtifact: Query0[Artifact] =
3424
selectRequest("artifacts", Seq("*"), where = Seq("language_version = 'Java'", "version ~ '^[^.]*$'"))

Diff for: modules/infra/src/main/scala/scaladex/infra/sql/DoobieUtils.scala

+2-5
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,11 @@ object DoobieUtils {
4343
private implicit val cs: ContextShift[IO] =
4444
IO.contextShift(ExecutionContext.global)
4545

46-
def flyway(conf: PostgreSQLConfig): Flyway = {
47-
val datasource = getHikariDataSource(conf)
48-
flyway(datasource)
49-
}
50-
def flyway(datasource: HikariDataSource): Flyway =
46+
def flyway(datasource: HikariDataSource, testMode: Boolean): Flyway =
5147
Flyway
5248
.configure()
5349
.dataSource(datasource)
50+
.cleanDisabled(!testMode)
5451
.locations("migrations", "scaladex/infra/migrations")
5552
.load()
5653

Diff for: modules/infra/src/test/scala/scaladex/infra/BaseDatabaseSuite.scala

-57
This file was deleted.

0 commit comments

Comments
 (0)