Skip to content

Commit

Permalink
Merge pull request moby#12369 from runcom/fix-links-graph-ref
Browse files Browse the repository at this point in the history
Fix wrong graphdb refs paths purging
  • Loading branch information
LK4D4 committed Apr 15, 2015
2 parents f0c60f7 + 088e69d commit 5b048ab
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
14 changes: 12 additions & 2 deletions pkg/graphdb/graphdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,22 @@ func (db *Database) Purge(id string) (int, error) {
tx.Rollback()
return -1, err
}

changes, err := rows.RowsAffected()
if err != nil {
return -1, err
}

// Clear who's using this id as parent
refs, err := tx.Exec("DELETE FROM edge WHERE parent_id = ?;", id)
if err != nil {
tx.Rollback()
return -1, err
}
refsCount, err := refs.RowsAffected()
if err != nil {
return -1, err
}

// Delete entity
if _, err := tx.Exec("DELETE FROM entity where id = ?;", id); err != nil {
tx.Rollback()
Expand All @@ -394,7 +404,7 @@ func (db *Database) Purge(id string) (int, error) {
return -1, err
}

return int(changes), nil
return int(changes + refsCount), nil
}

// Rename an edge for a given path
Expand Down
44 changes: 41 additions & 3 deletions pkg/graphdb/graphdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ func TestPurgeId(t *testing.T) {

db.Set("/webapp", "1")

if db.Refs("1") != 1 {
t.Fatal("Expect reference count to be 1")
if c := db.Refs("1"); c != 1 {
t.Fatalf("Expect reference count to be 1, got %d", c)
}

db.Set("/db", "2")
Expand All @@ -484,7 +484,45 @@ func TestPurgeId(t *testing.T) {
t.Fatal(err)
}
if count != 2 {
t.Fatal("Expected 2 references to be removed")
t.Fatalf("Expected 2 references to be removed, got %d", count)
}
}

// Regression test https://github.com/docker/docker/issues/12334
func TestPurgeIdRefPaths(t *testing.T) {
db, dbpath := newTestDb(t)
defer destroyTestDb(dbpath)

db.Set("/webapp", "1")
db.Set("/db", "2")

db.Set("/db/webapp", "1")

if c := db.Refs("1"); c != 2 {
t.Fatalf("Expected 2 reference for webapp, got %d", c)
}
if c := db.Refs("2"); c != 1 {
t.Fatalf("Expected 1 reference for db, got %d", c)
}

if rp := db.RefPaths("2"); len(rp) != 1 {
t.Fatalf("Expected 1 reference path for db, got %d", len(rp))
}

count, err := db.Purge("2")
if err != nil {
t.Fatal(err)
}

if count != 2 {
t.Fatalf("Expected 2 rows to be removed, got %d", count)
}

if c := db.Refs("2"); c != 0 {
t.Fatalf("Expected 0 reference for db, got %d", c)
}
if c := db.Refs("1"); c != 1 {
t.Fatalf("Expected 1 reference for webapp, got %d", c)
}
}

Expand Down

0 comments on commit 5b048ab

Please sign in to comment.