Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Simple (no streaming) migration data from one DB to another
- // using 2 DB references in Slick
- // application.conf
- slick.dbs.remoteDb {
- profile = "slick.jdbc.MySQLProfile$"
- db {
- driver = "com.mysql.jdbc.Driver"
- url = "jdbc:mysql://127.0.0.1:3307/loans?useSSL=false" // port forwarding: "ssh -L 127.0.0.1:3307:remote:3306 amitrakov@proxy.com"
- user = "amitrakov"
- password = "qwerty"
- connectionTimeout = 10s
- }
- }
- slick.dbs.localDb {
- profile = "slick.jdbc.MySQLProfile$"
- db {
- driver = "com.mysql.jdbc.Driver"
- url = "jdbc:mysql://127.0.0.1:3306/loans?useSSL=false"
- user = "test"
- password = "test"
- connectionTimeout = 10s
- }
- }
- // CopyProcessor.scala
- import javax.inject.Inject
- import scala.concurrent.ExecutionContext
- import play.api.db.NamedDatabase
- import play.api.db.slick.DatabaseConfigProvider
- import play.api.Logger
- import slick.jdbc.JdbcProfile
- import slick.lifted.AbstractTable
- import dao.Tables._
- class CopyProcessor @Inject()(
- @NamedDatabase("remoteDb") val dbConfigProvider1: DatabaseConfigProvider,
- @NamedDatabase("localDb") val dbConfigProvider2: DatabaseConfigProvider
- ) (implicit ec: ExecutionContext) {
- import profile.api._
- val db1 = dbConfigProvider1.get[JdbcProfile].db
- val db2 = dbConfigProvider2.get[JdbcProfile].db
- def copyToLocal[T <: AbstractTable[_]](table: TableQuery[T]): Unit = {
- db1.run(table.result) foreach { rows =>
- Logger.info(s"${rows.length} rows fetched")
- rows foreach { row =>
- db2.run(table += row)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement