Skip to content

Commit

Permalink
chore: fix query api
Browse files Browse the repository at this point in the history
  • Loading branch information
moshloop committed Apr 7, 2022
1 parent f1d9266 commit 8876f25
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 10 additions & 0 deletions db/migrations/5_jsonb_type.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- +goose Up
-- +goose StatementBegin
---

ALTER TABLE config_item ALTER COLUMN config SET DATA TYPE jsonb USING config::jsonb;
ALTER TABLE config_scraper ALTER COLUMN spec SET DATA TYPE jsonb USING spec::jsonb;
ALTER TABLE config_change ALTER COLUMN patches SET DATA TYPE jsonb USING patches::jsonb;
ALTER TABLE config_analysis ALTER COLUMN analysis SET DATA TYPE jsonb USING analysis::jsonb;

-- +goose StatementEnd
10 changes: 3 additions & 7 deletions db/repository/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (d *DBRepo) CreateConfigChange(cc *models.ConfigChange) error {

func (d *DBRepo) QueryConfigItems(request v1.QueryRequest) (*v1.QueryResult, error) {
results := d.db.Raw(request.Query)
logger.Debugf(request.Query)
logger.Tracef(request.Query)
if results.Error != nil {
return nil, fmt.Errorf("failed to parse query: %s -> %s", request.Query, results.Error)
}
Expand All @@ -77,19 +77,15 @@ func (d *DBRepo) QueryConfigItems(request v1.QueryRequest) (*v1.QueryResult, err
if err != nil {
logger.Errorf("failed to get column details: %v", err)
}
rows.Next()
if err := results.ScanRows(rows, &response.Results); err != nil {
return nil, fmt.Errorf("failed to run query: %s -> %s", request.Query, err)
return nil, fmt.Errorf("failed to scan rows: %s -> %s", request.Query, err)
}
for _, col := range columns {
response.Columns = append(response.Columns, v1.QueryColumn{
Name: col,
})
}

if len(response.Results) > 0 && isRowEmpty(response.Results[0]) {
//FIXME: why is an empty first row returned in the first place?
response.Results = response.Results[1:]
}
}
response.Count = len(response.Results)
return &response, nil
Expand Down

0 comments on commit 8876f25

Please sign in to comment.