This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bad check for error in MetricTableName
Previously, bad error checking logic in MetricTableName could have returned a blank table name on error which eventually led to the following postgres error: "ERROR: zero-length delimited identifier at or near \"\"\"\" (SQLSTATE 42601)" As reported in issue #668 This fixes that and adds defensive checks to avoid blank table name. It's not clear what the underlying error was though.
- Loading branch information
Showing
5 changed files
with
155 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package ingestor | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/timescale/promscale/pkg/pgmodel/model" | ||
) | ||
|
||
func TestMetricTableName(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
tableName string | ||
errExpected bool | ||
sqlQueries []model.SqlQuery | ||
}{ | ||
{ | ||
name: "no error", | ||
tableName: "res1", | ||
sqlQueries: []model.SqlQuery{ | ||
{ | ||
Sql: "SELECT table_name, possibly_new FROM _prom_catalog.get_or_create_metric_table_name($1)", | ||
Args: []interface{}{"t1"}, | ||
Results: model.RowResults{{"res1", true}}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "no error2", | ||
tableName: "res2", | ||
sqlQueries: []model.SqlQuery{ | ||
{ | ||
Sql: "SELECT table_name, possibly_new FROM _prom_catalog.get_or_create_metric_table_name($1)", | ||
Args: []interface{}{"t1"}, | ||
Results: model.RowResults{{"res2", true}}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "error", | ||
tableName: "res1", | ||
errExpected: true, | ||
sqlQueries: []model.SqlQuery{ | ||
{ | ||
Sql: "SELECT table_name, possibly_new FROM _prom_catalog.get_or_create_metric_table_name($1)", | ||
Args: []interface{}{"t1"}, | ||
Err: fmt.Errorf("test"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "empty table name", | ||
tableName: "res2", | ||
errExpected: true, | ||
sqlQueries: []model.SqlQuery{ | ||
{ | ||
Sql: "SELECT table_name, possibly_new FROM _prom_catalog.get_or_create_metric_table_name($1)", | ||
Args: []interface{}{"t1"}, | ||
Results: model.RowResults{{"", true}}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range testCases { | ||
t.Run(c.name, func(t *testing.T) { | ||
mock := model.NewSqlRecorder(c.sqlQueries, t) | ||
|
||
name, _, err := metricTableName(mock, "t1") | ||
require.Equal(t, c.errExpected, err != nil) | ||
|
||
if err == nil { | ||
require.Equal(t, c.tableName, name) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters