新的迁移规则
This commit is contained in:
parent
ca62f9b361
commit
d4d6c67477
|
|
@ -0,0 +1,31 @@
|
|||
package data
|
||||
|
||||
type (
|
||||
CourseCollect struct {
|
||||
CourseCollectId int64 `json:"course_collect_id" gorm:"column:course_collect_id;primary_key"`
|
||||
CourseId int64 `json:"course_id" gorm:"column:course_id"`
|
||||
UserId int64 `json:"user_id" gorm:"column:user_id"`
|
||||
CreateTime string `json:"create_time" gorm:"column:create_time"`
|
||||
CourseDetailsId int64 `json:"course_details_id" gorm:"column:course_details_id"`
|
||||
Classify int `json:"classify" gorm:"column:classify"`
|
||||
UpdateTime string `json:"update_time" gorm:"column:update_time"`
|
||||
}
|
||||
|
||||
CourseCollectCopy1 struct {
|
||||
CourseCollectId int64 `json:"course_collect_id" gorm:"column:course_collect_id;primary_key"`
|
||||
CourseId int64 `json:"course_id" gorm:"column:course_id"`
|
||||
UserId int64 `json:"user_id" gorm:"column:user_id"`
|
||||
CreateTime string `json:"create_time" gorm:"column:create_time"`
|
||||
CourseDetailsId int64 `json:"course_details_id" gorm:"column:course_details_id"`
|
||||
Classify int `json:"classify" gorm:"column:classify"`
|
||||
UpdateTime string `json:"update_time" gorm:"column:update_time"`
|
||||
}
|
||||
)
|
||||
|
||||
func (CourseCollect) TableName() string {
|
||||
return "course_collect"
|
||||
}
|
||||
|
||||
func (CourseCollectCopy1) TableName() string {
|
||||
return "course_collect_copy1"
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
package data
|
||||
|
||||
type (
|
||||
CourseUser struct {
|
||||
CourseUserId int64 `json:"course_user_id" gorm:"column:course_user_id;primary_key"`
|
||||
CourseId int64 `json:"course_id" gorm:"column:course_id"`
|
||||
OrderId int64 `json:"order_id" gorm:"column:order_id"`
|
||||
UserId int64 `json:"user_id" gorm:"column:user_id"`
|
||||
CreateTime string `json:"create_time" gorm:"column:create_time"`
|
||||
UpdateTime string `json:"update_time" gorm:"column:update_time"`
|
||||
CourseDetailsId int64 `json:"course_details_id" gorm:"column:course_details_id"`
|
||||
Classify int64 `json:"classify" gorm:"column:classify"`
|
||||
}
|
||||
|
||||
CourseUserCopy1 struct {
|
||||
CourseUserId int64 `json:"course_user_id" gorm:"column:course_user_id;primary_key"`
|
||||
CourseId int64 `json:"course_id" gorm:"column:course_id"`
|
||||
OrderId int64 `json:"order_id" gorm:"column:order_id"`
|
||||
UserId int64 `json:"user_id" gorm:"column:user_id"`
|
||||
CreateTime string `json:"create_time" gorm:"column:create_time"`
|
||||
UpdateTime string `json:"update_time" gorm:"column:update_time"`
|
||||
CourseDetailsId int64 `json:"course_details_id" gorm:"column:course_details_id"`
|
||||
Classify int64 `json:"classify" gorm:"column:classify"`
|
||||
}
|
||||
)
|
||||
|
||||
func (CourseUser) TableName() string {
|
||||
return "course_user"
|
||||
}
|
||||
|
||||
func (CourseUserCopy1) TableName() string {
|
||||
return "course_user_copy1"
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
package data
|
||||
|
||||
type (
|
||||
DiscSpinningRecord struct {
|
||||
Id int64 `json:"id" gorm:"column:id;primary_key"`
|
||||
UserId int64 `json:"user_id" gorm:"column:user_id"`
|
||||
SourceId int64 `json:"source_id" gorm:"column:source_id"`
|
||||
ImgUrl string `json:"img_url" gorm:"column:img_url"`
|
||||
Name string `json:"name" gorm:"column:name"`
|
||||
Type int `json:"type" gorm:"column:type"`
|
||||
Number float64 `json:"number" gorm:"column:number"`
|
||||
Target string `json:"target" gorm:"column:target"`
|
||||
TargetId int64 `json:"target_id" gorm:"column:target_id"`
|
||||
DrawDay string `json:"draw_day" gorm:"column:draw_day"`
|
||||
Source string `json:"source" gorm:"column:source"`
|
||||
CreateTime string `json:"create_time" gorm:"column:create_time"`
|
||||
}
|
||||
|
||||
DiscSpinningRecordCopy1 struct {
|
||||
Id int64 `json:"id" gorm:"column:id;primary_key"`
|
||||
UserId int64 `json:"user_id" gorm:"column:user_id"`
|
||||
SourceId int64 `json:"source_id" gorm:"column:source_id"`
|
||||
ImgUrl string `json:"img_url" gorm:"column:img_url"`
|
||||
Name string `json:"name" gorm:"column:name"`
|
||||
Type int `json:"type" gorm:"column:type"`
|
||||
Number float64 `json:"number" gorm:"column:number"`
|
||||
Target string `json:"target" gorm:"column:target"`
|
||||
TargetId int64 `json:"target_id" gorm:"column:target_id"`
|
||||
DrawDay string `json:"draw_day" gorm:"column:draw_day"`
|
||||
Source string `json:"source" gorm:"column:source"`
|
||||
CreateTime string `json:"create_time" gorm:"column:create_time"`
|
||||
}
|
||||
)
|
||||
|
||||
func (DiscSpinningRecord) TableName() string {
|
||||
return "disc_spinning_record"
|
||||
}
|
||||
|
||||
func (DiscSpinningRecordCopy1) TableName() string {
|
||||
return "disc_spinning_record_copy1"
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@ type (
|
|||
TradeNo string `json:"trade_no" gorm:"column:trade_no"`
|
||||
Money float64 `json:"money" gorm:"column:money"`
|
||||
UserId int64 `json:"user_id" gorm:"column:user_id"`
|
||||
State int `json:"state" gorm:"column:state"`
|
||||
CreateTime string `json:"create_time" gorm:"column:create_time"`
|
||||
PayTime string `json:"pay_time" gorm:"column:pay_time"`
|
||||
Type int `json:"type" gorm:"column:type"`
|
||||
|
|
@ -24,6 +25,7 @@ type (
|
|||
TradeNo string `json:"trade_no" gorm:"column:trade_no"`
|
||||
Money float64 `json:"money" gorm:"column:money"`
|
||||
UserId int64 `json:"user_id" gorm:"column:user_id"`
|
||||
State int `json:"state" gorm:"column:state"`
|
||||
CreateTime string `json:"create_time" gorm:"column:create_time"`
|
||||
PayTime string `json:"pay_time" gorm:"column:pay_time"`
|
||||
Type int `json:"type" gorm:"column:type"`
|
||||
|
|
|
|||
120
db/copy_data.go
120
db/copy_data.go
|
|
@ -129,6 +129,126 @@ func _execUserMoneyDetails(db *gorm.DB, timeNow string, index int) {
|
|||
_execUserMoneyDetails(db, timeNow, index+1)
|
||||
}
|
||||
|
||||
func copyCourseCollect(db *gorm.DB, timeNow time.Time) {
|
||||
collect := &data.CourseCollect{}
|
||||
start := timeNow.Format("2006-01-02 15:04:05")
|
||||
first := db.Model(collect).Where("create_time < ?", start).Order("create_time asc").First(collect)
|
||||
if first.RowsAffected == 0 {
|
||||
fmt.Println("no course_collect data")
|
||||
return
|
||||
}
|
||||
|
||||
deleteTime := collect.CreateTime
|
||||
|
||||
db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.CourseCollectCopy1{})
|
||||
|
||||
_execCourseCollect(db, start, 0)
|
||||
|
||||
db.Debug().Where("create_time < ?", start).Delete(&data.CourseCollect{})
|
||||
}
|
||||
|
||||
func _execCourseCollect(db *gorm.DB, timeNow string, index int) {
|
||||
var collects []*data.CourseCollect
|
||||
|
||||
db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&collects)
|
||||
|
||||
fmt.Printf("index: %d, timeNow: %s ,course_collect count: %d\n", index, timeNow, len(collects))
|
||||
if len(collects) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var copyList []*data.CourseCollectCopy1
|
||||
for _, collect := range collects {
|
||||
collectCopy := &data.CourseCollectCopy1{}
|
||||
CopyStruct(collect, collectCopy)
|
||||
copyList = append(copyList, collectCopy)
|
||||
}
|
||||
|
||||
db.Create(©List)
|
||||
|
||||
_execCourseCollect(db, timeNow, index+1)
|
||||
}
|
||||
|
||||
func copyCourseUser(db *gorm.DB, timeNow time.Time) {
|
||||
user := &data.CourseUser{}
|
||||
start := timeNow.Format("2006-01-02 15:04:05")
|
||||
first := db.Model(user).Where("create_time < ?", start).Order("create_time asc").First(user)
|
||||
if first.RowsAffected == 0 {
|
||||
fmt.Println("no course_user data")
|
||||
return
|
||||
}
|
||||
|
||||
deleteTime := user.CreateTime
|
||||
|
||||
db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.CourseUserCopy1{})
|
||||
|
||||
_execCourseUser(db, start, 0)
|
||||
|
||||
db.Debug().Where("create_time < ?", start).Delete(&data.CourseUser{})
|
||||
}
|
||||
|
||||
func _execCourseUser(db *gorm.DB, timeNow string, index int) {
|
||||
var users []*data.CourseUser
|
||||
|
||||
db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&users)
|
||||
|
||||
fmt.Printf("index: %d, timeNow: %s ,course_user count: %d\n", index, timeNow, len(users))
|
||||
if len(users) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var copyList []*data.CourseUserCopy1
|
||||
for _, user := range users {
|
||||
userCopy := &data.CourseUserCopy1{}
|
||||
CopyStruct(user, userCopy)
|
||||
copyList = append(copyList, userCopy)
|
||||
}
|
||||
|
||||
db.Create(©List)
|
||||
|
||||
_execCourseUser(db, timeNow, index+1)
|
||||
}
|
||||
|
||||
func copyDiscSpinningRecord(db *gorm.DB, timeNow time.Time) {
|
||||
record := &data.DiscSpinningRecord{}
|
||||
start := timeNow.Format("2006-01-02 15:04:05")
|
||||
first := db.Model(record).Where("create_time < ?", start).Order("create_time asc").First(record)
|
||||
if first.RowsAffected == 0 {
|
||||
fmt.Println("no disc_spinning_record data")
|
||||
return
|
||||
}
|
||||
|
||||
deleteTime := record.CreateTime
|
||||
|
||||
db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.DiscSpinningRecordCopy1{})
|
||||
|
||||
_execDiscSpinningRecord(db, start, 0)
|
||||
|
||||
db.Debug().Where("create_time < ?", start).Delete(&data.DiscSpinningRecord{})
|
||||
}
|
||||
|
||||
func _execDiscSpinningRecord(db *gorm.DB, timeNow string, index int) {
|
||||
var records []*data.DiscSpinningRecord
|
||||
|
||||
db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&records)
|
||||
|
||||
fmt.Printf("index: %d, timeNow: %s ,disc_spinning_record count: %d\n", index, timeNow, len(records))
|
||||
if len(records) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var copyList []*data.DiscSpinningRecordCopy1
|
||||
for _, record := range records {
|
||||
recordCopy := &data.DiscSpinningRecordCopy1{}
|
||||
CopyStruct(record, recordCopy)
|
||||
copyList = append(copyList, recordCopy)
|
||||
}
|
||||
|
||||
db.Create(©List)
|
||||
|
||||
_execDiscSpinningRecord(db, timeNow, index+1)
|
||||
}
|
||||
|
||||
// CopyStruct 使用反射复制结构体
|
||||
func CopyStruct(src, dst interface{}) {
|
||||
_ = copier.Copy(dst, src)
|
||||
|
|
|
|||
|
|
@ -32,8 +32,17 @@ func CopyData() {
|
|||
|
||||
for _, db := range dbList {
|
||||
copyOrders(db, parse)
|
||||
time.Sleep(time.Second * 2)
|
||||
copyPayDetails(db, parse)
|
||||
time.Sleep(time.Second * 2)
|
||||
copyUserMoneyDetails(db, parse)
|
||||
time.Sleep(time.Second * 2)
|
||||
copyCourseCollect(db, parse)
|
||||
time.Sleep(time.Second * 2)
|
||||
copyCourseUser(db, parse)
|
||||
time.Sleep(time.Second * 2)
|
||||
copyDiscSpinningRecord(db, parse)
|
||||
time.Sleep(time.Second * 2)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,10 +12,23 @@ dev:
|
|||
- duanju-3
|
||||
- duanju-4
|
||||
|
||||
prod:
|
||||
host: rm-gc7xx913734hv5w5qxo.mysql.cn-chengdu.rds.aliyuncs.com
|
||||
name: video_user
|
||||
password: VideoUser@1
|
||||
port: 3306
|
||||
dbs:
|
||||
- duanju-0
|
||||
- duanju-1
|
||||
- duanju-2
|
||||
- duanju-3
|
||||
- duanju-4
|
||||
|
||||
|
||||
#prod:
|
||||
# host: rm-gc7xx913734hv5w5qxo.mysql.cn-chengdu.rds.aliyuncs.com
|
||||
# name: video_user
|
||||
# password: VideoUser@1
|
||||
# host: localhost
|
||||
# name: root
|
||||
# password: gongyijieqp002
|
||||
# port: 3306
|
||||
# dbs:
|
||||
# - duanju-0
|
||||
|
|
@ -23,16 +36,3 @@ dev:
|
|||
# - duanju-2
|
||||
# - duanju-3
|
||||
# - duanju-4
|
||||
|
||||
|
||||
prod:
|
||||
host: localhost
|
||||
name: root
|
||||
password: gongyijieqp002
|
||||
port: 3306
|
||||
dbs:
|
||||
- duanju-0
|
||||
# - duanju-1
|
||||
# - duanju-2
|
||||
# - duanju-3
|
||||
# - duanju-4
|
||||
|
|
|
|||
32
main.go
32
main.go
|
|
@ -1,26 +1,28 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/robfig/cron/v3"
|
||||
"time"
|
||||
"video_data_copy/db"
|
||||
)
|
||||
|
||||
func main() {
|
||||
_startCopyData()
|
||||
//c := cron.New()
|
||||
//_, err := c.AddFunc("0 4 * * *", func() {
|
||||
// fmt.Println("定时任务开始执行")
|
||||
// _startCopyData()
|
||||
//})
|
||||
//if err != nil {
|
||||
// fmt.Printf("添加定时任务出错: %v\n", err)
|
||||
// return
|
||||
//}
|
||||
//
|
||||
//fmt.Println("定时任务启动成功")
|
||||
//c.Start()
|
||||
//
|
||||
//select {}
|
||||
//_startCopyData()
|
||||
c := cron.New()
|
||||
_, err := c.AddFunc("0 4 * * *", func() {
|
||||
fmt.Println("定时任务开始执行")
|
||||
_startCopyData()
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Printf("添加定时任务出错: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("定时任务启动成功")
|
||||
c.Start()
|
||||
|
||||
select {}
|
||||
}
|
||||
|
||||
func _startCopyData() {
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Reference in New Issue