From cb4d47927c4d6f1c02e4fcf95a52e5ce074d2724 Mon Sep 17 00:00:00 2001 From: Shailesh Patil <53746241+mineme0110@users.noreply.github.com> Date: Fri, 21 Jun 2024 17:22:38 +0100 Subject: [PATCH] fix: migrate and repair in case of renaming issues (#1211) Signed-off-by: mineme0110 --- .../identus/agent/server/MainApp.scala | 6 +++--- .../identus/agent/server/sql/Migrations.scala | 19 +++++++++++++++++++ .../connect/sql/repository/Migrations.scala | 19 +++++++++++++++++++ .../pollux/sql/repository/Migrations.scala | 19 +++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/MainApp.scala b/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/MainApp.scala index f5063ddd06..a2a69e8d9e 100644 --- a/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/MainApp.scala +++ b/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/MainApp.scala @@ -100,9 +100,9 @@ object MainApp extends ZIOAppDefault { } yield () private val migrations = for { - _ <- ZIO.serviceWithZIO[PolluxMigrations](_.migrate) - _ <- ZIO.serviceWithZIO[ConnectMigrations](_.migrate) - _ <- ZIO.serviceWithZIO[AgentMigrations](_.migrate) + _ <- ZIO.serviceWithZIO[PolluxMigrations](_.migrateAndRepair) + _ <- ZIO.serviceWithZIO[ConnectMigrations](_.migrateAndRepair) + _ <- ZIO.serviceWithZIO[AgentMigrations](_.migrateAndRepair) _ <- ZIO.logInfo("Running post-migration RLS checks for DB application users") _ <- PolluxMigrations.validateRLS.provide(RepoModule.polluxContextAwareTransactorLayer) _ <- ConnectMigrations.validateRLS.provide(RepoModule.connectContextAwareTransactorLayer) diff --git a/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/sql/Migrations.scala b/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/sql/Migrations.scala index 725d9b984f..730015d086 100644 --- a/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/sql/Migrations.scala +++ b/cloud-agent/service/server/src/main/scala/org/hyperledger/identus/agent/server/sql/Migrations.scala @@ -3,6 +3,7 @@ package org.hyperledger.identus.agent.server.sql import doobie.* import doobie.implicits.* import doobie.util.transactor.Transactor +import org.flywaydb.core.api.exception.FlywayValidateException import org.flywaydb.core.Flyway import org.hyperledger.identus.shared.db.{ContextAwareTask, DbConfig} import org.hyperledger.identus.shared.db.Implicits.* @@ -30,6 +31,24 @@ final case class Migrations(config: DbConfig) { } } yield () + def repair: Task[Unit] = + for { + _ <- ZIO.logInfo("Repairing Flyway schema history") + _ <- ZIO.attempt { + Flyway + .configure() + .dataSource(config.jdbcUrl, config.username, config.password) + .locations(migrationScriptsLocation) + .load() + .repair() + } + } yield () + + def migrateAndRepair: Task[Unit] = + migrate.catchSome { case e: FlywayValidateException => + ZIO.logError("Migration validation failed, attempting to repair") *> repair *> migrate + } + } object Migrations { diff --git a/connect/sql-doobie/src/main/scala/org/hyperledger/identus/connect/sql/repository/Migrations.scala b/connect/sql-doobie/src/main/scala/org/hyperledger/identus/connect/sql/repository/Migrations.scala index 68755a3fef..32a9acd094 100644 --- a/connect/sql-doobie/src/main/scala/org/hyperledger/identus/connect/sql/repository/Migrations.scala +++ b/connect/sql-doobie/src/main/scala/org/hyperledger/identus/connect/sql/repository/Migrations.scala @@ -3,6 +3,7 @@ package org.hyperledger.identus.connect.sql.repository import doobie.* import doobie.implicits.* import doobie.util.transactor.Transactor +import org.flywaydb.core.api.exception.FlywayValidateException import org.flywaydb.core.Flyway import org.hyperledger.identus.shared.db.{ContextAwareTask, DbConfig} import org.hyperledger.identus.shared.db.Implicits.* @@ -30,6 +31,24 @@ final case class Migrations(config: DbConfig) { } } yield () + def repair: Task[Unit] = + for { + _ <- ZIO.logInfo("Repairing Flyway schema history") + _ <- ZIO.attempt { + Flyway + .configure() + .dataSource(config.jdbcUrl, config.username, config.password) + .locations(migrationScriptsLocation) + .load() + .repair() + } + } yield () + + def migrateAndRepair: Task[Unit] = + migrate.catchSome { case e: FlywayValidateException => + ZIO.logError("Migration validation failed, attempting to repair") *> repair *> migrate + } + } object Migrations { diff --git a/pollux/sql-doobie/src/main/scala/org/hyperledger/identus/pollux/sql/repository/Migrations.scala b/pollux/sql-doobie/src/main/scala/org/hyperledger/identus/pollux/sql/repository/Migrations.scala index 8b74727aa0..f4c29749cb 100644 --- a/pollux/sql-doobie/src/main/scala/org/hyperledger/identus/pollux/sql/repository/Migrations.scala +++ b/pollux/sql-doobie/src/main/scala/org/hyperledger/identus/pollux/sql/repository/Migrations.scala @@ -3,6 +3,7 @@ package org.hyperledger.identus.pollux.sql.repository import doobie.* import doobie.implicits.* import doobie.util.transactor.Transactor +import org.flywaydb.core.api.exception.FlywayValidateException import org.flywaydb.core.Flyway import org.hyperledger.identus.shared.db.{ContextAwareTask, DbConfig} import org.hyperledger.identus.shared.db.Implicits.* @@ -31,6 +32,24 @@ final case class Migrations(config: DbConfig) { } } yield () + def repair: Task[Unit] = + for { + _ <- ZIO.logInfo("Repairing Flyway schema history") + _ <- ZIO.attempt { + Flyway + .configure() + .dataSource(config.jdbcUrl, config.username, config.password) + .locations(migrationScriptsLocation) + .load() + .repair() + } + } yield () + + def migrateAndRepair: Task[Unit] = + migrate.catchSome { case e: FlywayValidateException => + ZIO.logError("Migration validation failed, attempting to repair") *> repair *> migrate + } + } object Migrations {