Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions src/main/scala/com/chrisomeara/pillar/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ class PartialMigration {
var upStages = new mutable.MutableList[String]()
var downStages : Option[mutable.MutableList[String]] = None

var currentUp = new mutable.MutableList[String]()
var currentUp = new mutable.MutableList[mutable.MutableList[String]]()
var currentDown: Option[mutable.MutableList[String]] = None

var lastLine = ""

def rotateUp() = {
upStages += currentUp.mkString("\n")
upStages ++= currentUp.withFilter(_.nonEmpty).map(_.filter(_.nonEmpty).mkString("\n"))
upStages = upStages.filterNot(line => line.isEmpty)
currentUp = new mutable.MutableList[String]()
lastLine = ""
currentUp = new mutable.MutableList[mutable.MutableList[String]]()
}

def rotateDown() = {
Expand Down Expand Up @@ -83,6 +86,14 @@ class Parser {

case object ParsingDownStage extends ParserState

private def parseMultilineStatments(pm: PartialMigration, currentLine : String): Unit = {
if(pm.lastLine.trim().isEmpty) pm.currentUp += new mutable.MutableList[String]()

pm.currentUp.last += currentLine

pm.lastLine = currentLine
}

def parse(resource: InputStream): Migration = {
val inProgress = new PartialMigration
var state: ParserState = ParsingAttributes
Expand All @@ -105,13 +116,10 @@ class Parser {
case ParsingDownStage => inProgress.rotateDown(); inProgress.currentDown = Some(new mutable.MutableList[String]())
}
case cql =>
if (!cql.isEmpty) {

state match {
case ParsingUp | ParsingUpStage => inProgress.currentUp += cql
case ParsingDown | ParsingDownStage => inProgress.currentDown.get += cql
case other =>
}
(cql.isEmpty, state) match {
case (_, ParsingUp | ParsingUpStage) => parseMultilineStatments(inProgress, cql)
case (false, ParsingDown | ParsingDownStage) => inProgress.currentDown.get += cql
case other =>
}
}
inProgress.validate match {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- description: creates profiles table and populate
-- authoredAt: 1370028265000
-- up:

CREATE TABLE profiles (
email text,
name text,
address text,
PRIMARY KEY (email)
)

INSERT INTO profiles(email, name, address) VALUES(
'rich@yopmail.com',
'Rich Halle',
'Schadowstrasse 124; Dusseldorf')
38 changes: 38 additions & 0 deletions src/test/scala/com/chrisomeara/pillar/ParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,44 @@ class ParserSpec extends FunSpec with BeforeAndAfter with ShouldMatchers {
}
}

describe("1370028265000_creates_profiles_table.cql") {
val migrationPath = "src/test/resources/pillar/migrations/faker/1370028265000_creates_profiles_table.cql"

it("returns a migration object") {
val resource = new FileInputStream(migrationPath)
Parser().parse(resource).getClass should be(classOf[IrreversibleMigration])
}

it("assigns authoredAt") {
val resource = new FileInputStream(migrationPath)
Parser().parse(resource).authoredAt should equal(new Date(1370028265000L))
}

it("assigns description") {
val resource = new FileInputStream(migrationPath)
Parser().parse(resource).description should equal("creates profiles table and populate")
}

it("creates two up statements from the `up` section") {
val resource = new FileInputStream(migrationPath)
val migration = Parser().parse(resource)

migration.up should contain(
"""CREATE TABLE profiles (
| email text,
| name text,
| address text,
| PRIMARY KEY (email)
|)""".stripMargin)

migration.up should contain(
"""INSERT INTO profiles(email, name, address) VALUES(
| 'rich@yopmail.com',
| 'Rich Halle',
| 'Schadowstrasse 124; Dusseldorf')""".stripMargin)
}
}

describe("1370028263000_creates_views_table.cql") {
val migrationPath = "src/test/resources/pillar/migrations/faker/1370028263000_creates_views_table.cql"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class PillarCommandLineAcceptanceSpec extends FeatureSpec with GivenWhenThen wit
session.execute(QueryBuilder.select().from(keyspaceName, "views")).all().size() should equal(0)

And("the applied_migrations table records the migrations")
session.execute(QueryBuilder.select().from(keyspaceName, "applied_migrations")).all().size() should equal(4)
session.execute(QueryBuilder.select().from(keyspaceName, "applied_migrations")).all().size() should equal(5)

And("the first migration was authored at Fri May 31 18:01:02 2013")
session.execute(QueryBuilder.select().from(keyspaceName, "applied_migrations").where(QueryBuilder.eq("authored_at", 1370023262000L))).all().size() should equal(1)
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/com/chrisomeara/pillar/RegistrySpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class RegistrySpec extends FunSpec with BeforeAndAfter with ShouldMatchers with
describe("with a directory that exists and has migration files") {
it("returns a registry with migrations") {
val registry = Registry.fromDirectory(new File("src/test/resources/pillar/migrations/faker/"))
registry.all.size should equal(4)
registry.all.size should equal(5)
}
}

Expand Down