Skip to content

Instantly share code, notes, and snippets.

@pjazdzewski1990
Created July 8, 2015 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjazdzewski1990/537835c0e6d73f3620c8 to your computer and use it in GitHub Desktop.
Save pjazdzewski1990/537835c0e6d73f3620c8 to your computer and use it in GitHub Desktop.
val suppliers = TableQuery[Suppliers]
val products = TableQuery[Products]
val tablesExist: DBIO[Boolean] = MTable.getTables.map { tables =>
val names = Vector(suppliers.baseTableRow.tableName, products.baseTableRow.tableName)
names.intersect(tables.map(_.name.name)) == names
}
val create: DBIO[Unit] = (suppliers.schema ++ products.schema).create
val createIfNotExist: DBIO[Unit] = tablesExist.flatMap(exist => if (!exist) create else SuccessAction{})
val insertSuppliers: DBIO[Option[Int]] = suppliers.map(s => (s.id, s.name)) ++= Seq((1, "Fruits Co."), (2, "Birds R Us"))
val insertProducts: DBIO[Option[Int]] = products.map(p => (p.supplierId, p.name)) ++= Seq((1, "Bananas"), (1, "Oranges"), (2, "Norwegian Blue"))
val reloadData: DBIO[Option[Int]] = products.delete >> suppliers.delete >> insertSuppliers >> insertProducts
val listSuppliers: DBIO[Seq[String]] = suppliers.map(_.name).result
val suppliersNames: Future[Seq[String]] = db.run(createIfNotExist >> reloadData >> listSuppliers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment