From 4576d0f802f864db35bab2130180888abe207c8a Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Wed, 9 Oct 2013 19:44:31 -0700 Subject: [PATCH] gograph: fix a bug which caused the unicity of (parent, name) to not be enforced --- gograph/gograph.go | 5 +++++ gograph/gograph_test.go | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/gograph/gograph.go b/gograph/gograph.go index 876bd6b58e..28ce74081e 100644 --- a/gograph/gograph.go +++ b/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") diff --git a/gograph/gograph_test.go b/gograph/gograph_test.go index 519b199699..f21d8bb85c 100644 --- a/gograph/gograph_test.go +++ b/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)