Sort Table slice to ensure stable template output
This commit is contained in:
parent
070df18197
commit
e22d6cf77b
2 changed files with 15 additions and 1 deletions
|
@ -1,7 +1,11 @@
|
||||||
// Package bdb supplies the sql(b)oiler (d)ata(b)ase abstractions.
|
// Package bdb supplies the sql(b)oiler (d)ata(b)ase abstractions.
|
||||||
package bdb
|
package bdb
|
||||||
|
|
||||||
import "github.com/pkg/errors"
|
import (
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
// Interface for a database driver. Functionality required to support a specific
|
// Interface for a database driver. Functionality required to support a specific
|
||||||
// database type (eg, MySQL, Postgres etc.)
|
// database type (eg, MySQL, Postgres etc.)
|
||||||
|
@ -45,6 +49,8 @@ func Tables(db Interface, schema string, whitelist, blacklist []string) ([]Table
|
||||||
return nil, errors.Wrap(err, "unable to get table names")
|
return nil, errors.Wrap(err, "unable to get table names")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sort.Strings(names)
|
||||||
|
|
||||||
var tables []Table
|
var tables []Table
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
t := Table{
|
t := Table{
|
||||||
|
|
|
@ -124,6 +124,14 @@ func TestTables(t *testing.T) {
|
||||||
t.Errorf("Expected len 7, got: %d\n", len(tables))
|
t.Errorf("Expected len 7, got: %d\n", len(tables))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prev := ""
|
||||||
|
for i := range tables {
|
||||||
|
if prev >= tables[i].Name {
|
||||||
|
t.Error("tables are not sorted")
|
||||||
|
}
|
||||||
|
prev = tables[i].Name
|
||||||
|
}
|
||||||
|
|
||||||
pilots := GetTable(tables, "pilots")
|
pilots := GetTable(tables, "pilots")
|
||||||
if len(pilots.Columns) != 2 {
|
if len(pilots.Columns) != 2 {
|
||||||
t.Error()
|
t.Error()
|
||||||
|
|
Loading…
Reference in a new issue