diff --git a/README.md b/README.md index dbb8bc3..1f40dde 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ This Lambda function is supporting 2 migration methods. 1. Automatic migration by put SQL file into S3. 1. Manual migration by invoking yourself. (Since 0.2.0) +Currently, this project uses **Flyway 6**. + # Setup ## S3 bucket @@ -30,22 +32,16 @@ s3://my-flyway <- Flyway migration bucket. ### Flyway configuration -create Flyway configuration file named `flyway.conf` in resource folder. +Create a Flyway configuration file named `flyway.conf` in the resource folder. +The file is fed directly to Flyway so all [configuration options listed on the docs page](https://flywaydb.org/documentation/configfiles) are supported. +At the very least, these options must be provided: ``` flyway.url = jdbc:mysql://RDS_ENDPOINT/DATABSE_NAME flyway.user = USER_NAME flyway.password = PASSWORD ``` -#### Supported options. - -See [Flyway - Config Files](https://flywaydb.org/documentation/configfiles) for option details. - -* `flyway.outOfOrder` -* `flyway.schemas` -* `flyway.cleanOnValidationError` - ## AWS Settings ### VPC Endpoint diff --git a/build.sbt b/build.sbt index 25868b8..e4a64c7 100644 --- a/build.sbt +++ b/build.sbt @@ -10,7 +10,7 @@ lazy val flywayAwsLambda = (project in file(".")).settings( libraryDependencies ++= Seq( // Flyway - "org.flywaydb" % "flyway-core" % "4.0.3", + "org.flywaydb" % "flyway-core" % "6.0.4", "mysql" % "mysql-connector-java" % "6.0.5", // Flyway supports only Ver.6 higher. // AWS diff --git a/src/main/scala/crossroad0201/aws/flywaylambda/S3MigrationHandlerBase.scala b/src/main/scala/crossroad0201/aws/flywaylambda/S3MigrationHandlerBase.scala index 836f764..241bf9c 100644 --- a/src/main/scala/crossroad0201/aws/flywaylambda/S3MigrationHandlerBase.scala +++ b/src/main/scala/crossroad0201/aws/flywaylambda/S3MigrationHandlerBase.scala @@ -49,11 +49,11 @@ trait S3MigrationHandlerBase extends FlywayMigrator { _ = { logger.log( s"""--- Flyway configuration ------------------------------------ - |flyway.url = ${d.url} + |flyway.url = ${d.flywayConfig.getDataSource.getConnection.getMetaData.getURL} |flyway.user = **** |flyway.password = **** | - |SQL locations = ${d.location} + |SQL locations = ${d.flywayConfig.getLocations.mkString(", ")} |SQL files = ${d.sqlFiles.mkString(", ")} |------------------------------------------------------------- """.stripMargin) diff --git a/src/main/scala/crossroad0201/aws/flywaylambda/deploy/FlywayDeployment.scala b/src/main/scala/crossroad0201/aws/flywaylambda/deploy/FlywayDeployment.scala index 5ac201a..ada1d32 100644 --- a/src/main/scala/crossroad0201/aws/flywaylambda/deploy/FlywayDeployment.scala +++ b/src/main/scala/crossroad0201/aws/flywaylambda/deploy/FlywayDeployment.scala @@ -1,66 +1,27 @@ package crossroad0201.aws.flywaylambda.deploy import java.nio.file.Path -import java.util.{ Properties => JProperties } +import java.util.{Properties => JProperties} import org.flywaydb.core.Flyway +import org.flywaydb.core.api.configuration.Configuration case class FlywayDeployment( sourceBucket: String, - sourcePrefix:String, - url: String, - user: String, - password: String, - location: String, + sourcePrefix: String, sqlFiles: Seq[Path], - options: Seq[FlywayOption] + flywayConfig: Configuration ) + object FlywayDeployment { def apply(sourceBucket: String, sourcePrefix:String, conf: JProperties, location: String, sqlFiles: Seq[Path]): FlywayDeployment = { FlywayDeployment( sourceBucket, sourcePrefix, - conf.getProperty("flyway.url"), - conf.getProperty("flyway.user"), - conf.getProperty("flyway.password"), - location, sqlFiles, - FlywayOption.buildOptions(conf) + Flyway.configure() + .configuration(conf) + .locations(location) ) } } - -sealed trait FlywayOption { - def apply(flyway: Flyway): Flyway -} -object FlywayOption { - def buildOptions(conf: JProperties): Seq[FlywayOption] = { - import scala.collection.JavaConverters._ - conf.asScala.flatMap { - case ("flyway.outOfOrder", enabled) => Some(OutOfOrder(enabled.toBoolean)) - case ("flyway.cleanOnValidationError", enabled) => Some(CleanOnValidationError(enabled.toBoolean)) - case ("flyway.schemas", schemas) => Some(Schemas(schemas.split(",").map(_.trim))) - case _ => None - }.toSeq - } -} -case class OutOfOrder(enabled: Boolean) extends FlywayOption { - override def apply(flyway: Flyway) = { - flyway.setOutOfOrder(enabled) - flyway - } -} - -case class CleanOnValidationError(enabled: Boolean) extends FlywayOption { - override def apply(flyway: Flyway) = { - flyway.setCleanOnValidationError(enabled) - flyway - } -} - -case class Schemas(schemas: Array[String]) extends FlywayOption { - override def apply(flyway: Flyway) = { - flyway.setSchemas(schemas: _*) - flyway - } -} diff --git a/src/main/scala/crossroad0201/aws/flywaylambda/migration/FlywayMigrator.scala b/src/main/scala/crossroad0201/aws/flywaylambda/migration/FlywayMigrator.scala index 4380cbe..00be656 100644 --- a/src/main/scala/crossroad0201/aws/flywaylambda/migration/FlywayMigrator.scala +++ b/src/main/scala/crossroad0201/aws/flywaylambda/migration/FlywayMigrator.scala @@ -8,18 +8,9 @@ import scala.util.{Failure, Success, Try} trait FlywayMigrator { def migrate(deployment: FlywayDeployment) = { - val flyway = new Flyway + val flyway = new Flyway(deployment.flywayConfig) val appliedCount = Try { - flyway.setDataSource( - deployment.url, - deployment.user, - deployment.password - ) - flyway.setLocations(deployment.location) - - deployment.options.map(_.apply(flyway)) - flyway.migrate } @@ -27,11 +18,12 @@ trait FlywayMigrator { flyway.info.all } + val url = deployment.flywayConfig.getDataSource.getConnection.getMetaData.getURL (appliedCount, migrationInfos) match { - case (Success(c), Success(is)) => MigrationResult.success(deployment.url, c, is.map(MigrationInfo(_))) - case (Success(c), Failure(e)) => MigrationResult.failure(deployment.url, e, Seq()) - case (Failure(e), Success(is)) => MigrationResult.failure(deployment.url, e, is.map(MigrationInfo(_))) - case (Failure(e1), Failure(e2)) => MigrationResult.failure(deployment.url, e1, Seq()) + case (Success(c), Success(is)) => MigrationResult.success(url, c, is.map(MigrationInfo(_))) + case (Success(c), Failure(e)) => MigrationResult.failure(url, e, Seq()) + case (Failure(e), Success(is)) => MigrationResult.failure(url, e, is.map(MigrationInfo(_))) + case (Failure(e1), Failure(e2)) => MigrationResult.failure(url, e1, Seq()) } }