From 9a78731a8dd820ae00f101fcb19a38ce5b4f13da Mon Sep 17 00:00:00 2001 From: Joakim Bygdell Date: Sat, 21 Dec 2024 13:23:36 +0100 Subject: [PATCH] [internal/database] GetCorrID needs to accept multiple rows as result 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. --- sda/internal/database/db_functions.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/sda/internal/database/db_functions.go b/sda/internal/database/db_functions.go index ea7a39344..c8ff91391 100644 --- a/sda/internal/database/db_functions.go +++ b/sda/internal/database/db_functions.go @@ -725,15 +725,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 { + break + } + } - return corrID, nil + return corrID.String, nil } // list all users with files not yet assigned to a dataset