All Downloads are FREE. Search and download functionalities are using the official Maven repository.

storage.Migrate.scala Maven / Gradle / Ivy

The newest version!
package nelson
package storage


import org.flywaydb.core.Flyway
import journal.Logger
import scalaz.concurrent.Task


object Migrate {

  val log = Logger[Migrate.type]

  def migrate(cfg: DatabaseConfig): Task[Unit] =
    Task.delay {
      val flyway = new Flyway
      flyway.setDataSource(
        cfg.connection,
        cfg.username.getOrElse(""),
        cfg.password.getOrElse(""))

      try {
        log.info("Conducting database schema migrations if needed.")
        val completed = flyway.migrate()
        log.info(s"Completed $completed succsessful migrations.")
      } catch {
        case e: Throwable => {
          // attempt a repair (useful for local debugging)
          log.error(s"Failed to migrate database. ${e.getMessage}")
          log.info("Repairing database before retrying migration")
          flyway.repair()
          val completed = flyway.migrate()
          log.info(s"After repair, completed $completed succsessful migrations.")
        }
      }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy