Skip to content

Commit

Permalink
Merge pull request #41 from sdether/issue_39_sql_file_migration
Browse files Browse the repository at this point in the history
WIP: Created SqlResourceMigration to read sql from a resource file
  • Loading branch information
lastland authored Nov 18, 2017
2 parents f352f7e + 0810e3a commit 81aa69f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
8 changes: 8 additions & 0 deletions example/migrations/src/main/resources/4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create table "user_email" (
"id" INTEGER NOT NULL PRIMARY KEY,
"user_id" INTEGER NOT NULL,
"email" VARCHAR NOT NULL
);
ALTER TABLE "user_email"
ADD FOREIGN KEY ("user_id")
REFERENCES "user"("id");
6 changes: 6 additions & 0 deletions example/migrations/src_migrations/main/scala/4.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import slick.jdbc.H2Profile.api._
import com.liyaos.forklift.slick.SqlResourceMigration

object M4 {
MyMigrations.migrations = MyMigrations.migrations :+ SqlResourceMigration(4, slick.jdbc.H2Profile)
}
3 changes: 3 additions & 0 deletions migrations/slick/src/main/scala/Commands.scala
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ trait SlickMigrationCommands
case m: SqlMigrationInterface[_] =>
println(migration.id + " SqlMigration:")
println("\t" + m.queries.map(_.getDumpInfo.mainInfo).mkString("\n\t"))
case m: SqlResourceMigrationInterface[_] =>
println(migration.id + " SqlResourceMigration:")
println(m.sqlQueries)
case m: DBIOMigration[_] =>
println(migration.id + " DBIOMigration:")
println("\t" + m.code)
Expand Down
24 changes: 24 additions & 0 deletions migrations/slick/src/main/scala/SqlResourceMigration.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.liyaos.forklift.slick

import com.liyaos.forklift.core.Migration
import slick.dbio.DBIO
import slick.jdbc.JdbcProfile

import scala.io.Source

trait SqlResourceMigrationInterface[T] extends Migration[T, DBIO[Unit]] {
def sqlQueries: String
}

case class SqlResourceMigration[T](id: T, profile: JdbcProfile) extends SqlResourceMigrationInterface[T] {

import profile.api._

private val classLoader = Thread.currentThread().getContextClassLoader
val sqlQueries: String = Source.fromInputStream(classLoader.getResourceAsStream(s"$id.sql")).mkString

def up = {
slick.dbio.DBIO.seq(List(sqlu"#$sqlQueries"):_*)
}
}

2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "0.3.1"
version in ThisBuild := "0.3.2-SNAPSHOT"

0 comments on commit 81aa69f

Please sign in to comment.