Skip to content

Commit

Permalink
Merge pull request #167 from mraerino/fix/table-prefixes-in-joins
Browse files Browse the repository at this point in the history
Fix table prefixes in joins with orders
  • Loading branch information
bcomnes authored Jan 25, 2019
2 parents 6226df2 + 6885d7c commit 799ac5f
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion api/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion api/reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
19 changes: 19 additions & 0 deletions api/reports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]")
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)
}
1 change: 1 addition & 0 deletions api/test.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion api/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions api/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 0 additions & 3 deletions cmd/multi_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 0 additions & 4 deletions cmd/root_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/spf13/cobra"

"github.com/netlify/gocommerce/conf"
"github.com/netlify/gocommerce/models"
)

var configFile = ""
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions models/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 799ac5f

Please sign in to comment.