This commit is contained in:
Jamie Curnow 2023-05-31 13:52:58 +10:00
parent 94b6971ef7
commit 4b4c7baea4
No known key found for this signature in database
GPG Key ID: FFBB624C43388E9E
14 changed files with 54 additions and 27 deletions

View File

@ -139,7 +139,7 @@ CREATE TABLE "upstream" (
"created_at" BIGINT NOT NULL DEFAULT 0,
"updated_at" BIGINT NOT NULL DEFAULT 0,
"is_deleted" INTEGER NOT NULL DEFAULT 0, -- int on purpose, gormism
"user_id" INTEGER NOT NULL REFERENCES "user"("id"),
"user_id" INTEGER NOT NULL REFERENCES "user"("id") ON DELETE CASCADE,
"name" VARCHAR(50) NOT NULL,
"nginx_template_id" INTEGER NOT NULL REFERENCES "nginx_template"("id") ON DELETE CASCADE,
"ip_hash" BOOLEAN NOT NULL DEFAULT FALSE,

View File

@ -1,8 +1,27 @@
package database
import (
"fmt"
"npm/internal/config"
"strings"
)
const (
// DateFormat for DateFormat
DateFormat = "2006-01-02"
// DateTimeFormat for DateTimeFormat
DateTimeFormat = "2006-01-02T15:04:05"
)
// QuoteTableName is a special function that will quote a table
// name based on the driver. Gorm normally handles this but this
// is for special cases where we run raw sql
func QuoteTableName(tbl string) string {
switch strings.ToLower(config.Configuration.DB.Driver) {
case config.DatabasePostgres:
return fmt.Sprintf(`"%s"`, tbl)
default:
// This is the same for Mysql and Sqlite
return fmt.Sprintf("`%s`", tbl)
}
}

View File

@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -37,7 +37,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -47,7 +47,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -23,7 +23,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -33,7 +33,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -25,13 +25,21 @@ type ListResponse struct {
// ListQueryBuilder is used to setup queries for lists
func ListQueryBuilder(
pageInfo *model.PageInfo,
defaultSort model.Sort,
filters []model.Filter,
filterMap map[string]filterMapValue,
) *gorm.DB {
scopes := make([]func(*gorm.DB) *gorm.DB, 0)
scopes = append(scopes, ScopeOrderBy(pageInfo, defaultSort))
scopes = append(scopes, ScopeOffsetLimit(pageInfo))
scopes = append(scopes, ScopeFilters(filters, filterMap))
return database.GetDB().Scopes(scopes...)
}
// AddOrderToList is used after query above is used for counting
// Postgres in particular doesn't like count(*) when ordering at the same time
func AddOrderToList(
dbo *gorm.DB,
pageInfo *model.PageInfo,
defaultSort model.Sort,
) *gorm.DB {
return dbo.Scopes(ScopeOrderBy(pageInfo, defaultSort))
}

View File

@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -30,7 +30,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -40,7 +40,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -21,7 +21,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -31,7 +31,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -30,7 +30,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -40,7 +40,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter) (entity.ListResponse,
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}

View File

@ -1,6 +1,7 @@
package user
import (
"fmt"
"npm/internal/database"
"npm/internal/entity"
"npm/internal/logger"
@ -41,7 +42,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
Direction: "ASC",
}
dbo := entity.ListQueryBuilder(&pageInfo, defaultSort, filters, entity.GetFilterMap(Model{}, true))
dbo := entity.ListQueryBuilder(&pageInfo, filters, entity.GetFilterMap(Model{}, true))
// Get count of items in this search
var totalRows int64
@ -51,7 +52,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
// Get rows
items := make([]Model, 0)
if res := dbo.Find(&items); res.Error != nil {
if res := entity.AddOrderToList(dbo, &pageInfo, defaultSort).Find(&items); res.Error != nil {
return result, res.Error
}
@ -84,8 +85,7 @@ func List(pageInfo model.PageInfo, filters []model.Filter, expand []string) (ent
func DeleteAll() error {
db := database.GetDB()
// nolint errcheck
db.Exec("DELETE FROM auth")
result := db.Exec("DELETE FROM user WHERE is_system = ?", false)
result := db.Exec(fmt.Sprintf(`DELETE FROM %s WHERE is_system = ?`, database.QuoteTableName("user")), false)
return result.Error
}