Skip to content

Commit

Permalink
fix: listcolumns returns an empty list instead of an error in case th…
Browse files Browse the repository at this point in the history
…e relation doesn't exist (#99)

# Description
handled non existent relation scenario in getColumns

## Linear Ticket
REV-1051

## Security

- [x] The code changed/added as part of this pull request won't create
any security issues with how the software is being used.

---------

Co-authored-by: Vikas Venkatraman <[email protected]>
  • Loading branch information
Vikas26021999 and Vikas Venkatraman authored Jun 12, 2024
1 parent 7c9398c commit bfe30c7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sqlconnect/internal/base/tableadmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,12 @@ func (db *DB) ListColumns(ctx context.Context, relation sqlconnect.RelationRef)
if err := columns.Err(); err != nil {
return nil, fmt.Errorf("iterating list columns for %s: %w", relation.String(), err)
}

// check if relation exists before returning columns
if len(res) == 0 {
return nil, fmt.Errorf("cannot fetch columns for %s: relation does not exist", relation.String())
}

return res, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ func TestDatabaseScenarios(t *testing.T, warehouse string, configJSON json.RawMe
})

t.Run("list columns", func(t *testing.T) {
t.Run("with nonexistent relation", func(t *testing.T) {
nonExistentRelation := sqlconnect.NewRelationRef(formatfn("foobar"), sqlconnect.WithSchema(schema.Name))
_, err := db.ListColumns(ctx, nonExistentRelation)
require.Error(t, err, "it should throw an error when columns are listed for a nonexistent relation")
})

t.Run("with context cancelled", func(t *testing.T) {
_, err := db.ListColumns(cancelledCtx, table)
require.Error(t, err, "it should not be able to list columns with a cancelled context")
Expand Down

0 comments on commit bfe30c7

Please sign in to comment.