Переглянути джерело

gograph: fix a bug which caused the unicity of (parent, name) to not be enforced

Solomon Hykes 11 роки тому
батько
коміт
4576d0f802
2 змінених файлів з 17 додано та 0 видалено
  1. 5 0
      gograph/gograph.go
  2. 12 0
      gograph/gograph_test.go

+ 5 - 0
gograph/gograph.go

@@ -22,7 +22,9 @@ const (
         CONSTRAINT "parent_fk" FOREIGN KEY ("parent_id") REFERENCES "entity" ("id"),
         CONSTRAINT "entity_fk" FOREIGN KEY ("entity_id") REFERENCES "entity" ("id")
         );
+    `
 
+	createEdgeIndices = `
     CREATE UNIQUE INDEX "name_parent_ix" ON "edge" (parent_id, name);
     `
 )
@@ -67,6 +69,9 @@ func NewDatabase(dbPath string) (*Database, error) {
 	if _, err := conn.Exec(createEdgeTable); err != nil {
 		return nil, err
 	}
+	if _, err := conn.Exec(createEdgeIndices); err != nil {
+		return nil, err
+	}
 
 	rollback := func() {
 		conn.Exec("ROLLBACK")

+ 12 - 0
gograph/gograph_test.go

@@ -59,6 +59,18 @@ func TestSetEntityWithDifferentName(t *testing.T) {
 	}
 }
 
+func TestSetDuplicateEntity(t *testing.T) {
+	db := newTestDb(t)
+	defer destroyTestDb(db)
+
+	if _, err := db.Set("/foo", "42"); err != nil {
+		t.Fatal(err)
+	}
+	if _, err := db.Set("/foo", "43"); err == nil {
+		t.Fatalf("Creating an entry with a duplciate path did not cause an error")
+	}
+}
+
 func TestCreateChild(t *testing.T) {
 	db := newTestDb(t)
 	defer destroyTestDb(db)