Skip to content

Commit

Permalink
Introduce DB migration by flyway
Browse files Browse the repository at this point in the history
  • Loading branch information
nacam403 committed Oct 28, 2017
1 parent af57bd3 commit d5a537a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ lazy val core = (project in file("./scala-todo-core"))
name := "Scala Todo Core",
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.2.1",
"com.typesafe.slick" %% "slick-hikaricp" % "3.2.1",
"com.h2database" % "h2" % "1.4.194",
"org.flywaydb" % "flyway-core" % "4.2.0",
"org.slf4j" % "slf4j-nop" % "1.7.10" % Test
)
)
Expand Down
5 changes: 5 additions & 0 deletions scala-todo-core/src/main/resources/db/migration/V1__init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE "TODOS" (
"ID" BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
"DESCRIPTION" VARCHAR NOT NULL,
"DONE" BOOLEAN DEFAULT false NOT NULL
)
6 changes: 2 additions & 4 deletions scala-todo-core/src/main/resources/slick.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
h2mem = {
url = "jdbc:h2:mem:todo"
h2 = {
url = "jdbc:h2:~/temp/todo"
driver = org.h2.Driver
connectionPool = disabled
keepAliveConnection = true
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package nacam403.todo.core

import com.typesafe.config.ConfigFactory
import org.flywaydb.core.Flyway
import slick.jdbc.H2Profile.api._

import scala.concurrent.Await
import scala.concurrent.duration.Duration

object DatabaseManager {

val db = Database.forConfig("h2mem", ConfigFactory.load("slick"))
val config = ConfigFactory.load("slick").getConfig("h2")
val db = Database.forConfig("", config)

def migrate() = {
Await.result(db.run(TodoTable.schema.create), Duration.Inf)
val url = config.getString("url")
val user = if (config.hasPath("user")) config.getString("user") else null
val password = if (config.hasPath("password")) config.getString("password") else null

val flyway = new Flyway
flyway.setDataSource(url, user, password)
flyway.migrate()
}

def close() = db.close()
Expand Down
4 changes: 4 additions & 0 deletions scala-todo-core/src/test/resources/slick.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
h2 = {
url = "jdbc:h2:mem:todo"
driver = org.h2.Driver
}

0 comments on commit d5a537a

Please sign in to comment.