Skip to content

Commit

Permalink
fix(dashboard): sql error with custom table prefix in page search
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Sep 4, 2024
1 parent 90d8503 commit a3c6d20
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 11 deletions.
13 changes: 12 additions & 1 deletion internal/dao/query.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package dao

import "github.com/ArtalkJS/Artalk/internal/entity"
import (
"github.com/ArtalkJS/Artalk/internal/entity"
"gorm.io/gorm"
)

func (dao *Dao) GetUserAllCommentIDs(userID uint) []uint {
userAllCommentIDs := []uint{}
dao.DB().Model(&entity.Comment{}).Select("id").Where("user_id = ?", userID).Find(&userAllCommentIDs)
return userAllCommentIDs
}

// Get the table name of the entity
func (dao *Dao) GetTableName(entity any) string {
// @see https://github.com/go-gorm/gorm/issues/3603#issuecomment-709883403
stmt := &gorm.Statement{DB: dao.DB()}
stmt.Parse(entity)
return stmt.Schema.Table
}
17 changes: 17 additions & 0 deletions internal/dao/query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dao_test

import (
"testing"

"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/ArtalkJS/Artalk/test"
"github.com/stretchr/testify/assert"
)

func TestGetTableName(t *testing.T) {
app, _ := test.NewTestApp()
defer app.Cleanup()

assert.Equal(t, "atk_pages", app.Dao().GetTableName(&entity.Page{}))
assert.Equal(t, "atk_comments", app.Dao().GetTableName(&entity.Comment{}))
}
2 changes: 1 addition & 1 deletion internal/entity/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Page struct {
//
// And must caution that the db query statement quoted the field name `key` with backticks.
// Different db may have different rules. The pgsql is not backticks, but double quotes.
// So use the pages.key (without any quotes) to instead of `key`.
// So use the pages.key (without any quotes) to instead of `key` (Mind the prefix table name).
//
// Consider to rename this column and make a db migration in the future.
Key string `gorm:"index;size:255"` // Page key
Expand Down
13 changes: 9 additions & 4 deletions server/handler/page_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,15 @@ func PageList(app *core.App, router fiber.Router) {
}

// Search
q = q.Scopes(func(d *gorm.DB) *gorm.DB {
return d.Where("LOWER(pages.key) LIKE LOWER(?) OR LOWER(title) LIKE LOWER(?)",
"%"+p.Search+"%", "%"+p.Search+"%")
})
if p.Search != "" {
q = q.Scopes(func(d *gorm.DB) *gorm.DB {
// Because historical reasons, the naming of this field named `key` does not follow best practices.
// In some database, directly use the field name `key` will cause an error.
// So must keep the table name before the field name.
return d.Where("LOWER("+app.Dao().GetTableName(&entity.Page{})+".key) LIKE LOWER(?) OR LOWER(title) LIKE LOWER(?)",
"%"+p.Search+"%", "%"+p.Search+"%")
})
}

// Total count
var total int64
Expand Down
10 changes: 6 additions & 4 deletions server/handler/user_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ func UserList(app *core.App, router fiber.Router) {
}

// Search
q = q.Scopes(func(d *gorm.DB) *gorm.DB {
return d.Where("LOWER(name) LIKE LOWER(?) OR LOWER(email) LIKE LOWER(?) OR badge_name = ? OR last_ip = ?",
"%"+p.Search+"%", "%"+p.Search+"%", p.Search, p.Search)
})
if p.Search != "" {
q = q.Scopes(func(d *gorm.DB) *gorm.DB {
return d.Where("LOWER(name) LIKE LOWER(?) OR LOWER(email) LIKE LOWER(?) OR badge_name = ? OR last_ip = ?",
"%"+p.Search+"%", "%"+p.Search+"%", p.Search, p.Search)
})
}

// Total count
var total int64
Expand Down
6 changes: 5 additions & 1 deletion test/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-testfixtures/testfixtures/v3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

type TestApp struct {
Expand Down Expand Up @@ -48,7 +49,10 @@ func NewTestApp() (*TestApp, error) {

// open a sqlite db
dbInstance, err := gorm.Open(sqlite.Open(dbFile), &gorm.Config{
Logger: db_logger.New(),
Logger: db_logger.New(),
NamingStrategy: schema.NamingStrategy{
TablePrefix: "atk_", // Test table prefix, fixture filenames should match this
},
DisableForeignKeyConstraintWhenMigrating: true,
})
if err != nil {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit a3c6d20

Please sign in to comment.