Browse Source

迁移download_info表

developer
appolli 6 months ago
parent
commit
d1b345dd3f
  1. 2
      main_program/config.yml
  2. 13
      main_program/entity/DownloadInfo.go
  3. 29
      main_program/moveData/moveDataService.go

2
main_program/config.yml

@ -3,7 +3,7 @@ log:
logpath: main_program
command: move_data
movedata:
table: Videos
table: DownLoadInfo
dbpath: D:/Work/Code/youtube_dev/youtube_prod.db
mysql:
host: 47.108.20.249:3306

13
main_program/entity/DownloadInfo.go

@ -0,0 +1,13 @@
package entity
type DownloadInfo struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement"`
VideoId string `gorm:"column:videoId;type:varchar(255);not null"`
DownloadType int `gorm:"column:downloadType;not null"`
TryTime int `gorm:"column:tryTIme;not null"`
IsFinished int `gorm:"column:isFinished;not null"`
}
func (DownloadInfo) TableName() string {
return "Download_info"
}

29
main_program/moveData/moveDataService.go

@ -35,6 +35,8 @@ func (m *moveDataService) Start(table string, sqlitePath string, mysqlConfig con
m.moveChannel(sqliteDB, mysqlDB)
} else if table == "Videos" {
m.moveVideos(sqliteDB, mysqlDB)
} else if table == "DownLoadInfo" {
m.moveDownLoadInfo(mysqlDB)
}
}
@ -168,3 +170,30 @@ func (m *moveDataService) moveVideos(sqliteDB *sql.DB, mysqlDB *gorm.DB) {
}
config.Logger.Info("完成迁移Videos表...")
}
func (m *moveDataService) moveDownLoadInfo(mysqlDB *gorm.DB) {
continueFlag := true
count := 1
for continueFlag {
// 从videos表里获取,直接放到DownLoadInfo,并全部设置为未下载状态
sqlStr := "SELECT v.* FROM Videos v WHERE NOT EXISTS ( SELECT * FROM Download_info d WHERE v.videoId = d.videoId ) LIMIT 1000;"
var videos []entity.Video
mysqlDB.Raw(sqlStr).Scan(&videos)
var downloadInfos []entity.DownloadInfo
for _, video := range videos {
config.Logger.Info(video)
downloadInfo := new(entity.DownloadInfo)
downloadInfo.DownloadType = 1
downloadInfo.IsFinished = 0
downloadInfo.TryTime = 0
downloadInfo.VideoId = video.VideoId
downloadInfos = append(downloadInfos, *downloadInfo)
}
mysqlDB.CreateInBatches(downloadInfos, 1000)
count += 1
config.Logger.Infof("count: %d", count)
// 判断是否还存在
existsStr := "SELECT EXISTS(SELECT v.* FROM Videos v WHERE NOT EXISTS ( SELECT * FROM Download_info d WHERE v.videoId = d.videoId ) LIMIT 1);"
mysqlDB.Raw(existsStr).Scan(&continueFlag)
}
}

Loading…
Cancel
Save