-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from sdether/issue_39_sql_file_migration
WIP: Created SqlResourceMigration to read sql from a resource file
- Loading branch information
Showing
5 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
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
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"); |
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
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) | ||
} |
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
24 changes: 24 additions & 0 deletions
24
migrations/slick/src/main/scala/SqlResourceMigration.scala
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
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"):_*) | ||
} | ||
} | ||
|
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
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" |