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)