Skip to content

Commit

Permalink
Merge pull request #99 from pellucidanalytics/fix/schema-manager
Browse files Browse the repository at this point in the history
Fix change detection logic in SchemaManager
  • Loading branch information
dwhjames committed Dec 19, 2013
2 parents 292faaf + 3a269ba commit c56e118
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/datomisca/schemaManagement.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ object SchemaManager {
: Future[Boolean] = {
Future.traverse(schemaNames) { schemaName =>
val (requires, txDatas) = schemaMap(schemaName)
ensureSchemas(schemaTag, schemaMap, requires: _*) flatMap { _ =>
ensureSchemas(schemaTag, schemaMap, requires: _*) flatMap { dependentsChanged =>
if (txDatas.isEmpty) {
throw new DatomiscaException(s"No schema data provided for schema ${schemaName}")
} else if (hasSchema(schemaTag, schemaName)(conn.database)) {
Future.successful(false)
Future.successful(dependentsChanged)
} else {
Future.traverse(txDatas) { txData =>
Datomic.transact(
Expand Down
56 changes: 47 additions & 9 deletions integration/src/it/scala/datomisca/SchemaManagerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,29 @@ class SchemaManagerSpec
val schemaTag = Datomic.KW(":my-schema-tag")

case object SchemaA {
val attrA = Attribute(Datomic.KW(":attrA"), SchemaType.string, Cardinality.one)
var attrA = Attribute(Datomic.KW(":attrA"), SchemaType.string, Cardinality.one)

val name = "SchemaA"
var name = "SchemaA"
val requires = Seq.empty[String]
val txData = Seq(attrA)
def txData = Seq(attrA)
}
case object SchemaB {
val attrB = Attribute(Datomic.KW(":attrB"), SchemaType.string, Cardinality.one)

val name = "SchemaB"
val requires = Seq(SchemaA.name)
val txData = Seq(attrB)
def requires = Seq(SchemaA.name)
def txData = Seq(attrB)
}

case object SchemaC {
val attrC = Attribute(Datomic.KW(":attrC"), SchemaType.string, Cardinality.one)

val name = "SchemaC"
val requires = Seq(SchemaB.name)
val txData = Seq(attrC)
def requires = Seq(SchemaB.name)
def txData = Seq(attrC)
}

val schemaMap = Map(
def schemaMap = Map(
SchemaA.name ->
(SchemaA.requires,
Seq(SchemaA.txData)),
Expand All @@ -66,6 +66,7 @@ class SchemaManagerSpec

"Schema Manager" should "provision schemas if needed" in withDatomicDB { implicit conn =>

// Install schema A into an empty db
whenReady(SchemaManager.installSchema(schemaTag, schemaMap, SchemaA.name)) { changed =>
implicit val db = conn.database

Expand All @@ -78,18 +79,55 @@ class SchemaManagerSpec
SchemaManager.hasSchema(schemaTag, SchemaB.name) should be (false)
}

// reïnstall schema A
whenReady(SchemaManager.installSchema(schemaTag, schemaMap, SchemaA.name)) { changed =>
changed should be (false)
}

whenReady(SchemaManager.installSchema(schemaTag, schemaMap, SchemaC.name)) { _ =>
// install schema C
whenReady(SchemaManager.installSchema(schemaTag, schemaMap, SchemaC.name)) { changed =>
implicit val db = conn.database

changed should be (true)

SchemaManager.hasAttribute(SchemaB.attrB.ident) should be (true)
SchemaManager.hasAttribute(SchemaC.attrC.ident) should be (true)

SchemaManager.hasSchema(schemaTag, SchemaB.name) should be (true)
SchemaManager.hasSchema(schemaTag, SchemaC.name) should be (true)
}

// install schema B (which should be a reïnstall)
whenReady(SchemaManager.installSchema(schemaTag, schemaMap, SchemaB.name)) { changed =>
changed should be (false)
}

// mutate the version name of schema A (simulating an update)
val schemaANameOrig = SchemaA.name
SchemaA.name = "SchemaA_v2"

val schemaAAttrOrig = SchemaA.attrA.ident
SchemaA.attrA = Attribute(Datomic.KW(":attrA-v2"), SchemaType.string, Cardinality.one)

// reïnstall schema C (which should be an update of schema A)
whenReady(SchemaManager.installSchema(schemaTag, schemaMap, SchemaC.name)) { changed =>
implicit val db = conn.database

changed should be (true)

SchemaManager.hasAttribute(SchemaA.attrA.ident) should be (true)
SchemaManager.hasAttribute(SchemaB.attrB.ident) should be (true)
SchemaManager.hasAttribute(SchemaC.attrC.ident) should be (true)


SchemaManager.hasSchema(schemaTag, SchemaA.name) should be (true)
SchemaManager.hasSchema(schemaTag, SchemaB.name) should be (true)
SchemaManager.hasSchema(schemaTag, SchemaC.name) should be (true)

// original is still there
SchemaManager.hasAttribute(schemaAAttrOrig) should be (true)
SchemaManager.hasSchema(schemaTag, schemaANameOrig) should be (true)
}

}
}

0 comments on commit c56e118

Please sign in to comment.