Skip to content

Commit 799ac5f

Browse files
authored
Merge pull request #167 from mraerino/fix/table-prefixes-in-joins
Fix table prefixes in joins with orders
2 parents 6226df2 + 6885d7c commit 799ac5f

File tree

9 files changed

+28
-10
lines changed

9 files changed

+28
-10
lines changed

api/download.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (a *API) DownloadList(w http.ResponseWriter, r *http.Request) error {
110110
orderTable := a.db.NewScope(models.Order{}).QuotedTableName()
111111
downloadsTable := a.db.NewScope(models.Download{}).QuotedTableName()
112112

113-
query := a.db.Joins("join " + orderTable + " as orders ON " + downloadsTable + ".order_id = " + orderTable + ".id and " + orderTable + ".payment_state = 'paid'")
113+
query := a.db.Joins("join " + orderTable + " ON " + downloadsTable + ".order_id = " + orderTable + ".id and " + orderTable + ".payment_state = 'paid'")
114114
if order != nil {
115115
query = query.Where(orderTable+".id = ?", order.ID)
116116
} else {

api/reports.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (a *API) ProductsReport(w http.ResponseWriter, r *http.Request) error {
6363
query := a.db.
6464
Model(&models.LineItem{}).
6565
Select("sku, path, sum(quantity * price) as total, currency").
66-
Joins("JOIN " + ordersTable + " as orders " + "ON orders.id = " + itemsTable + ".order_id " + "AND orders.payment_state = 'paid'").
66+
Joins("JOIN " + ordersTable + " ON " + ordersTable + ".id = " + itemsTable + ".order_id " + "AND " + ordersTable + ".payment_state = 'paid'").
6767
Group("sku, path, currency").
6868
Order("total desc")
6969

api/reports_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,22 @@ func TestSalesReport(t *testing.T) {
2424
assert.Equal(t, uint64(2), row.Orders)
2525
})
2626
}
27+
28+
func TestProductsReport(t *testing.T) {
29+
test := NewRouteTest(t)
30+
token := testAdminToken("admin-yo", "[email protected]")
31+
recorder := test.TestEndpoint(http.MethodGet, "/reports/products", nil, token)
32+
33+
report := []productsRow{}
34+
extractPayload(t, http.StatusOK, recorder, &report)
35+
assert.Len(t, report, 3)
36+
prod1 := report[0]
37+
assert.Equal(t, "234-fancy-belts", prod1.Sku)
38+
assert.Equal(t, uint64(45), prod1.Total)
39+
prod2 := report[1]
40+
assert.Equal(t, "123-i-can-fly-456", prod2.Sku)
41+
assert.Equal(t, uint64(24), prod2.Total)
42+
prod3 := report[2]
43+
assert.Equal(t, "456-i-rollover-all-things", prod3.Sku)
44+
assert.Equal(t, uint64(10), prod3.Total)
45+
}

api/test.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ GOCOMMERCE_JWT_AUD=api.netlify.com
44
GOCOMMERCE_DB_DRIVER=sqlite3
55
GOCOMMERCE_DB_AUTOMIGRATE=true
66
GOCOMMERCE_DB_DATABASE_URL=test.db
7+
GOCOMMERCE_DB_NAMESPACE=test
78
GOCOMMERCE_API_HOST=localhost
89
PORT=9999
910
GOCOMMERCE_LOG_LEVEL=error

api/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (a *API) UserList(w http.ResponseWriter, r *http.Request) error {
9191
orderTable := a.db.NewScope(models.Order{}).QuotedTableName()
9292
userTable := a.db.NewScope(models.User{}).QuotedTableName()
9393
query = query.
94-
Joins("LEFT JOIN " + orderTable + " as orders ON " + userTable + ".id = " + orderTable + ".user_id").
94+
Joins("LEFT JOIN " + orderTable + " ON " + userTable + ".id = " + orderTable + ".user_id").
9595
Group(userTable + ".id")
9696

9797
instanceID := gcontext.GetInstanceID(r.Context())

api/utils_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func testConfig() (*conf.GlobalConfiguration, *conf.Configuration) {
6969

7070
globalConfig := new(conf.GlobalConfiguration)
7171
globalConfig.DB.Automigrate = true
72+
globalConfig.DB.Namespace = "test"
7273

7374
config := new(conf.Configuration)
7475
config.JWT.Secret = "testsecret"

cmd/multi_cmd.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ func multi(cmd *cobra.Command, args []string) {
2525
if globalConfig.OperatorToken == "" {
2626
logrus.Fatal("Operator token secret is required")
2727
}
28-
if globalConfig.DB.Namespace != "" {
29-
models.Namespace = globalConfig.DB.Namespace
30-
}
3128

3229
db, err := models.Connect(globalConfig)
3330
if err != nil {

cmd/root_cmd.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"github.com/spf13/cobra"
66

77
"github.com/netlify/gocommerce/conf"
8-
"github.com/netlify/gocommerce/models"
98
)
109

1110
var configFile = ""
@@ -36,8 +35,5 @@ func execWithConfig(cmd *cobra.Command, fn func(globalConfig *conf.GlobalConfigu
3635
logrus.Fatalf("Failed to load configuration: %+v", err)
3736
}
3837

39-
if globalConfig.DB.Namespace != "" {
40-
models.Namespace = globalConfig.DB.Namespace
41-
}
4238
fn(globalConfig, config)
4339
}

models/connection.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ var Namespace string
2222

2323
// Connect will connect to that storage engine
2424
func Connect(config *conf.GlobalConfiguration) (*gorm.DB, error) {
25+
if config.DB.Namespace != "" {
26+
Namespace = config.DB.Namespace
27+
}
28+
2529
if config.DB.Dialect == "" {
2630
config.DB.Dialect = config.DB.Driver
2731
}

0 commit comments

Comments
 (0)