Created
October 2, 2022 16:07
-
-
Save nafg/25b0cf942fbfe160e49b4939e1a61552 to your computer and use it in GitHub Desktop.
SlickMigration
This file contains 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 scala.concurrent.duration.Duration | |
import scala.concurrent.{Await, ExecutionContext} | |
import slick.dbio.DBIO | |
import org.flywaydb.core.api.migration.{BaseJavaMigration, Context} | |
abstract class SlickMigration extends BaseJavaMigration { | |
implicit val executionContext: ExecutionContext = ExecutionContext.global | |
def action: DBIO[Unit] | |
override def migrate(context: Context): Unit = { | |
val db = new UnmanagedDatabase(context.getConnection) | |
Await.result(db.run(action), Duration.Inf) | |
} | |
} |
This file contains 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 { | |
override def createConnection() = conn | |
override def close(): Unit = () | |
override val maxConnections = None | |
} | |
class UnmanagedSession(database: DatabaseDef) extends BaseSession(database) { | |
override def close(): Unit = () | |
} | |
class UnmanagedDatabase(conn: Connection) | |
extends JdbcBackend.DatabaseDef( | |
new UnmanagedJdbcDataSource(conn), AsyncExecutor.default("UnmanagedDatabase-AsyncExecutor") | |
) { | |
override def createSession() = new UnmanagedSession(this) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment