Advertisement
mitrakov

Slick: 2 DB instances

Oct 4th, 2018
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.58 KB | None | 0 0
  1. // Simple (no streaming) migration data from one DB to another
  2. // using 2 DB references in Slick
  3.  
  4. // application.conf
  5. slick.dbs.remoteDb {
  6.   profile = "slick.jdbc.MySQLProfile$"
  7.   db {
  8.     driver = "com.mysql.jdbc.Driver"
  9.     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"
  10.     user = "amitrakov"
  11.     password = "qwerty"
  12.     connectionTimeout = 10s
  13.   }
  14. }
  15.  
  16. slick.dbs.localDb {
  17.   profile = "slick.jdbc.MySQLProfile$"
  18.   db {
  19.     driver = "com.mysql.jdbc.Driver"
  20.     url = "jdbc:mysql://127.0.0.1:3306/loans?useSSL=false"
  21.     user = "test"
  22.     password = "test"
  23.     connectionTimeout = 10s
  24.   }
  25. }
  26.  
  27. // CopyProcessor.scala
  28. import javax.inject.Inject
  29.  
  30. import scala.concurrent.ExecutionContext
  31.  
  32. import play.api.db.NamedDatabase
  33. import play.api.db.slick.DatabaseConfigProvider
  34. import play.api.Logger
  35. import slick.jdbc.JdbcProfile
  36. import slick.lifted.AbstractTable
  37. import dao.Tables._
  38.  
  39. class CopyProcessor @Inject()(
  40.     @NamedDatabase("remoteDb") val dbConfigProvider1: DatabaseConfigProvider,
  41.     @NamedDatabase("localDb")  val dbConfigProvider2: DatabaseConfigProvider
  42.   ) (implicit ec: ExecutionContext) {
  43.  
  44.   import profile.api._
  45.  
  46.   val db1 = dbConfigProvider1.get[JdbcProfile].db
  47.   val db2 = dbConfigProvider2.get[JdbcProfile].db
  48.  
  49.   def copyToLocal[T <: AbstractTable[_]](table: TableQuery[T]): Unit = {
  50.     db1.run(table.result) foreach { rows =>
  51.       Logger.info(s"${rows.length} rows fetched")
  52.       rows foreach { row =>
  53.         db2.run(table += row)
  54.       }
  55.     }
  56.   }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement