Slick ã®ã³ã¼ããçæãã sbt ãã©ã°ã¤ã³ãä½ãã¾ãã
Slick ã³ã¼ããçæã®ã¤ãã©ãªããããå ¬å¼ã®ã»ãã«ããã¤ã使ãæ¹ã®ãµã³ãã«ãããã¾ãããæ¯åã³ããããã®ããªãã¨æã£ã¦ sbt ãã©ã°ã¤ã³ã«ãã¾ãããä»ã®ã¨ãã Slick 2.1.0 ã«ä¾åãã¦ãã¾ããSlick 3.0.0 ãåºããããç¨ã®ãã¼ã¸ã§ã³ãåºããã¨æãã¾ãã
ã¤ã³ã¹ãã¼ã«ã¯ plugins.sbt ã« addSbtPlugin ãå ããä¸ã§ãã³ã¼ãçæã«ä½¿ã JDBC ãã©ã¤ãã追å ãã¾ãã
// plugins.sbt addSbtPlugin("com.github.tototoshi" % "sbt-slick-codegen" % "0.1.0") // Database driver // For example, when you are using PostgreSQL libraryDependencies += "org.postgresql" % "postgresql" % "9.4-1201-jdbc41
build.sbt ã®è¨å®ã¯ä»¥ä¸ã®ãããªæãã§ãã
// build.sbt import scala.slick.codegen.SourceCodeGenerator import scala.slick.{ model => m } // required slickCodegenSettings // required // Register codegen hook sourceGenerators in Compile <+= slickCodegen // required slickCodegenDatabaseUrl := "jdbc:postgresql://localhost/example" // required slickCodegenDatabaseUser := "dbuser" // required slickCodegenDatabasePassword := "dbpassword" // required (If not set, postgresql driver is choosen) slickCodegenDriver := scala.slick.driver.PostgresDriver // required (If not set, postgresql driver is choosen) slickCodegenJdbcDriver := "org.postgresql.Driver" // optional but maybe you want slickCodegenOutputPackage := "com.example.models" // optional, pass your own custom source code generator slickCodegenCodeGenerator := { (model: m.Model) => new SourceCodeGenerator(model) } // optional // For example, to exclude flyway's schema_version table from the target of codegen slickCodegenExcludedTables in Compile := Seq("schema_version")
sourceGenerators in Compile
ã«å ãããã¨ã§ã³ã³ãã¤ã«ã®åã«ã³ã¼ãçæãèµ°ãã¾ãããã®è¨å®ã§ã¯æ¯åã³ã¼ãçæãèµ°ãã¾ãããæ¯ååããã¡ã¤ã«ãçæããããããåã³ã³ãã¤ã«ããããã¨ã¯ããã¾ãããã³ã¼ãçæèªä½ã®æéãæ°ã«ãªãå ´å㯠slickCodegen
ã¿ã¹ã¯ã«ãã£ãã·ã¥ãå
¥ããã°ããã¨æãã¾ããsbt ã® FileFunction.cached
ã¨ã使ãã°ç°¡åã«å®è£
ã§ããã¨æãã¾ãã
ããã©ã«ãã§ã¯ Slick ã® SourceCodeGenerator
ã«ããã½ã¼ã¹ã³ã¼ãçæãè¡ããã¾ããã以åãã®ããã°ã«ãæ¸ãã¾ããããjava.sql.Timestamp
ãããªã㦠joda-time ã使ãããããªã©ã¨ãããã¨ãããã¨æãã¾ãããã®ã¨ã㯠slickCodegenCodeGenerator
ã«ã«ã¹ã¿ãã¤ãºãã SourceCodeGenerator
ãè¨å®ãã¾ãã
slickCodegenCodeGenerator := { (model: m.Model) => new SourceCodeGenerator(model) { override def code = "import com.github.tototoshi.slick.PostgresJodaSupport._\n" + "import org.joda.time.DateTime\n" + super.code override def Table = new Table(_) { override def Column = new Column(_) { override def rawType = model.tpe match { case "java.sql.Timestamp" => "DateTime" case _ => super.rawType } } } } }
ãã¼ã¿ãã¼ã¹ãã¤ã°ã¬ã¼ã·ã§ã³ã¨å ±åããã
æ¬ç·¨ãããããSlick ã«éã£ã話ã§ã¯ãªãã§ããããã§ã«ãã¼ã¿ãã¼ã¹ã«ãããã¼ãã«ã使ã£ã¦ã³ã¼ãçæãè¡ãã¨ããä»çµã¿ã¯ããã¼ã¿ãã¼ã¹ãã¤ã°ã¬ã¼ã·ã§ã³ãã¼ã«ã¨ã®ç¸æ§ãé常ã«æªãã§ããplay-evolutions ã play-flyway 㯠Play ã¢ããªã±ã¼ã·ã§ã³ã®èµ·åæã«ãã¼ã¿ãã¼ã¹ãã¤ã°ã¬ã¼ã·ã§ã³ãè¡ããããã³ã¼ãçæã¨ãã¤ã°ã¬ã¼ã·ã§ã³ããããããã¯ãã¦ã©ãããããããã¾ããã
ä»ã®ã¨ãã㯠dev-mode ã®ãã¼ã¿ãã¼ã¹ãã¤ã°ã¬ã¼ã·ã§ã³ã¯ play-flyway ã§ã¯ãªããFlyway ã® sbt ãã©ã°ã¤ã³ã使ããã¨ã§åé¿ãã¦ãã¾ãããã¹ãã®ã¨ããªã©ã® Slick ã®ã³ã¼ãçæãçµãã£ãå¾ã§ã¯ play-flyway ã使ããããã«ãªãã¾ããã¡ãã£ã¨ããµãæ°ããã¦ããããã©ã追å ããè¨å®ãå°ãªãããã¾ãè¯ããã¨ããæãã§ããsbt ã§ã¯ãªã Flyway ã®ã³ãã³ãã©ã¤ã³ãã¼ã«ã使ãã¨ããã®ã§ãè¯ãã¨æãã¾ãã
å°ã£ããã¨ã« Flyway ã® sbt ãã©ã°ã¤ã³ã使ã£ãããã¨ãã£ã¦ãã®åé¡ã¨ãããªãã§ããããã§ã¯ããã¾ããã§ãããå®ã¯ãã®ãã©ã°ã¤ã³ãã³ã³ãã¤ã«ã®å¾ã«ãã¤ã°ã¬ã¼ã·ã§ã³ãèµ°ãããä½ãã«ãªã£ã¦ãããããFlyway ç¨ã«å¥ã®ç©ºããã¸ã§ã¯ãã使ã£ã¦ãããã§ãã¤ã°ã¬ã¼ã·ã§ã³ãè¡ããã¨ããããã«ãã¾ãããflyway
ã¨ãã空ããã¸ã§ã¯ããä½ã£ããããã¤ã°ã¬ã¼ã·ã§ã³ã¯ flyway/flywayMigrate
ã¨ããã³ãã³ãã§å®è¡ã§ãã¾ãã
è¨å®ãè¦ã¦ãããã®ãæ©ãã¨æãã®ã§ããµã³ãã«ããã¸ã§ã¯ãã«ã¾ã¨ãã¦ç½®ãã¾ããã
åé¿ã«åé¿ãéãã¾ããããæçµçãªè¨å®ã¨ãã¦ã¯ä¸å¿ç解å¯è½ãªã¬ãã«ã«ã¾ã¨ããããã®ã§ã¯ãªããã¨æãã¾ããæåã¯ãµãããã¸ã§ã¯ã㧠FakeApplication ã使ã£ã¦ç¡çãã Play.start ãã¦ãã¤ã°ã¬ã¼ã·ã§ã³ãèµ°ããã¦...ã¿ãããªãã¨ããã¦ããï¼æå³ãããã§ããããï¼ã®ã§ããããããã¯ãã£ã¨è¯ãã¨æãã¾ãã