Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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