Skip to content

Commit

Permalink
Capture row iterating error
Browse files Browse the repository at this point in the history
  • Loading branch information
angelini committed Jul 31, 2024
1 parent 849c166 commit f4ac454
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
10 changes: 10 additions & 0 deletions internal/db/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ func (cl *ContentLookup) Lookup(ctx context.Context, tx pgx.Tx, hashesToLookup m
contents[hash] = value
}
}

err = rows.Err()
if err != nil {
return nil, fmt.Errorf("failed to iterate rows: %w", err)
}
}

return contents, nil
Expand Down Expand Up @@ -262,5 +267,10 @@ func RandomContents(ctx context.Context, conn DbConnector, sample float32) ([]Ha
hashes = append(hashes, hash)
}

err = rows.Err()
if err != nil {
return nil, fmt.Errorf("failed to iterate rows: %w", err)
}

return hashes, nil
}
5 changes: 5 additions & 0 deletions internal/db/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func GcProjectObjects(ctx context.Context, conn DbConnector, project int64, keep
hashes = append(hashes, hash)
}

err = rows.Err()
if err != nil {
return nil, fmt.Errorf("failed to iterate rows: %w", err)
}

return hashes, nil
}

Expand Down
10 changes: 10 additions & 0 deletions internal/db/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func ListProjects(ctx context.Context, tx pgx.Tx) ([]*pb.Project, error) {
projects = append(projects, &pb.Project{Id: id, Version: version})
}

err = rows.Err()
if err != nil {
return nil, fmt.Errorf("failed to iterate rows: %w", err)
}

return projects, nil
}

Expand Down Expand Up @@ -96,6 +101,11 @@ func RandomProjects(ctx context.Context, conn DbConnector, sample float32) ([]in
projects = append(projects, project)
}

err = rows.Err()
if err != nil {
return nil, fmt.Errorf("failed to iterate rows: %w", err)
}

// With only few projects this sometimes returns no results.
if len(projects) > 0 {
break
Expand Down
14 changes: 10 additions & 4 deletions internal/db/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,8 @@ func executeQuery(ctx context.Context, tx pgx.Tx, queryBuilder *queryBuilder) ([
defer rows.Close()

var dbObjects []DbObject
for {
if !rows.Next() {
break
}

for rows.Next() {
var object DbObject

err := rows.Scan(&object.path, &object.mode, &object.size, &object.cached, &object.packed, &object.deleted, &object.hash.H1, &object.hash.H2)
Expand All @@ -136,6 +133,11 @@ func executeQuery(ctx context.Context, tx pgx.Tx, queryBuilder *queryBuilder) ([
dbObjects = append(dbObjects, object)
}

err = rows.Err()
if err != nil {
return nil, fmt.Errorf("failed to iterate rows: %w", err)
}

return dbObjects, nil
}

Expand Down Expand Up @@ -351,6 +353,10 @@ func GetCacheTars(ctx context.Context, tx pgx.Tx) (cacheTarStream, CloseFunc, er

return func() (int64, []byte, *Hash, error) {
if !rows.Next() {
err := rows.Err()
if err != nil {
return 0, nil, nil, fmt.Errorf("failed to iterate rows: %w", err)
}
return 0, nil, nil, io.EOF
}

Expand Down
5 changes: 5 additions & 0 deletions internal/db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func UpdatePackedObjects(ctx context.Context, tx pgx.Tx, conn DbConnector, proje
}
rows.Close()

err = rows.Err()
if err != nil {
return false, fmt.Errorf("failed to iterate rows: %w", err)
}

shouldInsert := true
updated, err := updateObjects(content, updates)
if errors.Is(err, ErrEmptyPack) {
Expand Down
1 change: 1 addition & 0 deletions test/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func latestCacheVersionHashes(tc util.TestCtx) (int64, []db.Hash) {

hashes = append(hashes, hash)
}
require.NoError(tc.T(), rows.Err(), "iterate rows")

return version, hashes
}
Expand Down
2 changes: 2 additions & 0 deletions test/shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ func debugProjects(tc util.TestCtx) {

fmt.Printf("%d,\t%d,\t\t%v\n", id, latestVersion, packPatterns)
}
require.NoError(tc.T(), rows.Err(), "iterate rows")

fmt.Println()
}
Expand Down Expand Up @@ -755,6 +756,7 @@ func debugObjects(tc util.TestCtx) {

fmt.Printf("%d,\t\t%d,\t\t%s,\t\t%s,\t%s,\t%d,\t%v,\t(%x, %x)\n", project, start_version, formatPtr(stop_version), path, formatMode(mode), size, packed, h1, h2)
}
require.NoError(tc.T(), rows.Err(), "iterate rows")

fmt.Println()
}
Expand Down

0 comments on commit f4ac454

Please sign in to comment.