package logic import ( "encoding/json" "fmt" "fusenapi/initalize" "fusenapi/model/gmodel" "fusenapi/utils/check" "log" "strings" "testing" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) func TestMain(t *testing.T) { conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") condUser := "user_id = 105" var mquerys map[string]*ModuleQuery = make(map[string]*ModuleQuery) var metadict map[string]any = make(map[string]any) for _, module := range []string{"material.logo.logo_url", "userinfo.profile"} { if !check.CheckModuleQuery(module) { } mlist := strings.Split(module, ".") if len(mlist) < 2 { } mtable := mlist[0] tname, ok := ModuleTable[mtable] if !ok { } moduleName := mlist[1] cond := fmt.Sprintf("module = '%s' and %s", moduleName, condUser) if mquery, ok := mquerys[mtable]; ok { mquery.ModuleQuery[strings.Join(mlist[2:], ",")] = struct{}{} } else { mquery := &ModuleQuery{ TableName: tname, Cond: cond, ModuleName: mtable + "." + moduleName, ModuleQuery: map[string]struct{}{strings.Join(mlist[2:], ","): {}}} mquerys[mtable] = mquery } } for _, mquery := range mquerys { sqlstr := fmt.Sprintf("select id, module, %s as querydata from %s where %s order by ctime asc limit 1", mquery.EncodeQuery("metadata"), mquery.TableName, mquery.Cond) raw := conn.Raw(sqlstr) if raw.Error != nil { if raw.Error == gorm.ErrRecordNotFound { continue } else { logx.Error(raw.Error) } } var info map[string]any = make(map[string]any) err := raw.Scan(&info).Error if err == gorm.ErrRecordNotFound { continue } if err != nil { logx.Error(err, mquery.EncodeQuery("metadata")) } logx.Error(info) queryjson, ok := info["querydata"].(string) if !ok { return } var querydata map[string]any = make(map[string]any) err = json.Unmarshal([]byte(queryjson), &querydata) if err != nil { logx.Error(info) } for k, v := range querydata { metadict[k] = v } } } func TestCaseJSON_EXTRACT(t *testing.T) { userProfile := &gmodel.UserProfile{ FirstName: "FirstName", LastName: "LastName", Resetaurant: "Resetaurant", } metadata, err := json.Marshal(userProfile) if err != nil { panic(err) } updatesql := `UPDATE fusen.fs_change_code SET metadata = CASE WHEN metadata IS NULL THEN '%s' ELSE JSON_MERGE_PATCH(metadata, '%s') END WHERE id = ?;` updatesql = fmt.Sprintf(updatesql, metadata, metadata) log.Println(string(updatesql)) conn := initalize.InitMysql("fsreaderwriter:XErSYmLELKMnf3Dh@tcp(fusen.cdmigcvz3rle.us-east-2.rds.amazonaws.com:3306)/fusen") // err = conn.Exec(updatesql, 6).Error log.Println(conn.Model(&gmodel.FsChangeCode{}).Select("id").Where("id = 5").Take(nil).Error) log.Println(err) }