Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Flyway to v6 + support all Flyway configuration options #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,22 @@ 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
}

val migrationInfos = Try {
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())
}
}

Expand Down