Skip to content

Commit 4b4c7ba

Browse files
committed
Updates
1 parent 94b6971 commit 4b4c7ba

File tree

14 files changed

+54
-27
lines changed

14 files changed

+54
-27
lines changed

backend/embed/migrations/postgres/20201013035318_initial_schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ CREATE TABLE "upstream" (
139139
"created_at" BIGINT NOT NULL DEFAULT 0,
140140
"updated_at" BIGINT NOT NULL DEFAULT 0,
141141
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
142-
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
142+
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
143143
"name" VARCHAR(50) NOT NULL,
144144
"nginx_template_id" INTEGER NOT NULL REFERENCES "nginx_template"("id") ON DELETE CASCADE,
145145
"ip_hash" BOOLEAN NOT NULL DEFAULT FALSE,

backend/internal/database/helpers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
package database
22

3+
import (
4+
"fmt"
5+
"npm/internal/config"
6+
"strings"
7+
)
8+
39
const (
410
// DateFormat for DateFormat
511
DateFormat = "2006-01-02"
612
// DateTimeFormat for DateTimeFormat
713
DateTimeFormat = "2006-01-02T15:04:05"
814
)
15+
16+
// QuoteTableName is a special function that will quote a table
17+
// name based on the driver. Gorm normally handles this but this
18+
// is for special cases where we run raw sql
19+
func QuoteTableName(tbl string) string {
20+
switch strings.ToLower(config.Configuration.DB.Driver) {
21+
case config.DatabasePostgres:
22+
return fmt.Sprintf(`"%s"`, tbl)
23+
default:
24+
// This is the same for Mysql and Sqlite
25+
return fmt.Sprintf("`%s`", tbl)
26+
}
27+
}

backend/internal/entity/accesslist/methods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
2121
Direction: "ASC",
2222
}
2323

24-
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
24+
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
2525

2626
// Get count of items in this search
2727
var totalRows int64
@@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
3131

3232
// Get rows
3333
items := make([]Model, 0)
34-
if res := dbo.Find(&items); res.Error != nil {
34+
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
3535
return result, res.Error
3636
}
3737

backend/internal/entity/certificate/methods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
3737
Direction: "ASC",
3838
}
3939

40-
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
40+
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
4141

4242
// Get count of items in this search
4343
var totalRows int64
@@ -47,7 +47,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
4747

4848
// Get rows
4949
items := make([]Model, 0)
50-
if res := dbo.Find(&items); res.Error != nil {
50+
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
5151
return result, res.Error
5252
}
5353

backend/internal/entity/certificateauthority/methods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
2121
Direction: "ASC",
2222
}
2323

24-
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
24+
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
2525

2626
// Get count of items in this search
2727
var totalRows int64
@@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
3131

3232
// Get rows
3333
items := make([]Model, 0)
34-
if res := dbo.Find(&items); res.Error != nil {
34+
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
3535
return result, res.Error
3636
}
3737

backend/internal/entity/dnsprovider/methods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
2121
Direction: "ASC",
2222
}
2323

24-
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
24+
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
2525

2626
// Get count of items in this search
2727
var totalRows int64
@@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
3131

3232
// Get rows
3333
items := make([]Model, 0)
34-
if res := dbo.Find(&items); res.Error != nil {
34+
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
3535
return result, res.Error
3636
}
3737

backend/internal/entity/host/methods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
2323
Direction: "ASC",
2424
}
2525

26-
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
26+
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
2727

2828
// Get count of items in this search
2929
var totalRows int64
@@ -33,7 +33,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
3333

3434
// Get rows
3535
items := make([]Model, 0)
36-
if res := dbo.Find(&items); res.Error != nil {
36+
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
3737
return result, res.Error
3838
}
3939

backend/internal/entity/lists.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,21 @@ type ListResponse struct {
2525
// ListQueryBuilder is used to setup queries for lists
2626
func ListQueryBuilder(
2727
pageInfo *model.PageInfo,
28-
defaultSort model.Sort,
2928
filters []model.Filter,
3029
filterMap map[string]filterMapValue,
3130
) *gorm.DB {
3231
scopes := make([]func(*gorm.DB) *gorm.DB, 0)
33-
scopes = append(scopes, ScopeOrderBy(pageInfo, defaultSort))
3432
scopes = append(scopes, ScopeOffsetLimit(pageInfo))
3533
scopes = append(scopes, ScopeFilters(filters, filterMap))
3634
return database.GetDB().Scopes(scopes...)
3735
}
36+
37+
// AddOrderToList is used after query above is used for counting
38+
// Postgres in particular doesn't like count(*) when ordering at the same time
39+
func AddOrderToList(
40+
dbo *gorm.DB,
41+
pageInfo *model.PageInfo,
42+
defaultSort model.Sort,
43+
) *gorm.DB {
44+
return dbo.Scopes(ScopeOrderBy(pageInfo, defaultSort))
45+
}

backend/internal/entity/nginxtemplate/methods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
2121
Direction: "ASC",
2222
}
2323

24-
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
24+
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
2525

2626
// Get count of items in this search
2727
var totalRows int64
@@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
3131

3232
// Get rows
3333
items := make([]Model, 0)
34-
if res := dbo.Find(&items); res.Error != nil {
34+
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
3535
return result, res.Error
3636
}
3737

backend/internal/entity/setting/methods.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
3030
Direction: "ASC",
3131
}
3232

33-
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
33+
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
3434

3535
// Get count of items in this search
3636
var totalRows int64
@@ -40,7 +40,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
4040

4141
// Get rows
4242
items := make([]Model, 0)
43-
if res := dbo.Find(&items); res.Error != nil {
43+
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
4444
return result, res.Error
4545
}
4646

0 commit comments

Comments
 (0)