psql schema generation

45 views
Skip to first unread message

Matthew Pocock

unread,
Apr 20, 2015, 11:20:42 AM4/20/15
to scala...@googlegroups.com
Hi,

Is there an example somewhere on-line of an sbt project that does an automated schema generation from an existing postgresql database for slick 3? I looked at the link in the slick 3 documentation, but it looked like it is linking to an example for slick 2.

I have several large, pre-existing postgres schemas to slurp in, and would really prefer to avoid writing all the scala schema glue manually.

Thanks,
Matthew

--
Dr Matthew Pocock
Turing ate my hamster LTD

Integrative Bioinformatics Group, School of Computing Science, Newcastle University

skype: matthew.pocock
tel: (0191) 2566550

Naftoli Gugenheim

unread,
Apr 20, 2015, 8:25:02 PM4/20/15
to scala...@googlegroups.com

You mean codegen? It's not that different in 3. Try it out and if you get stuck on a compiler error post back here.


--

---
You received this message because you are subscribed to the Google Groups "Slick / ScalaQuery" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scalaquery+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scalaquery/CAHQS_exCwX%2BkggRWw-c%2BvqC%2B1Yg1bvutVzQ7sxekgOyyc%3DyNmg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Matthew Pocock

unread,
Apr 21, 2015, 11:05:53 AM4/21/15
to scala...@googlegroups.com
Hi,

So for the sake of logging this, this project appears to work:

import sbt._
import Keys._

object ChebiBuild extends Build {

lazy val chebiDB = project
.in(file("chebiDB")).settings(
name := "chebi database API",
version := "0.1.0",
scalaVersion := "2.11.6",
libraryDependencies ++= Seq(
"com.typesafe.slick" %% "slick" % "3.0.0-RC3",
"com.typesafe.slick" %% "slick-codegen" % "3.0.0-RC3",
"org.slf4j" % "slf4j-nop" % "1.6.4",
"org.postgresql" % "postgresql" % "9.3-1100-jdbc4",
"com.h2database" % "h2" % "1.3.170",
"org.xerial" % "sqlite-jdbc" % "3.7.2"),
sourceGenerators in Compile <+= slickCodeGenTask
)

lazy val slick = TaskKey[Seq[File]]("gen-tables")
lazy val slickCodeGenTask = (sourceManaged, dependencyClasspath in Compile, runner in Compile, streams) map { (dir, cp, r, s) =>
val outputDir = (dir / "slick") // place generated files in sbt's managed sources folder
val url = "jdbc:postgresql://localhost/chebi" // connection info for a pre-populated throw-away, in-memory db for this demo, which is freshly initialized on every run
val jdbcDriver = "org.postgresql.Driver"
val slickDriver = "slick.driver.PostgresDriver"
val pkg = "uk.co.ebi.chebi"
toError(r.run("slick.codegen.SourceCodeGenerator", cp.files, Array(
slickDriver, jdbcDriver, url, outputDir.getPath, pkg, "chebi", "chebi"), s.log))
val fname = outputDir.getPath + "/uk/co/ebi/chebi/Tables.scala"
unmanagedSourceDirectories in Compile += outputDir
Seq(file(fname))
}
}

It's fine as a hack for one case. It would be nice if this was packaged up cleanly as a slick plugin that provides a pre-canned task. In my real project, I have a sub-project that is just this with the generated source, and then I have other sub-projects that do things with this.

Matthew


For more options, visit https://groups.google.com/d/optout.

Matan Safriel

unread,
Apr 23, 2015, 7:00:18 AM4/23/15
to scala...@googlegroups.com
Here's something for MySQL, that also gives you a diff.
Reply all
Reply to author
Forward
0 new messages