From dafcead48615c4efe7548086b2d983726aeef56f Mon Sep 17 00:00:00 2001 From: Marcus Weiner Date: Fri, 25 Jan 2019 14:37:37 +0100 Subject: [PATCH 1/3] Move namespace configuration into connection creator This allows testing db namespaces in tests --- cmd/multi_cmd.go | 3 --- cmd/root_cmd.go | 4 ---- models/connection.go | 4 ++++ 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/cmd/multi_cmd.go b/cmd/multi_cmd.go index 286defc..34d1d8b 100644 --- a/cmd/multi_cmd.go +++ b/cmd/multi_cmd.go @@ -25,9 +25,6 @@ func multi(cmd *cobra.Command, args []string) { if globalConfig.OperatorToken == "" { logrus.Fatal("Operator token secret is required") } - if globalConfig.DB.Namespace != "" { - models.Namespace = globalConfig.DB.Namespace - } db, err := models.Connect(globalConfig) if err != nil { diff --git a/cmd/root_cmd.go b/cmd/root_cmd.go index 93dc3f2..eb479fc 100644 --- a/cmd/root_cmd.go +++ b/cmd/root_cmd.go @@ -5,7 +5,6 @@ import ( "github.com/spf13/cobra" "github.com/netlify/gocommerce/conf" - "github.com/netlify/gocommerce/models" ) var configFile = "" @@ -36,8 +35,5 @@ func execWithConfig(cmd *cobra.Command, fn func(globalConfig *conf.GlobalConfigu logrus.Fatalf("Failed to load configuration: %+v", err) } - if globalConfig.DB.Namespace != "" { - models.Namespace = globalConfig.DB.Namespace - } fn(globalConfig, config) } diff --git a/models/connection.go b/models/connection.go index 115b473..648933e 100644 --- a/models/connection.go +++ b/models/connection.go @@ -22,6 +22,10 @@ var Namespace string // Connect will connect to that storage engine func Connect(config *conf.GlobalConfiguration) (*gorm.DB, error) { + if config.DB.Namespace != "" { + Namespace = config.DB.Namespace + } + if config.DB.Dialect == "" { config.DB.Dialect = config.DB.Driver } From 3033540274e5a20ef780930ee33099cbd891f641 Mon Sep 17 00:00:00 2001 From: Marcus Weiner Date: Fri, 25 Jan 2019 15:18:22 +0100 Subject: [PATCH 2/3] Test products report --- api/reports_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/api/reports_test.go b/api/reports_test.go index c967164..fd2a247 100644 --- a/api/reports_test.go +++ b/api/reports_test.go @@ -24,3 +24,22 @@ func TestSalesReport(t *testing.T) { assert.Equal(t, uint64(2), row.Orders) }) } + +func TestProductsReport(t *testing.T) { + test := NewRouteTest(t) + token := testAdminToken("admin-yo", "admin@wayneindustries.com") + recorder := test.TestEndpoint(http.MethodGet, "/reports/products", nil, token) + + report := []productsRow{} + extractPayload(t, http.StatusOK, recorder, &report) + assert.Len(t, report, 3) + prod1 := report[0] + assert.Equal(t, "234-fancy-belts", prod1.Sku) + assert.Equal(t, uint64(45), prod1.Total) + prod2 := report[1] + assert.Equal(t, "123-i-can-fly-456", prod2.Sku) + assert.Equal(t, uint64(24), prod2.Total) + prod3 := report[2] + assert.Equal(t, "456-i-rollover-all-things", prod3.Sku) + assert.Equal(t, uint64(10), prod3.Total) +} From 6885d7c1fd20b4098ab453b1bd8f3a86c3c9dacf Mon Sep 17 00:00:00 2001 From: Marcus Weiner Date: Fri, 25 Jan 2019 15:18:59 +0100 Subject: [PATCH 3/3] Fix table names on joins when db has a namespace This regression was introduced through parts of these commits: - 8ee1f4d88135f32161cd98a64a6704bb682e0b9b - 1df6447a9809ac909db98fa8d8a323595980194c --- api/download.go | 2 +- api/reports.go | 2 +- api/test.env | 1 + api/user.go | 2 +- api/utils_test.go | 1 + 5 files changed, 5 insertions(+), 3 deletions(-) diff --git a/api/download.go b/api/download.go index cc7e216..564a4cc 100644 --- a/api/download.go +++ b/api/download.go @@ -110,7 +110,7 @@ func (a *API) DownloadList(w http.ResponseWriter, r *http.Request) error { orderTable := a.db.NewScope(models.Order{}).QuotedTableName() downloadsTable := a.db.NewScope(models.Download{}).QuotedTableName() - query := a.db.Joins("join " + orderTable + " as orders ON " + downloadsTable + ".order_id = " + orderTable + ".id and " + orderTable + ".payment_state = 'paid'") + query := a.db.Joins("join " + orderTable + " ON " + downloadsTable + ".order_id = " + orderTable + ".id and " + orderTable + ".payment_state = 'paid'") if order != nil { query = query.Where(orderTable+".id = ?", order.ID) } else { diff --git a/api/reports.go b/api/reports.go index 0231e4d..9494c1c 100644 --- a/api/reports.go +++ b/api/reports.go @@ -63,7 +63,7 @@ func (a *API) ProductsReport(w http.ResponseWriter, r *http.Request) error { query := a.db. Model(&models.LineItem{}). Select("sku, path, sum(quantity * price) as total, currency"). - Joins("JOIN " + ordersTable + " as orders " + "ON orders.id = " + itemsTable + ".order_id " + "AND orders.payment_state = 'paid'"). + Joins("JOIN " + ordersTable + " ON " + ordersTable + ".id = " + itemsTable + ".order_id " + "AND " + ordersTable + ".payment_state = 'paid'"). Group("sku, path, currency"). Order("total desc") diff --git a/api/test.env b/api/test.env index 06f243d..025308d 100644 --- a/api/test.env +++ b/api/test.env @@ -4,6 +4,7 @@ GOCOMMERCE_JWT_AUD=api.netlify.com GOCOMMERCE_DB_DRIVER=sqlite3 GOCOMMERCE_DB_AUTOMIGRATE=true GOCOMMERCE_DB_DATABASE_URL=test.db +GOCOMMERCE_DB_NAMESPACE=test GOCOMMERCE_API_HOST=localhost PORT=9999 GOCOMMERCE_LOG_LEVEL=error diff --git a/api/user.go b/api/user.go index c8adfc8..ad634d8 100644 --- a/api/user.go +++ b/api/user.go @@ -91,7 +91,7 @@ func (a *API) UserList(w http.ResponseWriter, r *http.Request) error { orderTable := a.db.NewScope(models.Order{}).QuotedTableName() userTable := a.db.NewScope(models.User{}).QuotedTableName() query = query. - Joins("LEFT JOIN " + orderTable + " as orders ON " + userTable + ".id = " + orderTable + ".user_id"). + Joins("LEFT JOIN " + orderTable + " ON " + userTable + ".id = " + orderTable + ".user_id"). Group(userTable + ".id") instanceID := gcontext.GetInstanceID(r.Context()) diff --git a/api/utils_test.go b/api/utils_test.go index 5c53048..55f0681 100644 --- a/api/utils_test.go +++ b/api/utils_test.go @@ -69,6 +69,7 @@ func testConfig() (*conf.GlobalConfiguration, *conf.Configuration) { globalConfig := new(conf.GlobalConfiguration) globalConfig.DB.Automigrate = true + globalConfig.DB.Namespace = "test" config := new(conf.Configuration) config.JWT.Secret = "testsecret"