Skip to content

Commit 369d140

Browse files
committed
refactor(settings):更新旧设置模型并迁移数据到新的多模型版本
- 新增 OldSettings 结构体,用于表示旧的设置模型 - 实现 updateMultipleModel 函数,将旧设置中的 AI 配置数据迁移到新模型 - 在 AutoMigrate 函数中调用 updateMultipleModel,确保数据迁移在数据库自动迁移过程中完成
1 parent 1e7387f commit 369d140

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

backend/models/models.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,38 @@ type PMIResp struct {
465465
DCResp
466466
PMIResult PMIResult `json:"result"`
467467
}
468+
469+
type OldSettings struct {
470+
gorm.Model
471+
TushareToken string `json:"tushareToken"`
472+
LocalPushEnable bool `json:"localPushEnable"`
473+
DingPushEnable bool `json:"dingPushEnable"`
474+
DingRobot string `json:"dingRobot"`
475+
UpdateBasicInfoOnStart bool `json:"updateBasicInfoOnStart"`
476+
RefreshInterval int64 `json:"refreshInterval"`
477+
478+
OpenAiEnable bool `json:"openAiEnable"`
479+
OpenAiBaseUrl string `json:"openAiBaseUrl"`
480+
OpenAiApiKey string `json:"openAiApiKey"`
481+
OpenAiModelName string `json:"openAiModelName"`
482+
OpenAiMaxTokens int `json:"openAiMaxTokens"`
483+
OpenAiTemperature float64 `json:"openAiTemperature"`
484+
OpenAiApiTimeOut int `json:"openAiApiTimeOut"`
485+
Prompt string `json:"prompt"`
486+
CheckUpdate bool `json:"checkUpdate"`
487+
QuestionTemplate string `json:"questionTemplate"`
488+
CrawlTimeOut int64 `json:"crawlTimeOut"`
489+
KDays int64 `json:"kDays"`
490+
EnableDanmu bool `json:"enableDanmu"`
491+
BrowserPath string `json:"browserPath"`
492+
EnableNews bool `json:"enableNews"`
493+
DarkTheme bool `json:"darkTheme"`
494+
BrowserPoolSize int `json:"browserPoolSize"`
495+
EnableFund bool `json:"enableFund"`
496+
EnablePushNews bool `json:"enablePushNews"`
497+
SponsorCode string `json:"sponsorCode"`
498+
}
499+
500+
func (receiver OldSettings) TableName() string {
501+
return "settings"
502+
}

main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ func main() {
188188

189189
}
190190

191+
func updateMultipleModel() {
192+
oldSettings := &models.OldSettings{}
193+
db.Dao.Model(oldSettings).First(oldSettings)
194+
aiConfig := &data.AIConfig{}
195+
db.Dao.Model(aiConfig).First(aiConfig)
196+
if oldSettings.OpenAiEnable && oldSettings.OpenAiApiKey != "" && aiConfig.ID == 0 {
197+
aiConfig.Name = oldSettings.OpenAiModelName
198+
aiConfig.ApiKey = oldSettings.OpenAiApiKey
199+
aiConfig.BaseUrl = oldSettings.OpenAiBaseUrl
200+
aiConfig.ModelName = oldSettings.OpenAiModelName
201+
aiConfig.Temperature = oldSettings.OpenAiTemperature
202+
aiConfig.MaxTokens = oldSettings.OpenAiMaxTokens
203+
aiConfig.TimeOut = oldSettings.OpenAiApiTimeOut
204+
err := db.Dao.Model(aiConfig).Create(aiConfig).Error
205+
if err != nil {
206+
log.SugaredLogger.Error(err.Error())
207+
}
208+
}
209+
}
210+
191211
func AutoMigrate() {
192212
db.Dao.AutoMigrate(&data.StockInfo{})
193213
db.Dao.AutoMigrate(&data.StockBasic{})
@@ -207,6 +227,8 @@ func AutoMigrate() {
207227
db.Dao.AutoMigrate(&models.TelegraphTags{})
208228
db.Dao.AutoMigrate(&models.LongTigerRankData{})
209229
db.Dao.AutoMigrate(&data.AIConfig{})
230+
231+
updateMultipleModel()
210232
}
211233

212234
func initStockDataUS(ctx context.Context) {

0 commit comments

Comments
 (0)