Browse code

Fix wrong graphdb refs paths purging

Signed-off-by: Antonio Murdaca <me@runcom.ninja>

Antonio Murdaca authored on 2015/04/15 06:10:17
Showing 2 changed files
... ...
@@ -378,12 +378,22 @@ func (db *Database) Purge(id string) (int, error) {
378 378
 		tx.Rollback()
379 379
 		return -1, err
380 380
 	}
381
-
382 381
 	changes, err := rows.RowsAffected()
383 382
 	if err != nil {
384 383
 		return -1, err
385 384
 	}
386 385
 
386
+	// Clear who's using this id as parent
387
+	refs, err := tx.Exec("DELETE FROM edge WHERE parent_id = ?;", id)
388
+	if err != nil {
389
+		tx.Rollback()
390
+		return -1, err
391
+	}
392
+	refsCount, err := refs.RowsAffected()
393
+	if err != nil {
394
+		return -1, err
395
+	}
396
+
387 397
 	// Delete entity
388 398
 	if _, err := tx.Exec("DELETE FROM entity where id = ?;", id); err != nil {
389 399
 		tx.Rollback()
... ...
@@ -394,7 +404,7 @@ func (db *Database) Purge(id string) (int, error) {
394 394
 		return -1, err
395 395
 	}
396 396
 
397
-	return int(changes), nil
397
+	return int(changes + refsCount), nil
398 398
 }
399 399
 
400 400
 // Rename an edge for a given path
... ...
@@ -472,8 +472,8 @@ func TestPurgeId(t *testing.T) {
472 472
 
473 473
 	db.Set("/webapp", "1")
474 474
 
475
-	if db.Refs("1") != 1 {
476
-		t.Fatal("Expect reference count to be 1")
475
+	if c := db.Refs("1"); c != 1 {
476
+		t.Fatalf("Expect reference count to be 1, got %d", c)
477 477
 	}
478 478
 
479 479
 	db.Set("/db", "2")
... ...
@@ -484,7 +484,45 @@ func TestPurgeId(t *testing.T) {
484 484
 		t.Fatal(err)
485 485
 	}
486 486
 	if count != 2 {
487
-		t.Fatal("Expected 2 references to be removed")
487
+		t.Fatalf("Expected 2 references to be removed, got %d", count)
488
+	}
489
+}
490
+
491
+// Regression test https://github.com/docker/docker/issues/12334
492
+func TestPurgeIdRefPaths(t *testing.T) {
493
+	db, dbpath := newTestDb(t)
494
+	defer destroyTestDb(dbpath)
495
+
496
+	db.Set("/webapp", "1")
497
+	db.Set("/db", "2")
498
+
499
+	db.Set("/db/webapp", "1")
500
+
501
+	if c := db.Refs("1"); c != 2 {
502
+		t.Fatalf("Expected 2 reference for webapp, got %d", c)
503
+	}
504
+	if c := db.Refs("2"); c != 1 {
505
+		t.Fatalf("Expected 1 reference for db, got %d", c)
506
+	}
507
+
508
+	if rp := db.RefPaths("2"); len(rp) != 1 {
509
+		t.Fatalf("Expected 1 reference path for db, got %d", len(rp))
510
+	}
511
+
512
+	count, err := db.Purge("2")
513
+	if err != nil {
514
+		t.Fatal(err)
515
+	}
516
+
517
+	if count != 2 {
518
+		t.Fatalf("Expected 2 rows to be removed, got %d", count)
519
+	}
520
+
521
+	if c := db.Refs("2"); c != 0 {
522
+		t.Fatalf("Expected 0 reference for db, got %d", c)
523
+	}
524
+	if c := db.Refs("1"); c != 1 {
525
+		t.Fatalf("Expected 1 reference for webapp, got %d", c)
488 526
 	}
489 527
 }
490 528