• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hyperledger / identus-cloud-agent / 10793991050

10 Sep 2024 01:56PM CUT coverage: 48.504% (-4.5%) from 52.962%
10793991050

push

web-flow
build: sbt and plugins dependency update (#1337)

Signed-off-by: Hyperledger Bot <hyperledger-bot@hyperledger.org>
Signed-off-by: Yurii Shynbuiev <yurii.shynbuiev@iohk.io>
Co-authored-by: Hyperledger Bot <hyperledger-bot@hyperledger.org>
Co-authored-by: Yurii Shynbuiev <yurii.shynbuiev@iohk.io>

7406 of 15269 relevant lines covered (48.5%)

0.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/sql/Migrations.scala
1
package org.hyperledger.identus.agent.server.sql
2

3
import doobie.*
4
import doobie.implicits.*
5
import doobie.util.transactor.Transactor
6
import org.flywaydb.core.api.exception.FlywayValidateException
7
import org.flywaydb.core.Flyway
8
import org.hyperledger.identus.shared.db.{ContextAwareTask, DbConfig}
9
import org.hyperledger.identus.shared.db.Implicits.*
10
import zio.*
11
import zio.interop.catz.*
12

13
final case class Migrations(config: DbConfig) {
14

15
  val migrationScriptsLocation: String = "sql/agent"
16

17
  def migrate: Task[Unit] =
×
18
    for {
×
19
      _ <- ZIO.logInfo("Applying database migrations")
×
20
      _ <- ZIO.attempt {
×
21
        Flyway
22
          .configure()
×
23
          .dataSource(
×
24
            config.jdbcUrl,
25
            config.username,
26
            config.password
27
          )
28
          .locations(migrationScriptsLocation)
×
29
          .load()
×
30
          .migrate()
×
31
      }
32
    } yield ()
×
33

34
  def repair: Task[Unit] =
×
35
    for {
×
36
      _ <- ZIO.logInfo("Repairing Flyway schema history")
×
37
      _ <- ZIO.attempt {
×
38
        Flyway
39
          .configure()
×
40
          .dataSource(config.jdbcUrl, config.username, config.password)
×
41
          .locations(migrationScriptsLocation)
×
42
          .load()
×
43
          .repair()
×
44
      }
45
    } yield ()
×
46

47
  def migrateAndRepair: Task[Unit] =
×
48
    migrate.catchSome { case e: FlywayValidateException =>
×
49
      ZIO.logError("Migration validation failed, attempting to repair") *> repair *> migrate
×
50
    }
51

52
}
53

54
object Migrations {
55
  val layer: URLayer[DbConfig, Migrations] =
56
    ZLayer.fromFunction(Migrations.apply)
×
57

58
  /** Fail if the RLS is not enabled from a sample table */
59
  def validateRLS: RIO[Transactor[ContextAwareTask], Unit] = {
×
60
    val cxnIO = sql"""
×
61
      | SELECT row_security_active('public.peer_did');
62
      """.stripMargin
×
63
      .query[Boolean]
64
      .unique
×
65

66
    for {
×
67
      xa <- ZIO.service[Transactor[ContextAwareTask]]
×
68
      isRlsActive <- cxnIO.transactWithoutContext(xa)
×
69
      _ <- ZIO
×
70
        .fail(Exception("The RLS policy is not active for Agent DB application user"))
×
71
        .unless(isRlsActive)
72
    } yield ()
×
73
  }
74

75
  def initDbPrivileges(appUser: String): RIO[Transactor[Task], Unit] = {
×
76
    val cxnIO = for {
×
77
      _ <- doobie.free.connection.createStatement.map { stm =>
×
78
        stm.execute(s"""
×
79
          | ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO "$appUser"
80
          """.stripMargin)
×
81
      }
82
    } yield ()
×
83

84
    for {
×
85
      xa <- ZIO.service[Transactor[Task]]
×
86
      _ <- cxnIO.transact(xa)
×
87
    } yield ()
×
88
  }
89
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc