Skip to content

Commit

Permalink
feat(shared): add Flyway Migrations and expose in ZIO Layer to be use…
Browse files Browse the repository at this point in the history
…d in consuming service (#115)

* feat(shared): add Flyway Migrations and expose in ZIO Layer to be used in consuming service

Previously, migrations were managed by the infrastructure layer and needed to be composed at docker image build stage.
This change allows a consumer of the pollux and castor library to execute migrations in the application layer, by composing a
db config layer with the provided miggrations layer and calling the `migrate` method on the output.

Implements ATL-2174

* chore(shared): run scalafmt on all castor/lib and pollux/lib
  • Loading branch information
davidpoltorak-io committed Nov 14, 2022
1 parent b0ab7d0 commit cd11493
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 23 deletions.
5 changes: 4 additions & 1 deletion castor/lib/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object Dependencies {
val iris = "0.1.0"
val prismSdk = "v1.4.1"
val shared = "0.1.0"
val flyway = "9.7.0"
}

private lazy val zio = "dev.zio" %% "zio" % Versions.zio
Expand All @@ -20,6 +21,8 @@ object Dependencies {
private lazy val doobiePostgres = "org.tpolecat" %% "doobie-postgres" % Versions.doobie
private lazy val doobieHikari = "org.tpolecat" %% "doobie-hikari" % Versions.doobie

private lazy val flyway = "org.flywaydb" % "flyway-core" % Versions.flyway

private lazy val shared = "io.iohk.atala" % "shared" % Versions.shared
private lazy val irisClient = "io.iohk.atala" %% "iris-client" % Versions.iris

Expand All @@ -32,7 +35,7 @@ object Dependencies {

// Dependency Modules
private lazy val baseDependencies: Seq[ModuleID] = Seq(zio, zioTest, zioTestSbt, zioTestMagnolia, shared, prismCrypto, irisClient)
private lazy val doobieDependencies: Seq[ModuleID] = Seq(doobiePostgres, doobieHikari)
private lazy val doobieDependencies: Seq[ModuleID] = Seq(doobiePostgres, doobieHikari, flyway)

// Project Dependencies
lazy val coreDependencies: Seq[ModuleID] = baseDependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.iohk.atala.castor.sql.repository

import org.flywaydb.core.Flyway
import zio.*

import javax.sql.DataSource

final case class Migrations(config: DbConfig) {

val migrationScriptsLocation: String = "sql/castor"

def migrate: Task[Unit] =
ZIO.logInfo("Applying database migrations")
for {
_ <- ZIO.attempt {
Flyway
.configure()
.dataSource(
config.jdbcUrl,
config.username,
config.password
)
.locations(migrationScriptsLocation)
.load()
.migrate()
}
} yield ()

}

object Migrations {
val layer: URLayer[DbConfig, Migrations] =
ZLayer.fromFunction(Migrations.apply _)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.iohk.atala.castor.sql.repository

import cats.effect.{Async, Resource}
import cats.syntax.functor._
import cats.Functor
import doobie.util.transactor.Transactor
import com.zaxxer.hikari.HikariConfig
import doobie.util.ExecutionContexts
Expand All @@ -9,14 +11,14 @@ import zio.interop.catz.*
import zio.*
import cats.effect.std.Dispatcher

object TransactorLayer {
case class DbConfig(
username: String,
password: String,
jdbcUrl: String,
awaitConnectionThreads: Int = 8
)

case class DbConfig(
username: String,
password: String,
jdbcUrl: String,
awaitConnectionThreads: Int = 8
)
object TransactorLayer {

def hikari[A[_]: Async: Dispatcher](config: DbConfig)(using tag: Tag[Transactor[A]]): TaskLayer[Transactor[A]] = {
val transactorLayerZio = ZIO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.iohk.atala.pollux.core.model.error


sealed trait PublishCredentialBatchError

object PublishCredentialBatchError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import io.iohk.atala.mercury.protocol.issuecredential.RequestCredential
import io.iohk.atala.mercury.protocol.issuecredential.IssueCredential
trait CredentialRepository[F[_]] {
def createIssueCredentialRecord(record: IssueCredentialRecord): F[Int]

def getIssueCredentialRecords(): F[Seq[IssueCredentialRecord]]

def getIssueCredentialRecord(id: UUID): F[Option[IssueCredentialRecord]]

def getIssueCredentialRecordByThreadId(id: UUID): F[Option[IssueCredentialRecord]]

def updateCredentialRecordProtocolState(
id: UUID,
from: IssueCredentialRecord.ProtocolState,
Expand All @@ -25,8 +25,8 @@ trait CredentialRepository[F[_]] {
from: Option[IssueCredentialRecord.PublicationState],
to: Option[IssueCredentialRecord.PublicationState]
): F[Int]

def updateWithRequestCredential(request: RequestCredential): F[Int]

def updateWithIssueCredential(issue: IssueCredential): F[Int]
}
5 changes: 4 additions & 1 deletion pollux/lib/project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object Dependencies {
val prismSdk = "v1.3.3-snapshot-1657194253-992dd96"
val iris = "0.1.0"
val mercury = "0.4.0"
val flyway = "9.7.0"
}

private lazy val zio = "dev.zio" %% "zio" % Versions.zio
Expand All @@ -16,6 +17,8 @@ object Dependencies {
private lazy val doobiePostgres = "org.tpolecat" %% "doobie-postgres" % Versions.doobie
private lazy val doobieHikari = "org.tpolecat" %% "doobie-hikari" % Versions.doobie

private lazy val flyway = "org.flywaydb" % "flyway-core" % Versions.flyway

// We have to exclude bouncycastle since for some reason bitcoinj depends on bouncycastle jdk15to18
// (i.e. JDK 1.5 to 1.8), but we are using JDK 11
private lazy val prismCrypto = "io.iohk.atala" % "prism-crypto-jvm" % Versions.prismSdk excludeAll
Expand All @@ -32,7 +35,7 @@ object Dependencies {

// Dependency Modules
private lazy val baseDependencies: Seq[ModuleID] = Seq(zio, prismCrypto)
private lazy val doobieDependencies: Seq[ModuleID] = Seq(doobiePostgres, doobieHikari)
private lazy val doobieDependencies: Seq[ModuleID] = Seq(doobiePostgres, doobieHikari, flyway)
private lazy val mercuryDependencies: Seq[ModuleID] = Seq(mercuryModels, mercuryAgent, mercuryResolver)

// Project Dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.iohk.atala.pollux.sql.repository

import org.flywaydb.core.Flyway
import zio.*

import javax.sql.DataSource

final case class Migrations(config: DbConfig) {

val migrationScriptsLocation: String = "sql/pollux"

def migrate: Task[Unit] =
ZIO.logInfo("Applying database migrations")
for {
_ <- ZIO.attempt {
Flyway
.configure()
.dataSource(
config.jdbcUrl,
config.username,
config.password
)
.locations(migrationScriptsLocation)
.load()
.migrate()
}
} yield ()

}

object Migrations {
val layer: URLayer[DbConfig, Migrations] =
ZLayer.fromFunction(Migrations.apply _)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import zio.interop.catz.*
import zio.*
import cats.effect.std.Dispatcher

object TransactorLayer {
case class DbConfig(
username: String,
password: String,
jdbcUrl: String,
awaitConnectionThreads: Int = 8
)

case class DbConfig(
username: String,
password: String,
jdbcUrl: String,
awaitConnectionThreads: Int = 8
)
object TransactorLayer {

def hikari[A[_]: Async: Dispatcher](config: DbConfig)(using tag: Tag[Transactor[A]]): TaskLayer[Transactor[A]] = {
val transactorLayerZio = ZIO
Expand Down

0 comments on commit cd11493

Please sign in to comment.