diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..31dd5d4 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,23 @@ +name: Scalafmt + +permissions: {} + +on: + pull_request: + branches: ['**'] + +jobs: + build: + name: Code is formatted + runs-on: ubuntu-latest + steps: + - name: Checkout current branch (full) + uses: actions/checkout@v4 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Check project is formatted + uses: jrouly/scalafmt-native-action@v4 + with: + arguments: '--list' diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..cb37ed5 --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,28 @@ +version = 3.8.3 +runner.dialect = scala212 +maxColumn = 120 +project.git = true + +# http://docs.scala-lang.org/style/scaladoc.html recommends the JavaDoc style. +# scala/scala is written that way too https://github.com/scala/scala/blob/v2.12.2/src/library/scala/Predef.scala +docstrings.style = Asterisk + +# This also seems more idiomatic to include whitespace in import x.{ yyy } +spaces.inImportCurlyBraces = true + +rewrite.trailingCommas.style = keep + +align.tokens."+" = [ + { + code = "%" + owners = [ + { regex = "Term.ApplyInfix" } + ] + }, + { + code = "%%" + owners = [ + { regex = "Term.ApplyInfix" } + ] + } +] diff --git a/build.sbt b/build.sbt index a9c4120..30b56b4 100644 --- a/build.sbt +++ b/build.sbt @@ -8,42 +8,51 @@ ThisBuild / version := { "9.0.0-SNAPSHOT" } else orig } -lazy val root = (project in file (".")) - .enablePlugins(SbtPlugin) - .settings( - name := "flyway-sbt", - libraryDependencies ++= Seq( - "org.flywaydb" % "flyway-core" % flywayVersion - ), - scalacOptions ++= Seq( - "-deprecation", - "-unchecked", - "-Xfuture" - ), - pluginCrossBuild / sbtVersion := { - scalaBinaryVersion.value match { - case "2.12" => "1.5.8" - case _ => "2.0.0-M2" - } - }, - Compile / doc / scalacOptions ++= { - Seq( - "-sourcepath", - (LocalRootProject / baseDirectory).value.getAbsolutePath, - "-doc-source-url", - s"""https://github.com/sbt/flyway-sbt/tree/${sys.process.Process("git rev-parse HEAD").lineStream_!.head}€{FILE_PATH}.scala""" - ) - }, - scriptedLaunchOpts := { scriptedLaunchOpts.value ++ +lazy val root = (project in file(".")) + .enablePlugins(SbtPlugin) + .settings( + name := "flyway-sbt", + libraryDependencies ++= Seq( + "org.flywaydb" % "flyway-core" % flywayVersion + ), + scalacOptions ++= Seq( + "-deprecation", + "-unchecked", + "-Xfuture" + ), + pluginCrossBuild / sbtVersion := { + scalaBinaryVersion.value match { + case "2.12" => "1.5.8" + case _ => "2.0.0-M2" + } + }, + Compile / doc / scalacOptions ++= { + Seq( + "-sourcepath", + (LocalRootProject / baseDirectory).value.getAbsolutePath, + "-doc-source-url", + s"""https://github.com/sbt/flyway-sbt/tree/${sys.process + .Process("git rev-parse HEAD") + .lineStream_! + .head}€{FILE_PATH}.scala""" + ) + }, + scriptedLaunchOpts := { + scriptedLaunchOpts.value ++ Seq("-Xmx1024M", "-Dplugin.version=" + version.value) - }, - scriptedBufferLog := false, - publishMavenStyle := true, + }, + scriptedBufferLog := false, + publishMavenStyle := true, ) ThisBuild / description := "An sbt plugin for Flyway database migration" ThisBuild / developers := List( - Developer(id="davidmweber", name="David Weber", email="dave@veryflatcat.com", url=url("https://davidmweber.github.io/flyway-sbt-docs/")) + Developer( + id = "davidmweber", + name = "David Weber", + email = "dave@veryflatcat.com", + url = url("https://davidmweber.github.io/flyway-sbt-docs/") + ) ) ThisBuild / licenses := Seq("Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0.txt")) ThisBuild / scmInfo := Some( diff --git a/project/plugin.sbt b/project/plugin.sbt index b00e953..2e0a7a2 100644 --- a/project/plugin.sbt +++ b/project/plugin.sbt @@ -1,2 +1,3 @@ addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.9.0") addSbtPlugin("com.github.sbt" % "sbt-github-actions" % "0.24.0") +addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2") diff --git a/src/main/scala/flywaysbt/FlywayPlugin.scala b/src/main/scala/flywaysbt/FlywayPlugin.scala index 6230426..2d3c6dd 100644 --- a/src/main/scala/flywaysbt/FlywayPlugin.scala +++ b/src/main/scala/flywaysbt/FlywayPlugin.scala @@ -1,17 +1,14 @@ /** * Copyright 2010-2017 Boxfuse GmbH * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. */ package flywaysbt @@ -19,7 +16,7 @@ package flywaysbt import java.util.Properties import org.flywaydb.core.Flyway import org.flywaydb.core.api.callback.Callback -import org.flywaydb.core.api.logging.{Log, LogCreator, LogFactory} +import org.flywaydb.core.api.logging.{ Log, LogCreator, LogFactory } import org.flywaydb.core.internal.info.MigrationInfoDumper import sbt.Keys.* import sbt.* @@ -35,83 +32,164 @@ object FlywayPlugin extends AutoPlugin { object autoImport { - //********************* + // ********************* // common migration settings for all tasks - //********************* - val flywayDriver = settingKey[String]("The fully qualified classname of the jdbc driver to use to connect to the database. By default, the driver is autodetected based on the url.") + // ********************* + val flywayDriver = settingKey[String]( + "The fully qualified classname of the jdbc driver to use to connect to the database. By default, the driver is autodetected based on the url." + ) val flywayUrl = settingKey[String]("The jdbc url to use to connect to the database.") val flywayUser = settingKey[String]("The user to use to connect to the database.") val flywayPassword = settingKey[String]("The password to use to connect to the database.") - val flywaySchemas = settingKey[Seq[String]]("List of the schemas managed by Flyway. The first schema in the list will be automatically set as the default one during the migration. It will also be the one containing the metadata table. These schema names are case-sensitive. (default: The default schema for the datasource connection)") - val flywayTable = settingKey[String]("The name of the metadata table that will be used by Flyway. (default: schema_version) By default (single-schema mode) the metadata table is placed in the default schema for the connection provided by the datasource. When the flyway.schemas property is set (multi-schema mode), the metadata table is placed in the first schema of the list.") - val flywayBaselineVersion = settingKey[String]("The version to tag an existing schema with when executing baseline. (default: 1)") - val flywayBaselineDescription = settingKey[String]("The description to tag an existing schema with when executing baseline. (default: << Flyway Baseline >>)") + val flywaySchemas = settingKey[Seq[String]]( + "List of the schemas managed by Flyway. The first schema in the list will be automatically set as the default one during the migration. It will also be the one containing the metadata table. These schema names are case-sensitive. (default: The default schema for the datasource connection)" + ) + val flywayTable = settingKey[String]( + "The name of the metadata table that will be used by Flyway. (default: schema_version) By default (single-schema mode) the metadata table is placed in the default schema for the connection provided by the datasource. When the flyway.schemas property is set (multi-schema mode), the metadata table is placed in the first schema of the list." + ) + val flywayBaselineVersion = + settingKey[String]("The version to tag an existing schema with when executing baseline. (default: 1)") + val flywayBaselineDescription = settingKey[String]( + "The description to tag an existing schema with when executing baseline. (default: << Flyway Baseline >>)" + ) - //********************* + // ********************* // common settings for migration loading tasks (used by migrate, validate, info) - //********************* - val flywayLocations = settingKey[Seq[String]]("Locations on the classpath to scan recursively for migrations. Locations may contain both sql and code-based migrations. (default: classpath:db/migration)") - val flywayResolvers = settingKey[Seq[String]](" The fully qualified class names of the custom MigrationResolvers to be used in addition to the built-in ones for resolving Migrations to apply.") - val flywaySkipDefaultResolvers = settingKey[Boolean]("Whether default built-in resolvers should be skipped. (default: false)") + // ********************* + val flywayLocations = settingKey[Seq[String]]( + "Locations on the classpath to scan recursively for migrations. Locations may contain both sql and code-based migrations. (default: classpath:db/migration)" + ) + val flywayResolvers = settingKey[Seq[String]]( + " The fully qualified class names of the custom MigrationResolvers to be used in addition to the built-in ones for resolving Migrations to apply." + ) + val flywaySkipDefaultResolvers = + settingKey[Boolean]("Whether default built-in resolvers should be skipped. (default: false)") val flywayEncoding = settingKey[String]("The encoding of Sql migrations. (default: UTF-8)") val flywaySqlMigrationPrefix = settingKey[String]("The file name prefix for Sql migrations (default: V)") - val flywayRepeatableSqlMigrationPrefix = settingKey[String]("The file name prefix for repeatable sql migrations (default: R)") + val flywayRepeatableSqlMigrationPrefix = + settingKey[String]("The file name prefix for repeatable sql migrations (default: R)") val flywaySqlMigrationSeparator = settingKey[String]("The file name separator for Sql migrations (default: __)") - val flywaySqlMigrationSuffixes = settingKey[Seq[String]]("The file name suffixes for Sql migrations (default: .sql)") - val flywayCleanOnValidationError = settingKey[Boolean]("Whether to automatically call clean or not when a validation error occurs. (default: {@code false})
This is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM. Warning ! Do not enable in production !") - val flywayCleanDisabled = settingKey[Boolean]("Whether to disable clean. This is especially useful for production environments where running clean can be quite a career limiting move. (default: true)") - val flywayTarget = settingKey[String]("The target version up to which Flyway should run migrations. Migrations with a higher version number will not be applied. (default: the latest version)") - val flywayOutOfOrder = settingKey[Boolean]("Allows migrations to be run \"out of order\" (default: {@code false}). If you already have versions 1 and 3 applied, and now a version 2 is found, it will be applied too instead of being ignored.") - val flywayCallbacks = settingKey[Seq[Callback]]("A list of callbacks that will be used for Flyway lifecycle notifications. (default: Empty)") - val flywaySkipDefaultCallbacks = settingKey[Boolean]("Whether default built-in callbacks should be skipped. (default: false)") + val flywaySqlMigrationSuffixes = + settingKey[Seq[String]]("The file name suffixes for Sql migrations (default: .sql)") + val flywayCleanOnValidationError = settingKey[Boolean]( + "Whether to automatically call clean or not when a validation error occurs. (default: {@code false})
This is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM. Warning ! Do not enable in production !" + ) + val flywayCleanDisabled = settingKey[Boolean]( + "Whether to disable clean. This is especially useful for production environments where running clean can be quite a career limiting move. (default: true)" + ) + val flywayTarget = settingKey[String]( + "The target version up to which Flyway should run migrations. Migrations with a higher version number will not be applied. (default: the latest version)" + ) + val flywayOutOfOrder = settingKey[Boolean]( + "Allows migrations to be run \"out of order\" (default: {@code false}). If you already have versions 1 and 3 applied, and now a version 2 is found, it will be applied too instead of being ignored." + ) + val flywayCallbacks = settingKey[Seq[Callback]]( + "A list of callbacks that will be used for Flyway lifecycle notifications. (default: Empty)" + ) + val flywaySkipDefaultCallbacks = + settingKey[Boolean]("Whether default built-in callbacks should be skipped. (default: false)") - //********************* + // ********************* // settings for migrate - //********************* - val flywayIgnoreMissingMigrations = settingKey[Boolean]("Ignores missing migrations when reading the metadata table. (default: false)") - val flywayIgnoreFutureMigrations = settingKey[Boolean]("Ignores future migrations when reading the metadata table. These are migrations that were performed by a newer deployment of the application that are not yet available in this version. For example: we have migrations available on the classpath up to version 3.0. The metadata table indicates that a migration to version 4.0 (unknown to us) has already been applied. Instead of bombing out (fail fast) with an exception, a warning is logged and Flyway continues normally. This is useful for situations where one must be able to redeploy an older version of the application after the database has been migrated by a newer one. (default: true)") - val flywayIgnoreFailedFutureMigration = settingKey[Boolean]("Ignores failed future migrations when reading the metadata table. These are migrations that we performed by a newer deployment of the application that are not yet available in this version. For example: we have migrations available on the classpath up to version 3.0. The metadata table indicates that a migration to version 4.0 (unknown to us) has already been attempted and failed. Instead of bombing out (fail fast) with an exception, a warning is logged and Flyway terminates normally. This is useful for situations where a database rollback is not an option. An older version of the application can then be redeployed, even though a newer one failed due to a bad migration. (default: false)") + // ********************* + val flywayIgnoreMissingMigrations = + settingKey[Boolean]("Ignores missing migrations when reading the metadata table. (default: false)") + val flywayIgnoreFutureMigrations = settingKey[Boolean]( + "Ignores future migrations when reading the metadata table. These are migrations that were performed by a newer deployment of the application that are not yet available in this version. For example: we have migrations available on the classpath up to version 3.0. The metadata table indicates that a migration to version 4.0 (unknown to us) has already been applied. Instead of bombing out (fail fast) with an exception, a warning is logged and Flyway continues normally. This is useful for situations where one must be able to redeploy an older version of the application after the database has been migrated by a newer one. (default: true)" + ) + val flywayIgnoreFailedFutureMigration = settingKey[Boolean]( + "Ignores failed future migrations when reading the metadata table. These are migrations that we performed by a newer deployment of the application that are not yet available in this version. For example: we have migrations available on the classpath up to version 3.0. The metadata table indicates that a migration to version 4.0 (unknown to us) has already been attempted and failed. Instead of bombing out (fail fast) with an exception, a warning is logged and Flyway terminates normally. This is useful for situations where a database rollback is not an option. An older version of the application can then be redeployed, even though a newer one failed due to a bad migration. (default: false)" + ) val flywayPlaceholderReplacement = settingKey[Boolean]("Whether placeholders should be replaced. (default: true)") - val flywayPlaceholders = settingKey[Map[String, String]]("A map of to apply to sql migration scripts.") + val flywayPlaceholders = + settingKey[Map[String, String]]("A map of to apply to sql migration scripts.") val flywayPlaceholderPrefix = settingKey[String]("The prefix of every placeholder. (default: ${ )") val flywayPlaceholderSuffix = settingKey[String]("The suffix of every placeholder. (default: } )") - val flywayBaselineOnMigrate = settingKey[Boolean]("Whether to automatically call baseline when migrate is executed against a non-empty schema with no metadata table. This schema will then be baselined with the {@code baselineVersion} before executing the migrations. Only migrations above {@code baselineVersion} will then be applied. This is useful for initial Flyway production deployments on projects with an existing DB. Be careful when enabling this as it removes the safety net that ensures Flyway does not migrate the wrong database in case of a configuration mistake! (default: {@code false})") - val flywayValidateOnMigrate = settingKey[Boolean]("Whether to automatically call validate or not when running migrate. (default: true)") - val flywayAllowMixedMigrations = settingKey[Boolean]("Whether to allow mixing transactional and non-transactional statements within the same migration. (default: false)") - val flywayMixed = settingKey[Boolean]("Whether to allow mixing transactional and non-transactional statements within the same migration. (default: false)") - val flywayGroup = settingKey[Boolean]("Whether to group all pending migrations together in the same transaction when applying them (only recommended for databases with support for DDL transactions). (default: false)") - val flywayInstalledBy = settingKey[String]("The username that will be recorded in the metadata table as having applied the migration. (default: null)") + val flywayBaselineOnMigrate = settingKey[Boolean]( + "Whether to automatically call baseline when migrate is executed against a non-empty schema with no metadata table. This schema will then be baselined with the {@code baselineVersion} before executing the migrations. Only migrations above {@code baselineVersion} will then be applied. This is useful for initial Flyway production deployments on projects with an existing DB. Be careful when enabling this as it removes the safety net that ensures Flyway does not migrate the wrong database in case of a configuration mistake! (default: {@code false})" + ) + val flywayValidateOnMigrate = + settingKey[Boolean]("Whether to automatically call validate or not when running migrate. (default: true)") + val flywayAllowMixedMigrations = settingKey[Boolean]( + "Whether to allow mixing transactional and non-transactional statements within the same migration. (default: false)" + ) + val flywayMixed = settingKey[Boolean]( + "Whether to allow mixing transactional and non-transactional statements within the same migration. (default: false)" + ) + val flywayGroup = settingKey[Boolean]( + "Whether to group all pending migrations together in the same transaction when applying them (only recommended for databases with support for DDL transactions). (default: false)" + ) + val flywayInstalledBy = settingKey[String]( + "The username that will be recorded in the metadata table as having applied the migration. (default: null)" + ) - //********************* + // ********************* // flyway tasks - //********************* + // ********************* val flywayMigrate = taskKey[Unit]("Migrates of the configured database to the latest version.") - val flywayValidate = taskKey[Unit]("Validate applied migrations against resolved ones (on the filesystem or classpath) to detect accidental changes that may prevent the schema(s) from being recreated exactly. Validation fails if differences in migration names, types or checksums are found, versions have been applied that aren't resolved locally anymore or versions have been resolved that haven't been applied yet") - val flywayInfo = taskKey[Unit]("Retrieves the complete information about the migrations including applied, pending and current migrations with details and status.") + val flywayValidate = taskKey[Unit]( + "Validate applied migrations against resolved ones (on the filesystem or classpath) to detect accidental changes that may prevent the schema(s) from being recreated exactly. Validation fails if differences in migration names, types or checksums are found, versions have been applied that aren't resolved locally anymore or versions have been resolved that haven't been applied yet" + ) + val flywayInfo = taskKey[Unit]( + "Retrieves the complete information about the migrations including applied, pending and current migrations with details and status." + ) val flywayClean = taskKey[Unit]("Drops all database objects.") - val flywayBaseline = taskKey[Unit]("Baselines an existing database, excluding all migrations up to and including baselineVersion.") + val flywayBaseline = + taskKey[Unit]("Baselines an existing database, excluding all migrations up to and including baselineVersion.") val flywayRepair = taskKey[Unit]("Repairs the metadata table.") - val flywayDefaults = taskKey[FluentConfiguration]("Default configuration. This task is used to help resolve classpaths properly") + val flywayDefaults = + taskKey[FluentConfiguration]("Default configuration. This task is used to help resolve classpaths properly") } - //********************* + // ********************* // convenience settings - //********************* + // ********************* private case class ConfigDataSource(driver: String, url: String, user: String, password: String) { - def asProps: Map[String, String] = (if (driver.isEmpty) { - Map() - } else { - Map("flyway.driver" -> driver) - }) ++ Map("flyway.url" -> url, "flyway.user" -> user, "flyway.password" -> password) + def asProps: Map[String, String] = (if (driver.isEmpty) { + Map() + } else { + Map("flyway.driver" -> driver) + }) ++ Map( + "flyway.url" -> url, + "flyway.user" -> user, + "flyway.password" -> password + ) } - private case class ConfigBase(schemas: Seq[String], table: String, baselineVersion: String, baselineDescription: String) - private case class ConfigMigrationLoading(locations: Seq[String], resolvers: Seq[String], skipDefaultResolvers: Boolean, encoding: String, - cleanOnValidationError: Boolean, cleanDisabled: Boolean, target: String, outOfOrder: Boolean, - callbacks: Seq[Callback], skipDefaultCallbacks: Boolean) - private case class ConfigSqlMigration(sqlMigrationPrefix: String, repeatableSqlMigrationPrefix: String, sqlMigrationSeparator: String, sqlMigrationSuffixes: String*) - private case class ConfigMigrate(ignoreMissingMigrations: Boolean, ignoreFutureMigrations: Boolean, ignoreFailedMigrations: Boolean, - baselineOnMigrate: Boolean, validateOnMigrate: Boolean, mixed: Boolean, group: Boolean, installedBy: String) { + private case class ConfigBase( + schemas: Seq[String], + table: String, + baselineVersion: String, + baselineDescription: String + ) + private case class ConfigMigrationLoading( + locations: Seq[String], + resolvers: Seq[String], + skipDefaultResolvers: Boolean, + encoding: String, + cleanOnValidationError: Boolean, + cleanDisabled: Boolean, + target: String, + outOfOrder: Boolean, + callbacks: Seq[Callback], + skipDefaultCallbacks: Boolean + ) + private case class ConfigSqlMigration( + sqlMigrationPrefix: String, + repeatableSqlMigrationPrefix: String, + sqlMigrationSeparator: String, + sqlMigrationSuffixes: String* + ) + private case class ConfigMigrate( + ignoreMissingMigrations: Boolean, + ignoreFutureMigrations: Boolean, + ignoreFailedMigrations: Boolean, + baselineOnMigrate: Boolean, + validateOnMigrate: Boolean, + mixed: Boolean, + group: Boolean, + installedBy: String + ) { def ignorePatterns: Seq[ValidatePattern] = { var patterns = Seq.empty[String] if (ignoreMissingMigrations) patterns = patterns :+ "*:missing" @@ -119,27 +197,38 @@ object FlywayPlugin extends AutoPlugin { patterns.map(ValidatePattern.fromPattern) } } - private case class ConfigPlaceholder(placeholderReplacement: Boolean, placeholders: Map[String, String], - placeholderPrefix: String, placeholderSuffix: String) - private case class Config(dataSource: ConfigDataSource, base: ConfigBase, migrationLoading: ConfigMigrationLoading, - sqlMigration: ConfigSqlMigration, migrate: ConfigMigrate, placeholder: ConfigPlaceholder) - + private case class ConfigPlaceholder( + placeholderReplacement: Boolean, + placeholders: Map[String, String], + placeholderPrefix: String, + placeholderSuffix: String + ) + private case class Config( + dataSource: ConfigDataSource, + base: ConfigBase, + migrationLoading: ConfigMigrationLoading, + sqlMigration: ConfigSqlMigration, + migrate: ConfigMigrate, + placeholder: ConfigPlaceholder + ) private lazy val flywayConfigDataSource = taskKey[ConfigDataSource]("The Flyway data source configuration.") private lazy val flywayConfigBase = taskKey[ConfigBase]("The Flyway base configuration.") - private lazy val flywayConfigMigrationLoading = taskKey[ConfigMigrationLoading]("The Flyway migration loading configuration.") + private lazy val flywayConfigMigrationLoading = + taskKey[ConfigMigrationLoading]("The Flyway migration loading configuration.") private lazy val flywayConfigSqlMigration = taskKey[ConfigSqlMigration]("The Flyway sql migration configuration.") private lazy val flywayConfigMigrate = taskKey[ConfigMigrate]("The Flyway migrate configuration.") private lazy val flywayConfigPlaceholder = taskKey[ConfigPlaceholder]("The Flyway placeholder configuration.") private lazy val flywayConfig = taskKey[Config]("The Flyway configuration.") private lazy val flywayClasspath = taskKey[Types.Id[Classpath]]("The classpath used by Flyway.") - //********************* + // ********************* // flyway defaults - //********************* - override def projectSettings :Seq[Setting[_]] = flywayBaseSettings(Runtime) ++ inConfig(Test)(flywayBaseSettings(Test)) + // ********************* + override def projectSettings: Seq[Setting[_]] = + flywayBaseSettings(Runtime) ++ inConfig(Test)(flywayBaseSettings(Test)) - def flywayBaseSettings(conf: Configuration) :Seq[Setting[_]] = { + def flywayBaseSettings(conf: Configuration): Seq[Setting[_]] = { import FlywayPlugin.autoImport._ val defaults = getFlywayDefaults Seq[Setting[_]]( @@ -177,14 +266,60 @@ object FlywayPlugin extends AutoPlugin { flywayInstalledBy := "", flywayCleanOnValidationError := defaults.isCleanOnValidationError, flywayCleanDisabled := defaults.isCleanDisabled, - flywayConfigDataSource := ConfigDataSource(flywayDriver.value, flywayUrl.value, flywayUser.value, flywayPassword.value), - flywayConfigBase := ConfigBase(flywaySchemas.value, flywayTable.value, flywayBaselineVersion.value, flywayBaselineDescription.value), - flywayConfigMigrationLoading := ConfigMigrationLoading(flywayLocations.value, flywayResolvers.value, flywaySkipDefaultResolvers.value, flywayEncoding.value, flywayCleanOnValidationError.value, flywayCleanDisabled.value, flywayTarget.value, flywayOutOfOrder.value, flywayCallbacks.value, flywaySkipDefaultCallbacks.value), - flywayConfigSqlMigration := ConfigSqlMigration(flywaySqlMigrationPrefix.value, flywayRepeatableSqlMigrationPrefix.value, flywaySqlMigrationSeparator.value, flywaySqlMigrationSuffixes.value:_*), - flywayConfigMigrate := ConfigMigrate(flywayIgnoreMissingMigrations.value, flywayIgnoreFutureMigrations.value, flywayIgnoreFailedFutureMigration.value, - flywayBaselineOnMigrate.value, flywayValidateOnMigrate.value, flywayMixed.value, flywayGroup.value, flywayInstalledBy.value), - flywayConfigPlaceholder := ConfigPlaceholder(flywayPlaceholderReplacement.value, flywayPlaceholders.value, flywayPlaceholderPrefix.value, flywayPlaceholderSuffix.value), - flywayConfig := Config(flywayConfigDataSource.value, flywayConfigBase.value, flywayConfigMigrationLoading.value, flywayConfigSqlMigration.value, flywayConfigMigrate.value, flywayConfigPlaceholder.value), + flywayConfigDataSource := ConfigDataSource( + flywayDriver.value, + flywayUrl.value, + flywayUser.value, + flywayPassword.value + ), + flywayConfigBase := ConfigBase( + flywaySchemas.value, + flywayTable.value, + flywayBaselineVersion.value, + flywayBaselineDescription.value + ), + flywayConfigMigrationLoading := ConfigMigrationLoading( + flywayLocations.value, + flywayResolvers.value, + flywaySkipDefaultResolvers.value, + flywayEncoding.value, + flywayCleanOnValidationError.value, + flywayCleanDisabled.value, + flywayTarget.value, + flywayOutOfOrder.value, + flywayCallbacks.value, + flywaySkipDefaultCallbacks.value + ), + flywayConfigSqlMigration := ConfigSqlMigration( + flywaySqlMigrationPrefix.value, + flywayRepeatableSqlMigrationPrefix.value, + flywaySqlMigrationSeparator.value, + flywaySqlMigrationSuffixes.value: _* + ), + flywayConfigMigrate := ConfigMigrate( + flywayIgnoreMissingMigrations.value, + flywayIgnoreFutureMigrations.value, + flywayIgnoreFailedFutureMigration.value, + flywayBaselineOnMigrate.value, + flywayValidateOnMigrate.value, + flywayMixed.value, + flywayGroup.value, + flywayInstalledBy.value + ), + flywayConfigPlaceholder := ConfigPlaceholder( + flywayPlaceholderReplacement.value, + flywayPlaceholders.value, + flywayPlaceholderPrefix.value, + flywayPlaceholderSuffix.value + ), + flywayConfig := Config( + flywayConfigDataSource.value, + flywayConfigBase.value, + flywayConfigMigrationLoading.value, + flywayConfigSqlMigration.value, + flywayConfigMigrate.value, + flywayConfigPlaceholder.value + ), flywayClasspath := (Def.taskDyn { // fullClasspath triggers the compile task, so use a dynamic task to only run it if we need to. // https://github.com/flyway/flyway-sbt/issues/10 @@ -243,64 +378,65 @@ object FlywayPlugin extends AutoPlugin { private implicit class StringOps(val s: String) extends AnyVal { def emptyToNull(): String = s match { case ss if ss.isEmpty => null - case _ => s + case _ => s } } private implicit class FluentConfigurationyOps(val flyway: FluentConfiguration) extends AnyVal { def configure(config: Config): Flyway = { flyway - .configure(config.base) - .configure(config.migrationLoading) - .configure(config.sqlMigration) - .configure(config.migrate) - .configure(config.placeholder) - .configureSysProps(config.dataSource) - .load() + .configure(config.base) + .configure(config.migrationLoading) + .configure(config.sqlMigration) + .configure(config.migrate) + .configure(config.placeholder) + .configureSysProps(config.dataSource) + .load() } def configure(config: ConfigBase): FluentConfiguration = { flyway - .schemas(config.schemas: _*) - .table(config.table) - .baselineVersion(config.baselineVersion) - .baselineDescription(config.baselineDescription) + .schemas(config.schemas: _*) + .table(config.table) + .baselineVersion(config.baselineVersion) + .baselineDescription(config.baselineDescription) } def configure(config: ConfigMigrationLoading): FluentConfiguration = { - flyway.locations(config.locations: _*) - .encoding(config.encoding) - .cleanOnValidationError(config.cleanOnValidationError) - .cleanDisabled(config.cleanDisabled) - //.target(config.target) Setting this as-is will make the default be "current", which we don't want - .outOfOrder(config.outOfOrder) - .callbacks(config.callbacks: _*) - .resolvers(config.resolvers: _*) - .skipDefaultResolvers(config.skipDefaultResolvers) - .skipDefaultCallbacks(config.skipDefaultCallbacks) + flyway + .locations(config.locations: _*) + .encoding(config.encoding) + .cleanOnValidationError(config.cleanOnValidationError) + .cleanDisabled(config.cleanDisabled) + // .target(config.target) Setting this as-is will make the default be "current", which we don't want + .outOfOrder(config.outOfOrder) + .callbacks(config.callbacks: _*) + .resolvers(config.resolvers: _*) + .skipDefaultResolvers(config.skipDefaultResolvers) + .skipDefaultCallbacks(config.skipDefaultCallbacks) } def configure(config: ConfigSqlMigration): FluentConfiguration = { flyway - .sqlMigrationPrefix(config.sqlMigrationPrefix) - .repeatableSqlMigrationPrefix(config.repeatableSqlMigrationPrefix) - .sqlMigrationSeparator(config.sqlMigrationSeparator) - .sqlMigrationSuffixes(config.sqlMigrationSuffixes: _*) + .sqlMigrationPrefix(config.sqlMigrationPrefix) + .repeatableSqlMigrationPrefix(config.repeatableSqlMigrationPrefix) + .sqlMigrationSeparator(config.sqlMigrationSeparator) + .sqlMigrationSuffixes(config.sqlMigrationSuffixes: _*) } def configure(config: ConfigMigrate): FluentConfiguration = { flyway - .ignoreMigrationPatterns(config.ignorePatterns:_*) - .baselineOnMigrate(config.baselineOnMigrate) - .validateOnMigrate(config.validateOnMigrate) - .mixed(config.mixed) - .group(config.group) - .installedBy(config.installedBy) + .ignoreMigrationPatterns(config.ignorePatterns: _*) + .baselineOnMigrate(config.baselineOnMigrate) + .validateOnMigrate(config.validateOnMigrate) + .mixed(config.mixed) + .group(config.group) + .installedBy(config.installedBy) } def configure(config: ConfigPlaceholder): FluentConfiguration = { flyway - .placeholderReplacement(config.placeholderReplacement) - .placeholders(new java.util.HashMap(config.placeholders.asJava)) - .placeholderPrefix(config.placeholderPrefix) - .placeholderSuffix(config.placeholderSuffix) + .placeholderReplacement(config.placeholderReplacement) + .placeholders(new java.util.HashMap(config.placeholders.asJava)) + .placeholderPrefix(config.placeholderPrefix) + .placeholderSuffix(config.placeholderSuffix) } def configureSysProps(config: ConfigDataSource): FluentConfiguration = { val props = new Properties() @@ -322,9 +458,10 @@ object FlywayPlugin extends AutoPlugin { def info(message: String): Unit = { streams foreach (_.log.info(message)) } def warn(message: String): Unit = { streams foreach (_.log.warn(message)) } def error(message: String): Unit = { streams foreach (_.log.error(message)) } - def error(message: String, e: Exception): Unit = { streams foreach (_.log.error(message)); streams foreach (_.log.trace(e)) } + def error(message: String, e: Exception): Unit = { + streams foreach (_.log.error(message)); streams foreach (_.log.trace(e)) + } def notice(message: String): Unit = { streams foreach (_.log.info(message)) } } } - diff --git a/src/sbt-test/flyway-sbt/test4/project/Callback.scala b/src/sbt-test/flyway-sbt/test4/project/Callback.scala index db61685..5516281 100644 --- a/src/sbt-test/flyway-sbt/test4/project/Callback.scala +++ b/src/sbt-test/flyway-sbt/test4/project/Callback.scala @@ -1,5 +1,5 @@ -import org.flywaydb.core.api.callback.{BaseCallback, Context, Event} +import org.flywaydb.core.api.callback.{ BaseCallback, Context, Event } object Callback extends BaseCallback { def handle(event: Event, context: Context): Unit = () -} \ No newline at end of file +}