sqlite: LookupVolume: fix partial name match

A partial name match is tricky as we want it to be fast but also make
sure there's only one partial match iff there's no full one.

[NO NEW TESTS NEEDED] as it fixes a system test.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2023-02-28 10:43:37 +01:00
parent 01359457c4
commit df88f546b6

View File

@ -2046,24 +2046,25 @@ func (s *SQLiteState) LookupVolume(name string) (*Volume, error) {
return nil, define.ErrDBClosed return nil, define.ErrDBClosed
} }
rows, err := s.conn.Query("SELECT JSON FROM VolumeConfig WHERE Name LIKE ?;", name+"%") rows, err := s.conn.Query("SELECT Name, JSON FROM VolumeConfig WHERE Name LIKE ? ORDER BY LENGTH(Name) ASC;", name+"%")
if err != nil { if err != nil {
return nil, fmt.Errorf("querying database for volume %s: %w", name, err) return nil, fmt.Errorf("querying database for volume %s: %w", name, err)
} }
defer rows.Close() defer rows.Close()
var configJSON string var foundName, configJSON string
foundResult := false
for rows.Next() { for rows.Next() {
if foundResult { if foundName != "" {
return nil, fmt.Errorf("more than one result for volume name %s: %w", name, define.ErrVolumeExists) return nil, fmt.Errorf("more than one result for volume name %s: %w", name, define.ErrVolumeExists)
} }
if err := rows.Scan(&configJSON); err != nil { if err := rows.Scan(&foundName, &configJSON); err != nil {
return nil, fmt.Errorf("retrieving volume %s config from database: %w", name, err) return nil, fmt.Errorf("retrieving volume %s config from database: %w", name, err)
} }
foundResult = true if foundName == name {
break
} }
if !foundResult { }
if foundName == "" {
return nil, fmt.Errorf("no volume with name %q found: %w", name, define.ErrNoSuchVolume) return nil, fmt.Errorf("no volume with name %q found: %w", name, define.ErrNoSuchVolume)
} }