Skip to content

Commit

Permalink
[internal/database] GetCorrID needs to accept multiple rows as result
Browse files Browse the repository at this point in the history
QueryRow selects the first result returned, but the file_event_log always will return multiple rows. This is due to the `register_file` function  in our DB schema that stores the first log event without any correlation ID.
  • Loading branch information
jbygdell committed Dec 30, 2024
1 parent dac21cf commit d56a34b
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions sda/internal/database/db_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"database/sql"
"encoding/hex"
"errors"
"fmt"
"math"
"strings"
"time"
Expand Down Expand Up @@ -725,15 +726,29 @@ func (dbs *SDAdb) getCorrID(user, path, accession string) (string, error) {
dbs.checkAndReconnectIfNeeded()
db := dbs.DB
const query = "SELECT DISTINCT correlation_id FROM sda.file_event_log e " +
"RIGHT JOIN sda.files f ON e.file_id = f.id WHERE f.submission_file_path = $1 AND f.submission_user = $2 AND COALESCE(f.stable_id, '')= $3;"

var corrID string
err := db.QueryRow(query, path, user, accession).Scan(&corrID)
"RIGHT JOIN sda.files f ON e.file_id = f.id " +
"WHERE f.submission_file_path = $1 AND f.submission_user = $2 AND COALESCE(f.stable_id, '') = $3;"
rows, err := db.Query(query, path, user, accession)
if err != nil {
return "", err
}
if rows.Err() != nil {
return "", rows.Err()
}
defer rows.Close()

var corrID sql.NullString
for rows.Next() {
err := rows.Scan(&corrID)
if err != nil {
return "", err
}
if corrID.Valid {
return corrID.String, nil
}
}

return corrID, nil
return "", fmt.Errorf("sql: no rows in result set")
}

// list all users with files not yet assigned to a dataset
Expand Down

0 comments on commit d56a34b

Please sign in to comment.