mirror of
https://github.com/turbot/steampipe.git
synced 2025-12-25 03:00:48 -05:00
Fix query batch mode returning zero exit code when rows return errors. Closes #3044
This commit is contained in:
@@ -53,7 +53,7 @@ func RunBatchSession(ctx context.Context, initData *query.InitData) (int, error)
|
||||
// if we have resolved any queries, run them
|
||||
failures = executeQueries(ctx, initData)
|
||||
}
|
||||
// return the number of failures
|
||||
// return the number of query failures and the number of rows that returned errors
|
||||
return failures, nil
|
||||
}
|
||||
|
||||
@@ -61,16 +61,19 @@ func executeQueries(ctx context.Context, initData *query.InitData) int {
|
||||
utils.LogTime("queryexecute.executeQueries start")
|
||||
defer utils.LogTime("queryexecute.executeQueries end")
|
||||
|
||||
// run all queries
|
||||
// failures return the number of queries that failed and also the number of rows that
|
||||
// returned errors
|
||||
failures := 0
|
||||
t := time.Now()
|
||||
// build ordered list of queries
|
||||
// (ordered for testing repeatability)
|
||||
var queryNames = utils.SortedMapKeys(initData.Queries)
|
||||
var err error
|
||||
|
||||
for i, name := range queryNames {
|
||||
q := initData.Queries[name]
|
||||
if err := executeQuery(ctx, initData.Client, q); err != nil {
|
||||
// if executeQuery fails it returns err, else it returns the number of rows that returned errors while execution
|
||||
if err, failures = executeQuery(ctx, initData.Client, q); err != nil {
|
||||
failures++
|
||||
error_helpers.ShowWarning(fmt.Sprintf("executeQueries: query %d of %d failed: %v", i+1, len(queryNames), error_helpers.DecodePgError(err)))
|
||||
// if timing flag is enabled, show the time taken for the query to fail
|
||||
@@ -88,23 +91,24 @@ func executeQueries(ctx context.Context, initData *query.InitData) int {
|
||||
return failures
|
||||
}
|
||||
|
||||
func executeQuery(ctx context.Context, client db_common.Client, resolvedQuery *modconfig.ResolvedQuery) error {
|
||||
func executeQuery(ctx context.Context, client db_common.Client, resolvedQuery *modconfig.ResolvedQuery) (error, int) {
|
||||
utils.LogTime("query.execute.executeQuery start")
|
||||
defer utils.LogTime("query.execute.executeQuery end")
|
||||
|
||||
// the db executor sends result data over resultsStreamer
|
||||
resultsStreamer, err := db_common.ExecuteQuery(ctx, client, resolvedQuery.ExecuteSQL, resolvedQuery.Args...)
|
||||
if err != nil {
|
||||
return err
|
||||
return err, 0
|
||||
}
|
||||
|
||||
rowErrors := 0 // get the number of rows that returned an error
|
||||
// print the data as it comes
|
||||
for r := range resultsStreamer.Results {
|
||||
display.ShowOutput(ctx, r, display.ShowTimingOnOutput(constants.OutputFormatTable))
|
||||
rowErrors = display.ShowOutput(ctx, r, display.ShowTimingOnOutput(constants.OutputFormatTable))
|
||||
// signal to the resultStreamer that we are done with this result
|
||||
resultsStreamer.AllResultsRead()
|
||||
}
|
||||
return nil
|
||||
return nil, rowErrors
|
||||
}
|
||||
|
||||
// if we are displaying csv with no header, do not include lines between the query results
|
||||
|
||||
Reference in New Issue
Block a user