Home:ALL Converter>Slick - how to split population of test data into separate file

Slick - how to split population of test data into separate file

Ask Time:2014-08-14T08:12:32         Author:benstpierre

Json Formatter

I have a working bit of code that generates my schema in slick

import scala.slick.driver.PostgresDriver.simple._
import scala.slick.jdbc.{GetResult, StaticQuery => Q}

Database.forURL("jdbc:postgresql://localhost:5432/gopher_dev",
  driver = "org.postgresql.Driver",
  user = "unodishuser",
  password = "unodishpass"
) withSession {
  implicit session =>

    //drop and recreate public schema
    Q.updateNA("drop schema public cascade").execute
    Q.updateNA("create schema public").execute

    import Tables._
    //Make the tables
    (users.ddl ++ sessionData.ddl).create

    users ++= Seq(
      (1, "Ben", "[email protected]", "secretpassword")
    )
}

This piece which inserts an example user

users ++= Seq(
  (1, "Ben", "[email protected]", "secretpassword")
)

Works great for one row but I'd like to have a file with a dozen sample rows for each table (about 10 tables in total). I can keep adding lines like this to my withSession block but as I am new to scala I confused as to how I could break this logic into a seperate piece of code/source file.

I tried making an object in another file called "TestDataBuilder"

object TestDataBuilder {

  import Tables._
  import scala.slick.driver.PostgresDriver.simple._

  users ++= Seq(
    (1, "Ben St. Pierre", "[email protected]", "secretpassword")
  )
}

Then just called TestDataBuilder inside my withSession block like so:

import scala.slick.driver.PostgresDriver.simple._
import scala.slick.jdbc.{GetResult, StaticQuery => Q}

Database.forURL("jdbc:postgresql://localhost:5432/gopher_dev",
  driver = "org.postgresql.Driver",
  user = "unodishuser",
  password = "unodishpass"
) withSession {
  implicit session =>

  //drop and recreate public schema
  Q.updateNA("drop schema public cascade").execute
  Q.updateNA("create schema public").execute

  import Tables._
  //Make the tables
  (users.ddl ++ sessionData.ddl).create

  TestDataBuilder
}

Then I get this error:

[error] /usr/local/code/gofur/src/main/scala/com/lightningstrikesolutions/scala/db/TestDataBuilder.scala:11: could not find implicit value for parameter session: scala.slick.jdbc.JdbcBackend#SessionDef
[error]   users ++= Seq(
[error]         ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 3 s, completed 13-Aug-2014 6:11:17 PM

Any idea how to fix this?

Author:benstpierre,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/25298172/slick-how-to-split-population-of-test-data-into-separate-file
yy