Skip to content

Commit

Permalink
Merge pull request #89 from zbml/master
Browse files Browse the repository at this point in the history
refactor gorm table name
  • Loading branch information
LyricTian authored Aug 5, 2020
2 parents 3a066f3 + 6974bfc commit 6cf4080
Show file tree
Hide file tree
Showing 13 changed files with 15 additions and 53 deletions.
1 change: 1 addition & 0 deletions internal/app/injector/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ func NewGormDB() (*gorm.DB, func(), error) {
MaxIdleConns: cfg.Gorm.MaxIdleConns,
MaxLifetime: cfg.Gorm.MaxLifetime,
MaxOpenConns: cfg.Gorm.MaxOpenConns,
TablePrefix: cfg.Gorm.TablePrefix,
})
}
5 changes: 0 additions & 5 deletions internal/app/model/impl/gorm/entity/e_demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ type Demo struct {
Creator string `gorm:"column:creator;size:36;"` // 创建者
}

// TableName 表名
func (a Demo) TableName() string {
return a.Model.TableName("demo")
}

// ToSchemaDemo 转换为demo对象
func (a Demo) ToSchemaDemo() *schema.Demo {
item := new(schema.Demo)
Expand Down
5 changes: 0 additions & 5 deletions internal/app/model/impl/gorm/entity/e_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ type Menu struct {
Creator string `gorm:"column:creator;size:36;"` // 创建人
}

// TableName 表名
func (a Menu) TableName() string {
return a.Model.TableName("menu")
}

// ToSchemaMenu 转换为菜单对象
func (a Menu) ToSchemaMenu() *schema.Menu {
item := new(schema.Menu)
Expand Down
5 changes: 0 additions & 5 deletions internal/app/model/impl/gorm/entity/e_menu_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ type MenuAction struct {
Name string `gorm:"column:name;size:100;default:'';not null;"` // 动作名称
}

// TableName 表名
func (a MenuAction) TableName() string {
return a.Model.TableName("menu_action")
}

// ToSchemaMenuAction 转换为菜单动作对象
func (a MenuAction) ToSchemaMenuAction() *schema.MenuAction {
item := new(schema.MenuAction)
Expand Down
5 changes: 0 additions & 5 deletions internal/app/model/impl/gorm/entity/e_menu_action_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ type MenuActionResource struct {
Path string `gorm:"column:path;size:100;default:'';not null;"` // 资源请求路径(支持/:id匹配)
}

// TableName 表名
func (a MenuActionResource) TableName() string {
return a.Model.TableName("menu_action_resource")
}

// ToSchemaMenuActionResource 转换为菜单动作关联资源对象
func (a MenuActionResource) ToSchemaMenuActionResource() *schema.MenuActionResource {
item := new(schema.MenuActionResource)
Expand Down
5 changes: 0 additions & 5 deletions internal/app/model/impl/gorm/entity/e_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ type Role struct {
Creator string `gorm:"column:creator;size:36;"` // 创建者
}

// TableName 表名
func (a Role) TableName() string {
return a.Model.TableName("role")
}

// ToSchemaRole 转换为角色对象
func (a Role) ToSchemaRole() *schema.Role {
item := new(schema.Role)
Expand Down
5 changes: 0 additions & 5 deletions internal/app/model/impl/gorm/entity/e_role_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ type RoleMenu struct {
ActionID string `gorm:"column:action_id;size:36;index;default:'';not null;"` // 动作ID
}

// TableName 表名
func (a RoleMenu) TableName() string {
return a.Model.TableName("role_menu")
}

// ToSchemaRoleMenu 转换为角色菜单对象
func (a RoleMenu) ToSchemaRoleMenu() *schema.RoleMenu {
item := new(schema.RoleMenu)
Expand Down
5 changes: 0 additions & 5 deletions internal/app/model/impl/gorm/entity/e_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ type User struct {
Creator string `gorm:"column:creator;size:36;"` // 创建者
}

// TableName 表名
func (a User) TableName() string {
return a.Model.TableName("user")
}

// ToSchemaUser 转换为用户对象
func (a User) ToSchemaUser() *schema.User {
item := new(schema.User)
Expand Down
5 changes: 0 additions & 5 deletions internal/app/model/impl/gorm/entity/e_user_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ type UserRole struct {
RoleID string `gorm:"column:role_id;size:36;index;default:'';not null;"` // 角色内码
}

// TableName 表名
func (a UserRole) TableName() string {
return a.Model.TableName("user_role")
}

// ToSchemaUserRole 转换为用户角色对象
func (a UserRole) ToSchemaUserRole() *schema.UserRole {
item := new(schema.UserRole)
Expand Down
6 changes: 0 additions & 6 deletions internal/app/model/impl/gorm/entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package entity

import (
"context"
"fmt"
"time"

"github.com/LyricTian/gin-admin/v6/internal/app/config"
Expand All @@ -18,11 +17,6 @@ type Model struct {
DeletedAt *time.Time `gorm:"column:deleted_at;index;"`
}

// TableName table name
func (Model) TableName(name string) string {
return fmt.Sprintf("%s%s", config.C.Gorm.TablePrefix, name)
}

// GetDB ...
func GetDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
trans, ok := icontext.FromTrans(ctx)
Expand Down
5 changes: 5 additions & 0 deletions internal/app/model/impl/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ type Config struct {
MaxLifetime int
MaxOpenConns int
MaxIdleConns int
TablePrefix string
}

// NewDB 创建DB实例
func NewDB(c *Config) (*gorm.DB, func(), error) {
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
return c.TablePrefix + defaultTableName
}
db, err := gorm.Open(c.DBType, c.DSN)
if err != nil {
return nil, nil, err
Expand All @@ -49,6 +53,7 @@ func NewDB(c *Config) (*gorm.DB, func(), error) {
return nil, cleanFunc, err
}

db.SingularTable(true)
db.DB().SetMaxIdleConns(c.MaxIdleConns)
db.DB().SetMaxOpenConns(c.MaxOpenConns)
db.DB().SetConnMaxLifetime(time.Duration(c.MaxLifetime) * time.Second)
Expand Down
8 changes: 4 additions & 4 deletions pkg/auth/jwtauth/store/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ func (s *Store) Set(ctx context.Context, tokenString string, expiration time.Dur
}

// Delete ...
func (s *Store) Delete(ctx context.Context, tokenString string) error {
cmd := s.cli.Del(tokenString)
func (s *Store) Delete(ctx context.Context, tokenString string) (bool, error) {
cmd := s.cli.Del(s.wrapperKey(tokenString))
if err := cmd.Err(); err != nil {
return err
return false, err
}
return nil
return cmd.Val() > 0, nil
}

// Check ...
Expand Down
8 changes: 5 additions & 3 deletions pkg/auth/jwtauth/store/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ const (

func TestStore(t *testing.T) {
store := NewStore(&Config{
Addr: addr,
DB: 1,
Addr: addr,
DB: 1,
KeyPrefix: "prefix",
})

defer store.Close()
Expand All @@ -28,6 +29,7 @@ func TestStore(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, true, b)

err = store.Delete(ctx, key)
b, err = store.Delete(ctx, key)
assert.Nil(t, err)
assert.Equal(t, true, b)
}

0 comments on commit 6cf4080

Please sign in to comment.