forked from scalikejdbc/scalikejdbc
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixture support for scalikejdbc-play-plugin.
This pull request will add fixture feature to scalikejdbc-play-plugin. - Fixture scripts are Plain SQL like play-evolutions. - A fixture script have a 'Ups' part and a 'Downs' part like evolutions. 'Ups' are executed on starting and 'Downs' are on stopping. - The location for fixture scripts is config/db/fixtures/${dbname} - Need to specify fixture scripts in application.conf. To use in DEV mode db.${dbname}.fixtures.dev="insert_data.sql" To use in TEST mode db.${dbname}.fixtures.test="insert_data.sql" Multiple scripts are allowed too. db.${dbname}.fixtures.dev=[ "insert_data.sql", "insert_additional_data.sql" ] When specified multiple scripts, Ups part are executed in order. Downs are executed in reverse.
- Loading branch information
Showing
9 changed files
with
296 additions
and
75 deletions.
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
43 changes: 43 additions & 0 deletions
43
scalikejdbc-play-plugin/src/main/scala/scalikejdbc/Fixture.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,43 @@ | ||
/* | ||
* Copyright 2013 Toshiyuki Takahashi | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package scalikejdbc | ||
|
||
import java.io.File | ||
import play.api.libs.Files | ||
|
||
case class Fixture(file: File) { | ||
|
||
private def script: String = Files.readFile(file) | ||
|
||
private def isUpsMarker(s: String): Boolean = s.matches("""^#.*!Ups.*$""") | ||
|
||
private def isDownsMarker(s: String): Boolean = s.matches("""^#.*!Downs.*$""") | ||
|
||
def upScript: String = | ||
script | ||
.lines | ||
.dropWhile { line => !isUpsMarker(line) } | ||
.dropWhile { line => isUpsMarker(line) } | ||
.takeWhile { line => !isDownsMarker(line) } | ||
.mkString("\n") | ||
|
||
def downScript: String = | ||
script | ||
.lines | ||
.dropWhile { line => !isDownsMarker(line) } | ||
.dropWhile { line => isDownsMarker(line) } | ||
.mkString("\n") | ||
} |
20 changes: 20 additions & 0 deletions
20
scalikejdbc-play-plugin/src/main/scala/scalikejdbc/FixtureNotFoundException.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,20 @@ | ||
/* | ||
* Copyright 2013 Toshiyuki Takahashi | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package scalikejdbc | ||
|
||
class FixtureNotFoundException(message: String) | ||
extends Exception(message) | ||
|
89 changes: 89 additions & 0 deletions
89
scalikejdbc-play-plugin/src/main/scala/scalikejdbc/FixtureSupport.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,89 @@ | ||
/* | ||
* Copyright 2013 Toshiyuki Takahashi | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package scalikejdbc | ||
|
||
import play.api._ | ||
import java.io.File | ||
import scala.collection.JavaConverters._ | ||
|
||
trait FixtureSupport { | ||
|
||
val fixturesRootPath: String = "db/fixtures" | ||
|
||
private def fixtureConfigKey(dbName: String)(implicit app: Application): String = | ||
if (Play.isDev) { | ||
"db." + dbName + ".fixtures.dev" | ||
} else if (Play.isTest) { | ||
"db." + dbName + ".fixtures.test" | ||
} else { | ||
throw new UnsupportedOperationException("Fixture feature is only provided for dev mode and test mode.") | ||
} | ||
|
||
def fixtures(implicit app: Application): Map[String, Seq[Fixture]] = { | ||
(for { | ||
dbConfig <- app.configuration.getConfig("db").toList | ||
subKey <- dbConfig.subKeys | ||
} yield { | ||
val dbName = subKey | ||
val fixtureNames: Seq[String] = try { | ||
app.configuration.getStringList(fixtureConfigKey(subKey)) | ||
.map(_.asScala) | ||
.getOrElse(Nil) | ||
} catch { | ||
case e: PlayException => { | ||
app.configuration.getString(fixtureConfigKey(subKey)).toSeq | ||
} | ||
} | ||
|
||
val fixtureFiles = fixtureNames.map { fixtureName => | ||
val resourceName = List(fixturesRootPath, dbName, fixtureName).mkString("/") | ||
app.resource(resourceName) match { | ||
case Some(resource) => Fixture(new File(resource.getPath)) | ||
case None => throw new FixtureNotFoundException( | ||
"Fixture not found (%s)".format(resourceName) | ||
) | ||
} | ||
} | ||
|
||
(dbName -> fixtureFiles) | ||
}).toMap | ||
} | ||
|
||
def loadFixtures()(implicit app: Application): Unit = { | ||
for { | ||
(dbName, fs) <- fixtures | ||
f <- fs | ||
} { | ||
execute(dbName, f.upScript) | ||
} | ||
} | ||
|
||
def cleanFixtures()(implicit app: Application): Unit = { | ||
for { | ||
(dbName, fs) <- fixtures | ||
f <- fs.reverse | ||
} { | ||
execute(dbName, f.downScript) | ||
} | ||
} | ||
|
||
private def execute(dbName: String, script: String): Unit = { | ||
NamedDB(Symbol(dbName)) localTx { implicit session => | ||
SQL(script).update.apply() | ||
} | ||
} | ||
|
||
} |
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
69 changes: 69 additions & 0 deletions
69
scalikejdbc-play-plugin/src/test/scala/scalikejdbc/FixtureSpec.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,69 @@ | ||
package scalikejdbc | ||
|
||
import java.io.File | ||
import play.api.test._ | ||
import play.api.test.Helpers._ | ||
import play.api.libs.Files | ||
import org.specs2.mutable._ | ||
import org.specs2.specification.BeforeAfterExample | ||
|
||
class FixtureSpec extends Specification with BeforeAfterExample { | ||
|
||
def before = { | ||
} | ||
|
||
def after = { | ||
} | ||
|
||
def fixture = { | ||
val script = """ | ||
| | ||
|# --- !Ups | ||
|drop table users if exists; | ||
|create table users ( | ||
| email varchar(255) not null primary key, | ||
| name varchar(255) not null, | ||
| password varchar(255) not null | ||
|); | ||
| | ||
|# --- !Downs | ||
|drop table users if exists; | ||
| | ||
|""".stripMargin | ||
|
||
val tmpfile = File.createTempFile("tmp", ".sql") | ||
tmpfile.deleteOnExit() | ||
val writer = new java.io.PrintWriter(tmpfile) | ||
try { | ||
writer.println(script) | ||
} finally { | ||
writer.close() | ||
} | ||
Fixture(tmpfile) | ||
} | ||
|
||
"Fixture" should { | ||
|
||
"has #upScript" in { | ||
val expected = | ||
"""|drop table users if exists; | ||
|create table users ( | ||
| email varchar(255) not null primary key, | ||
| name varchar(255) not null, | ||
| password varchar(255) not null | ||
|); | ||
|""".stripMargin | ||
fixture.upScript must_== expected | ||
} | ||
|
||
"has #downScript" in { | ||
val expected = | ||
"""|drop table users if exists; | ||
| | ||
|""".stripMargin | ||
fixture.downScript must_== expected | ||
} | ||
|
||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
scalikejdbc-play-plugin/src/test/scala/scalikejdbc/FixtureSupportSpec.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,47 @@ | ||
package scalikejdbc | ||
|
||
import play.api.test._ | ||
import play.api.test.Helpers._ | ||
import org.specs2.mutable._ | ||
import org.specs2.specification.BeforeAfterExample | ||
import play.api.Play.current | ||
import scala.collection.JavaConverters._ | ||
|
||
class FixtureSupportSpec extends Specification with BeforeAfterExample { | ||
|
||
def before = { | ||
} | ||
|
||
def after = { | ||
} | ||
|
||
val fixtureSupport = new FixtureSupport {} | ||
|
||
def fakeApp = FakeApplication( | ||
additionalConfiguration = Map( | ||
"db.default.fixtures.test" -> List("users.sql", "project.sql").asJava, | ||
"db.secondary.fixtures.test" -> "a.sql", | ||
"db.default.driver" -> "org.h2.Driver", | ||
"db.default.url" -> "jdbc:h2:mem:default;DB_CLOSE_DELAY=-1", | ||
"db.default.user" -> "sa", | ||
"db.default.password" -> "sa", | ||
"db.secondary.driver" -> "org.h2.Driver", | ||
"db.secondary.url" -> "jdbc:h2:mem:secondary;DB_CLOSE_DELAY=-1", | ||
"db.secondary.user" -> "l", | ||
"db.secondary.password" -> "g" | ||
), | ||
additionalPlugins = Seq("scalikejdbc.PlayPlugin") | ||
) | ||
|
||
"FixtureSupport" should { | ||
|
||
"has #fixtures" in { | ||
running(fakeApp) { | ||
fixtureSupport.fixtures must have size 2 | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
|
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
Oops, something went wrong.