-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DbMigration.scala
42 lines (32 loc) · 1.08 KB
/
DbMigration.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package japgolly.webapputil.db
import cats.effect.IO
import javax.sql.DataSource
import org.flywaydb.core.Flyway
import org.flywaydb.core.api.configuration.FluentConfiguration
object DbMigration {
def apply(ds : DataSource,
schema: Option[String] = None,
flyway: FlywayConfig = FlywayConfig.default,
): DbMigration = {
var cfg = flyway(Flyway.configure).dataSource(ds)
schema.foreach(s => cfg = cfg.schemas(s))
new DbMigration(cfg)
}
type FlywayConfig = FluentConfiguration => FluentConfiguration
object FlywayConfig {
def default: FlywayConfig = _
.locations("db_migrations")
.sqlMigrationPrefix("v")
}
}
final class DbMigration(private val flywayCfg: FluentConfiguration) {
private val flyway: Flyway =
flywayCfg.load()
def withFlywayConfig(f: DbMigration.FlywayConfig): DbMigration =
new DbMigration(f(flywayCfg))
def migrate: IO[Unit] =
IO(flyway.migrate())
/** Drops all objects (tables, views, procedures, triggers, ...) in the configured schemas. */
def drop: IO[Unit] =
IO(flyway.clean())
}