Created
October 16, 2017 00:40
-
-
Save nafg/377e7c647bf14400aa4b5dd12b98b6ea to your computer and use it in GitHub Desktop.
flyway scala migration
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
import java.sql.Connection | |
import org.flywaydb.core.api.migration.jdbc.JdbcMigration | |
import slick.dbio.DBIO | |
import scala.concurrent.duration.Duration | |
import scala.concurrent.{Await, ExecutionContext} | |
abstract class SlickMigration extends JdbcMigration { | |
implicit def executionContext = ExecutionContext.global | |
def action: DBIO[Any] | |
final override def migrate(connection: Connection): Unit = { | |
val db = new UnmanagedDatabase(connection) | |
val future = db.run(action) | |
future.onFailure { | |
case e => e.printStackTrace() | |
} | |
Await.result(future, Duration.Inf) | |
} | |
} |
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
import java.sql.Connection | |
import slick.jdbc.JdbcBackend.{BaseSession, DatabaseDef} | |
import slick.jdbc.{JdbcBackend, JdbcDataSource} | |
import slick.util.AsyncExecutor | |
class UnmanagedJdbcDataSource(conn: Connection) extends JdbcDataSource { | |
def createConnection() = conn | |
def close() = () | |
} | |
class UnmanagedSession(database: DatabaseDef) extends BaseSession(database) { | |
override def close() = () | |
} | |
class UnmanagedDatabase(conn: Connection) extends JdbcBackend.DatabaseDef(new UnmanagedJdbcDataSource(conn), AsyncExecutor("UmanagedDatabase-AsyncExecutor", 1, -1)) { | |
override def createSession() = new UnmanagedSession(this) | |
} |
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
import java.sql.Types | |
import java.util.UUID | |
import slick.driver.H2Driver._ | |
import api._ | |
import slick.jdbc.{GetResult, SetParameter} | |
class V6__housing_unit_development_id extends SlickMigration { | |
implicit val setParam = SetParameter[UUID]((uuid, pp) => pp.setObject(uuid, Types.VARCHAR)) | |
implicit val getResult = GetResult[UUID](_.nextObject().asInstanceOf[UUID]) | |
def action = { | |
val myUuid = UUID.randomUUID() | |
sql"SELECT count(*) FROM housing_unit".as[Int].head flatMap { | |
case 0 => | |
sqlu"ALTER TABLE housing_unit ADD development_id UUID NOT NULL" | |
case _ => | |
DBIO.seq( | |
sqlu"INSERT INTO development (id, name) VALUES ($myUuid, 'default development')", | |
sqlu"ALTER TABLE housing_unit ADD development_id UUID", | |
sqlu"UPDATE housing_unit SET development_id = $myUuid", | |
sqlu"ALTER TABLE housing_unit ALTER COLUMN development_id SET NOT NULL" | |
) | |
} andThen | |
sqlu"ALTER TABLE housing_unit ADD FOREIGN KEY (development_id) REFERENCES development(id) ON DELETE RESTRICT ON UPDATE CASCADE" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment