From e22d6cf77bf1f263e2af0b9688b2a8553eb71691 Mon Sep 17 00:00:00 2001 From: guns Date: Wed, 3 May 2017 03:53:56 -0500 Subject: [PATCH] Sort Table slice to ensure stable template output --- bdb/interface.go | 8 +++++++- bdb/interface_test.go | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/bdb/interface.go b/bdb/interface.go index dfb33bc..2fe7213 100644 --- a/bdb/interface.go +++ b/bdb/interface.go @@ -1,7 +1,11 @@ // Package bdb supplies the sql(b)oiler (d)ata(b)ase abstractions. package bdb -import "github.com/pkg/errors" +import ( + "sort" + + "github.com/pkg/errors" +) // Interface for a database driver. Functionality required to support a specific // 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") } + sort.Strings(names) + var tables []Table for _, name := range names { t := Table{ diff --git a/bdb/interface_test.go b/bdb/interface_test.go index 4d429c6..e0d4cab 100644 --- a/bdb/interface_test.go +++ b/bdb/interface_test.go @@ -124,6 +124,14 @@ func TestTables(t *testing.T) { 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") if len(pilots.Columns) != 2 { t.Error()