Skip to content

Commit

Permalink
fix: 处理了一种数据库为空时启动的情况;同时改进v1兼容性 (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 authored Aug 17, 2024
1 parent 663d740 commit 6f2766e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 28 deletions.
3 changes: 2 additions & 1 deletion dice/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2205,7 +2205,7 @@ func (d *Dice) loads() {
err := json.Unmarshal(data, &groupInfo)
if err == nil {
groupInfo.GroupID = id
groupInfo.UpdatedAtTime = updatedAt
groupInfo.UpdatedAtTime = 0

// 找出其中以群号开头的,这是1.2版本的bug
var toDelete []string
Expand Down Expand Up @@ -2600,6 +2600,7 @@ func (d *Dice) Save(isAuto bool) {
}
}
}

// Pinenutn: Range模板 ServiceAtNew重构代码
d.ImSession.ServiceAtNew.Range(func(key string, groupInfo *GroupInfo) bool {
// Pinenutn: ServiceAtNew重构
Expand Down
3 changes: 2 additions & 1 deletion dice/ext_reply_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (m *ReplyConditionExprTrue) Clean() {

func (m *ReplyConditionExprTrue) Check(ctx *MsgContext, _ *Message, _ *CmdArgs, _ string) bool {
// r := ctx.Eval(m.Value, ds.RollConfig{})
r, _, err := DiceExprEvalBase(ctx, m.Value, RollExtraFlags{})
r, _, err := DiceExprEvalBase(ctx, m.Value, RollExtraFlags{V2Only: true})

if err != nil {
ctx.Dice.Logger.Infof("自定义回复表达式执行失败: %s", m.Value)
Expand All @@ -136,6 +136,7 @@ func (m *ReplyConditionExprTrue) Check(ctx *MsgContext, _ *Message, _ *CmdArgs,
ctx.Dice.Logger.Infof("自定义回复表达式执行失败(后半部分不能识别 %s): %s", r.GetRestInput(), m.Value)
return false
}

// fmt.Println("???", r, err, r.AsBool(), r.Value == int64(0), r.Value != int64(0))
return r.AsBool()
}
Expand Down
6 changes: 6 additions & 0 deletions dice/model/db_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"sync"

"sealdice-core/utils/spinner"
Expand All @@ -22,6 +23,11 @@ func DBCacheDelete() bool {
return os.Remove(fnPath) == nil
}

// 非 windows 不删缓存
if runtime.GOOS != "windows" {
return true
}

ok := true
if ok {
ok = tryDelete("data.db-shm")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ require (
github.com/samber/lo v1.44.0
github.com/schollz/progressbar/v3 v3.14.4
github.com/sealdice/botgo v0.0.0-20240102160217-e61d5bdfe083
github.com/sealdice/dicescript v0.0.0-20240808080124-e177356bbeb5
github.com/sealdice/dicescript v0.0.0-20240816161158-28fab8cf354e
github.com/slack-go/slack v0.13.0
github.com/sunshineplan/imgconv v1.1.4
github.com/tailscale/hujson v0.0.0-20221223112325-20486734a56a
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ github.com/schollz/progressbar/v3 v3.14.4 h1:W9ZrDSJk7eqmQhd3uxFNNcTr0QL+xuGNI9d
github.com/schollz/progressbar/v3 v3.14.4/go.mod h1:aT3UQ7yGm+2ZjeXPqsjTenwL3ddUiuZ0kfQ/2tHlyNI=
github.com/sealdice/botgo v0.0.0-20240102160217-e61d5bdfe083 h1:s/jzCGYlM/0+TYTXwva5574EFnIv/ggPCoXHFpdbSUw=
github.com/sealdice/botgo v0.0.0-20240102160217-e61d5bdfe083/go.mod h1:MGtR0REQDslBwQE+Rln4P9iDjH/ZInlu5qzOLdvBWJU=
github.com/sealdice/dicescript v0.0.0-20240808080124-e177356bbeb5 h1:8Q4evtC6pCVMhMivzbeC0/xebOVHcAzzVTVXMJCJhKY=
github.com/sealdice/dicescript v0.0.0-20240808080124-e177356bbeb5/go.mod h1:uof752qJvEQ4Kze+NVag+RKGgj5C4K3kMHoK3e2vOLg=
github.com/sealdice/dicescript v0.0.0-20240816161158-28fab8cf354e h1:DWOn8AdV6k16msHoy8vdq4GJTZx0C2U14RRKQSIGnMM=
github.com/sealdice/dicescript v0.0.0-20240816161158-28fab8cf354e/go.mod h1:uof752qJvEQ4Kze+NVag+RKGgj5C4K3kMHoK3e2vOLg=
github.com/sealdice/kook v0.0.3 h1:STMtiKRMFjhSFmUxi0BU5ktNkCQ8qi7Y5EEfrmYKvWY=
github.com/sealdice/kook v0.0.3/go.mod h1:WjHC7AmbmNjInT/U/etBVOmAw7T6EqdCwApceRGs1sk=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
65 changes: 42 additions & 23 deletions migrate/v150_attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func attrsGroupUserMigrate(db *sqlx.Tx) (int, int, error) {

// 群数据转换
func attrsGroupMigrate(db *sqlx.Tx) (int, int, error) {
rows, err := db.NamedQuery("select id, updated_at, data from main.attrs_group", map[string]any{})
rows, err := db.NamedQuery("select id, updated_at, data from attrs_group", map[string]any{})
if err != nil {
return 0, 0, err
}
Expand Down Expand Up @@ -380,6 +380,21 @@ func attrsUserMigrate(db *sqlx.Tx) (int, int, int, error) {
return count, countSheetsNum, countFailed, nil
}

func checkTableExists(db *sqlx.DB, tableName string) (bool, error) {
var name string
err := db.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name = $1;", tableName).Scan(&name)
switch {
case errors.Is(err, sql.ErrNoRows):
// 表格不存在,继续执行
return false, nil
case err != nil:
return false, err
default:
// 表格已经存在,说明转换完成,退出
return true, nil
}
}

func V150Upgrade() bool {
dbDataPath, _ := filepath.Abs("./data/default/data.db")
if _, err := os.Stat(dbDataPath); errors.Is(err, os.ErrNotExist) {
Expand All @@ -395,15 +410,12 @@ func V150Upgrade() bool {
_ = db.Close()
}()

var name string
err = db.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name='attrs';").Scan(&name)
switch {
case errors.Is(err, sql.ErrNoRows):
// 表格不存在,继续执行
case err != nil:
exists, err := checkTableExists(db, "attrs")
if err != nil {
fmt.Println("V150数据转换未知错误:", err.Error())
return false
default:
}
if exists {
// 表格已经存在,说明转换完成,退出
return true
}
Expand Down Expand Up @@ -444,32 +456,39 @@ CREATE TABLE IF NOT EXISTS attrs (
return false
}

count, countSheetsNum, countFailed, err := attrsUserMigrate(tx)
fmt.Printf("数据卡转换 - 角色卡,成功人数%d 失败人数 %d 卡数 %d\n", count, countFailed, countSheetsNum)
if err != nil {
fmt.Println("异常", err.Error())
return false
if exists, _ := checkTableExists(db, "attrs_user"); exists {
count, countSheetsNum, countFailed, err2 := attrsUserMigrate(tx)
fmt.Printf("数据卡转换 - 角色卡,成功人数%d 失败人数 %d 卡数 %d\n", count, countFailed, countSheetsNum)
if err2 != nil {
fmt.Println("异常", err2.Error())
return false
}
}

count, countFailed, err = attrsGroupUserMigrate(tx)
fmt.Printf("数据卡转换 - 群组个人数据,成功%d 失败 %d\n", count, countFailed)
if err != nil {
fmt.Println("异常", err.Error())
return false
if exists, _ := checkTableExists(db, "attrs_group_user"); exists {
count, countFailed, err2 := attrsGroupUserMigrate(tx)
fmt.Printf("数据卡转换 - 群组个人数据,成功%d 失败 %d\n", count, countFailed)
if err2 != nil {
fmt.Println("异常", err2.Error())
return false
}
}

count, countFailed, err = attrsGroupMigrate(tx)
fmt.Printf("数据卡转换 - 群数据,成功%d 失败 %d\n", count, countFailed)
if err != nil {
fmt.Println("异常", err.Error())
return false
if exists, _ := checkTableExists(db, "attrs_group"); exists {
count, countFailed, err2 := attrsGroupMigrate(tx)
fmt.Printf("数据卡转换 - 群数据,成功%d 失败 %d\n", count, countFailed)
if err2 != nil {
fmt.Println("异常", err2.Error())
return false
}
}

// 删档
fmt.Println("删除旧版本数据")
_, _ = tx.Exec("drop table attrs_group")
_, _ = tx.Exec("drop table attrs_group_user")
_, _ = tx.Exec("drop table attrs_user")
_, _ = db.Exec("PRAGMA wal_checkpoint(TRUNCATE);")
_, _ = tx.Exec("VACUUM;") // 收尾

sheetIdBindByGroupUserId = nil
Expand Down

0 comments on commit 6f2766e

Please sign in to comment.