Created
May 26, 2016 12:18
-
-
Save gleeb/0ded6186594cda4aedf47bc45dc41d88 to your computer and use it in GitHub Desktop.
slick mysql autogenerate schema sbt task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lazy val slickGenerate = taskKey[Seq[File]]("slick code generation from an existing database") | |
slickGenerate := { | |
import java.io.File | |
val dbName = "" | |
val userName = "" | |
val password = "" | |
val url = s"jdbc:mysql://localhost:3306/$dbName" // adapt as necessary to your system | |
val jdbcDriver = "com.mysql.jdbc.Driver" // replace if not MySQL | |
val slickDriver = "slick.driver.MySQLDriver" // replace if not MySQL | |
val resultRelativeDir = "model" // directory to create output scala slick definitions at | |
val targetPackageName = "com.cmp.model" // package name to give it | |
val resultFilePath = s"$resultRelativeDir/$targetPackageName/Tables.scala" // override the name if you like | |
val backupFilePath = s"$resultRelativeDir/$targetPackageName/Tables.scala.auto-backup" // override the name if you like | |
val format = scala.Console.BLUE + scala.Console.BOLD | |
println(format + s"Backing up existing slick mappings source to: file://${baseDirectory.value}/$backupFilePath") | |
println(format + s"About to auto-generate slick mappings source from database schema at $url...") | |
sbt.IO.copyFile(new File(resultFilePath), new File(backupFilePath)) | |
(runner in Compile).value.run("slick.codegen.SourceCodeGenerator", (dependencyClasspath in Compile).value.files, Array(slickDriver, jdbcDriver, url, resultRelativeDir, targetPackageName, userName, password), streams.value.log) | |
println(format + s"Result: file://${baseDirectory.value}/$resultFilePath" + scala.Console.RESET) | |
val diff = (s"diff -u $resultFilePath $backupFilePath" #| "colordiff").!! | |
println(scala.Console.BLUE + s"Changes compared to previous output file, follow, if any.\n\n $diff") | |
Seq(file(resultFilePath)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment