-
Notifications
You must be signed in to change notification settings - Fork 21
Add Flyway support #53
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
eef5417
WIP
jakubjanecek ffec2a0
feat: Add flyway and flyway-pureconfig modules
jakubjanecek a060287
fix: compilation error
jakubjanecek 17a0c6d
refactor: PR changes
jakubjanecek 3cfcd62
docs: Add some Flyway documentation
jakubjanecek 5328a0d
docs: Add some Flyway documentation
jakubjanecek 9ad65f4
refactor: Minor PR tweaks
jakubjanecek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Flyway | ||
|
||
[](https://repo1.maven.org/maven2/com/avast/sst-flyway_2.12/) | ||
|
||
`libraryDependencies += "com.avast" %% "sst-flyway" % "<VERSION>"` | ||
|
||
This module initializes `Flyway` which can be used to do automated SQL DB migrations. See the [documentation of Flyway](https://flywaydb.org/documentation/) | ||
on how to go about that. | ||
|
||
The method `make` requires `javax.sql.DataSource` which you can for example obtain from `doobie-hikari` module: | ||
|
||
```scala | ||
import cats.effect.Resource | ||
import com.avast.sst.doobie.DoobieHikariModule | ||
import com.avast.sst.flyway.FlywayModule | ||
import zio.Task | ||
import zio.interop.catz._ | ||
import zio.interop.catz.implicits._ | ||
|
||
for { | ||
doobieTransactor <- DoobieHikariModule.make[Task](???, ???, ???, ???) | ||
flyway <- Resource.liftF(FlywayModule.make[Task](doobieTransactor.kernel, ???)) | ||
} yield () | ||
``` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Flyway | ||
|
||
[](https://repo1.maven.org/maven2/com/avast/sst-flyway_2.12/) | ||
|
||
`libraryDependencies += "com.avast" %% "sst-flyway" % "<VERSION>"` | ||
|
||
This module initializes `Flyway` which can be used to do automated SQL DB migrations. See the [documentation of Flyway](https://flywaydb.org/documentation/) | ||
on how to go about that. | ||
|
||
The method `make` requires `javax.sql.DataSource` which you can for example obtain from `doobie-hikari` module: | ||
|
||
```scala | ||
import cats.effect.Resource | ||
import com.avast.sst.doobie.DoobieHikariModule | ||
import com.avast.sst.flyway.FlywayModule | ||
import zio.Task | ||
import zio.interop.catz._ | ||
import zio.interop.catz.implicits._ | ||
|
||
for { | ||
doobieTransactor <- DoobieHikariModule.make[Task](???, ???, ???, ???) | ||
flyway <- Resource.liftF(FlywayModule.make[Task](doobieTransactor.kernel, ???)) | ||
} yield () | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
flyway-pureconfig/src/main/scala/com/avast/sst/flyway/pureconfig/ConfigReaders.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.avast.sst.flyway.pureconfig | ||
|
||
import java.nio.charset.Charset | ||
|
||
import cats.syntax.either._ | ||
import com.avast.sst.flyway.FlywayConfig | ||
import org.flywaydb.core.api.MigrationVersion | ||
import pureconfig.ConfigReader | ||
import pureconfig.error.ExceptionThrown | ||
import pureconfig.generic.semiauto.deriveReader | ||
|
||
trait ConfigReaders { | ||
|
||
implicit private[pureconfig] val charsetReader: ConfigReader[Charset] = ConfigReader[String].emap { value => | ||
Either.catchNonFatal(Charset.forName(value)).leftMap(ExceptionThrown.apply) | ||
} | ||
|
||
implicit val migrationVersionReader: ConfigReader[MigrationVersion] = ConfigReader[String].map(MigrationVersion.fromVersion) | ||
|
||
implicit val configReader: ConfigReader[FlywayConfig] = deriveReader | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
flyway-pureconfig/src/main/scala/com/avast/sst/flyway/pureconfig/implicits.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.avast.sst.flyway.pureconfig | ||
|
||
object implicits extends ConfigReaders |
26 changes: 26 additions & 0 deletions
26
flyway/src/main/scala/com/avast/sst/flyway/FlywayConfig.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.avast.sst.flyway | ||
|
||
import java.nio.charset.{Charset, StandardCharsets} | ||
|
||
import org.flywaydb.core.api.MigrationVersion | ||
|
||
final case class FlywayConfig(baselineOnMigrate: Boolean = false, | ||
baselineVersion: Option[MigrationVersion] = None, | ||
jakubjanecek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
targetVersion: Option[MigrationVersion] = None, | ||
baselineDescription: Option[String] = None, | ||
batch: Boolean = false, | ||
cleanDisabled: Boolean = false, | ||
cleanOnValidationError: Boolean = false, | ||
connectRetries: Int = 0, | ||
encoding: Charset = StandardCharsets.UTF_8, | ||
group: Boolean = false, | ||
ignoreFutureMigrations: Boolean = true, | ||
ignoreIgnoredMigrations: Boolean = false, | ||
ignoreMissingMigrations: Boolean = false, | ||
ignorePendingMigrations: Boolean = false, | ||
installedBy: Option[String] = None, | ||
mixed: Boolean = false, | ||
locations: List[String] = List.empty, | ||
outOfOrder: Boolean = false, | ||
validateOnMigrate: Boolean = true, | ||
licenseKey: Option[String] = None) |
43 changes: 43 additions & 0 deletions
43
flyway/src/main/scala/com/avast/sst/flyway/FlywayModule.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.avast.sst.flyway | ||
|
||
import cats.effect.Sync | ||
import javax.sql.DataSource | ||
import org.flywaydb.core.Flyway | ||
|
||
object FlywayModule { | ||
|
||
/** Makes [[org.flywaydb.core.Flyway]] from the given [[javax.sql.DataSource]] and config. */ | ||
@SuppressWarnings(Array("org.wartremover.warts.NonUnitStatements")) | ||
def make[F[_]: Sync](dataSource: DataSource, config: FlywayConfig): F[Flyway] = { | ||
Sync[F].delay { | ||
val builder = Flyway | ||
.configure | ||
jakubjanecek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.dataSource(dataSource) | ||
.baselineOnMigrate(config.baselineOnMigrate) | ||
.batch(config.batch) | ||
.cleanDisabled(config.cleanDisabled) | ||
.cleanOnValidationError(config.cleanOnValidationError) | ||
.connectRetries(config.connectRetries) | ||
.encoding(config.encoding) | ||
.group(config.group) | ||
.ignoreFutureMigrations(config.ignoreFutureMigrations) | ||
.ignoreIgnoredMigrations(config.ignoreIgnoredMigrations) | ||
.ignoreMissingMigrations(config.ignoreMissingMigrations) | ||
.ignorePendingMigrations(config.ignorePendingMigrations) | ||
.mixed(config.mixed) | ||
.outOfOrder(config.outOfOrder) | ||
.validateOnMigrate(config.validateOnMigrate) | ||
|
||
config.baselineVersion.foreach(builder.baselineVersion) | ||
config.targetVersion.foreach(builder.target) | ||
config.baselineDescription.foreach(builder.baselineDescription) | ||
config.installedBy.foreach(builder.installedBy) | ||
if (config.locations.nonEmpty) builder.locations(config.locations: _*) | ||
|
||
config.licenseKey.foreach(builder.licenseKey) | ||
|
||
builder.load() | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.