diff --git a/src/main/scala/com/chrisomeara/pillar/Parser.scala b/src/main/scala/com/chrisomeara/pillar/Parser.scala index 55a2978..9fc5d71 100644 --- a/src/main/scala/com/chrisomeara/pillar/Parser.scala +++ b/src/main/scala/com/chrisomeara/pillar/Parser.scala @@ -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() = { @@ -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 @@ -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 { diff --git a/src/test/resources/pillar/migrations/faker/1370028265000_creates_profiles_table.cql b/src/test/resources/pillar/migrations/faker/1370028265000_creates_profiles_table.cql new file mode 100644 index 0000000..4584a18 --- /dev/null +++ b/src/test/resources/pillar/migrations/faker/1370028265000_creates_profiles_table.cql @@ -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') diff --git a/src/test/scala/com/chrisomeara/pillar/ParserSpec.scala b/src/test/scala/com/chrisomeara/pillar/ParserSpec.scala index 2d6c140..9d90529 100644 --- a/src/test/scala/com/chrisomeara/pillar/ParserSpec.scala +++ b/src/test/scala/com/chrisomeara/pillar/ParserSpec.scala @@ -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" diff --git a/src/test/scala/com/chrisomeara/pillar/PillarCommandLineAcceptanceSpec.scala b/src/test/scala/com/chrisomeara/pillar/PillarCommandLineAcceptanceSpec.scala index fcd2994..ea20393 100644 --- a/src/test/scala/com/chrisomeara/pillar/PillarCommandLineAcceptanceSpec.scala +++ b/src/test/scala/com/chrisomeara/pillar/PillarCommandLineAcceptanceSpec.scala @@ -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) diff --git a/src/test/scala/com/chrisomeara/pillar/RegistrySpec.scala b/src/test/scala/com/chrisomeara/pillar/RegistrySpec.scala index f31f8a0..c0c325b 100644 --- a/src/test/scala/com/chrisomeara/pillar/RegistrySpec.scala +++ b/src/test/scala/com/chrisomeara/pillar/RegistrySpec.scala @@ -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) } }