修改数据库

This commit is contained in:
gong 2025-10-28 14:30:40 +08:00
parent 3cbedd7063
commit a51728a14b
4 changed files with 65 additions and 25 deletions

View File

@ -22,17 +22,20 @@ func copyOrders(db *gorm.DB, timeNow time.Time) {
db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.OrdersCopy1{}) db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.OrdersCopy1{})
_execOrders(db, start, 0) var totalCount int64 = 0
db.Debug().Model(&data.Orders{}).Where("create_time >= ?", deleteTime).Count(&totalCount)
_execOrders(db, start, 0, totalCount)
db.Debug().Where("create_time < ?", start).Delete(&data.Orders{}) db.Debug().Where("create_time < ?", start).Delete(&data.Orders{})
} }
func _execOrders(db *gorm.DB, timeNow string, index int) { func _execOrders(db *gorm.DB, timeNow string, index int, totalCount int64) {
var orders []*data.Orders var orders []*data.Orders
db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&orders) db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&orders)
fmt.Printf("index: %d, timeNow: %s ,orders count: %d\n", index, timeNow, len(orders)) fmt.Printf("index: %d, timeNow: %s ,orders count: %d, totalCount: %d\n", index, timeNow, len(orders), totalCount)
if len(orders) == 0 { if len(orders) == 0 {
return return
} }
@ -46,7 +49,9 @@ func _execOrders(db *gorm.DB, timeNow string, index int) {
db.Create(&copyList) db.Create(&copyList)
_execOrders(db, timeNow, index+1) //time.Sleep(1500 * time.Millisecond)
_execOrders(db, timeNow, index+1, totalCount)
} }
func copyPayDetails(db *gorm.DB, timeNow time.Time) { func copyPayDetails(db *gorm.DB, timeNow time.Time) {
@ -62,17 +67,20 @@ func copyPayDetails(db *gorm.DB, timeNow time.Time) {
db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.PayDetailsCopy1{}) db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.PayDetailsCopy1{})
_execPayDetails(db, start, 0) var totalCount int64 = 0
db.Debug().Model(&data.PayDetails{}).Where("create_time >= ?", deleteTime).Count(&totalCount)
_execPayDetails(db, start, 0, totalCount)
db.Debug().Where("create_time < ?", start).Delete(&data.PayDetails{}) db.Debug().Where("create_time < ?", start).Delete(&data.PayDetails{})
} }
func _execPayDetails(db *gorm.DB, timeNow string, index int) { func _execPayDetails(db *gorm.DB, timeNow string, index int, totalCount int64) {
var details []*data.PayDetails var details []*data.PayDetails
db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&details) db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&details)
fmt.Printf("index: %d, timeNow: %s ,pay_details count: %d\n", index, timeNow, len(details)) fmt.Printf("index: %d, timeNow: %s ,pay_details count: %d, totalCount: %d\n", index, timeNow, len(details), totalCount)
if len(details) == 0 { if len(details) == 0 {
return return
} }
@ -86,7 +94,9 @@ func _execPayDetails(db *gorm.DB, timeNow string, index int) {
db.Create(&copyList) db.Create(&copyList)
_execPayDetails(db, timeNow, index+1) //time.Sleep(1500 * time.Millisecond)
_execPayDetails(db, timeNow, index+1, totalCount)
} }
func copyUserMoneyDetails(db *gorm.DB, timeNow time.Time) { func copyUserMoneyDetails(db *gorm.DB, timeNow time.Time) {
@ -102,17 +112,20 @@ func copyUserMoneyDetails(db *gorm.DB, timeNow time.Time) {
db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.UserMoneyDetailsCopy1{}) db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.UserMoneyDetailsCopy1{})
_execUserMoneyDetails(db, start, 0) var totalCount int64 = 0
db.Debug().Model(&data.UserMoneyDetails{}).Where("create_time >= ?", deleteTime).Count(&totalCount)
_execUserMoneyDetails(db, start, 0, totalCount)
db.Debug().Where("create_time < ?", start).Delete(&data.UserMoneyDetails{}) db.Debug().Where("create_time < ?", start).Delete(&data.UserMoneyDetails{})
} }
func _execUserMoneyDetails(db *gorm.DB, timeNow string, index int) { func _execUserMoneyDetails(db *gorm.DB, timeNow string, index int, totalCount int64) {
var details []*data.UserMoneyDetails var details []*data.UserMoneyDetails
db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&details) db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&details)
fmt.Printf("index: %d, timeNow: %s ,pay_details count: %d\n", index, timeNow, len(details)) fmt.Printf("index: %d, timeNow: %s ,user_money_details count: %d, totalCount: %d\n", index, timeNow, len(details), totalCount)
if len(details) == 0 { if len(details) == 0 {
return return
} }
@ -126,7 +139,9 @@ func _execUserMoneyDetails(db *gorm.DB, timeNow string, index int) {
db.Create(&copyList) db.Create(&copyList)
_execUserMoneyDetails(db, timeNow, index+1) //time.Sleep(1500 * time.Millisecond)
_execUserMoneyDetails(db, timeNow, index+1, totalCount)
} }
func copyCourseCollect(db *gorm.DB, timeNow time.Time) { func copyCourseCollect(db *gorm.DB, timeNow time.Time) {
@ -142,17 +157,20 @@ func copyCourseCollect(db *gorm.DB, timeNow time.Time) {
db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.CourseCollectCopy1{}) db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.CourseCollectCopy1{})
_execCourseCollect(db, start, 0) var totalCount int64 = 0
db.Debug().Model(&data.CourseCollect{}).Where("create_time >= ?", deleteTime).Count(&totalCount)
_execCourseCollect(db, start, 0, totalCount)
db.Debug().Where("create_time < ?", start).Delete(&data.CourseCollect{}) db.Debug().Where("create_time < ?", start).Delete(&data.CourseCollect{})
} }
func _execCourseCollect(db *gorm.DB, timeNow string, index int) { func _execCourseCollect(db *gorm.DB, timeNow string, index int, totalCount int64) {
var collects []*data.CourseCollect var collects []*data.CourseCollect
db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&collects) 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)) fmt.Printf("index: %d, timeNow: %s ,course_collect count: %d, totalCount: %d\n", index, timeNow, len(collects), totalCount)
if len(collects) == 0 { if len(collects) == 0 {
return return
} }
@ -166,7 +184,9 @@ func _execCourseCollect(db *gorm.DB, timeNow string, index int) {
db.Create(&copyList) db.Create(&copyList)
_execCourseCollect(db, timeNow, index+1) //time.Sleep(1500 * time.Millisecond)
_execCourseCollect(db, timeNow, index+1, totalCount)
} }
func copyCourseUser(db *gorm.DB, timeNow time.Time) { func copyCourseUser(db *gorm.DB, timeNow time.Time) {
@ -182,17 +202,20 @@ func copyCourseUser(db *gorm.DB, timeNow time.Time) {
db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.CourseUserCopy1{}) db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.CourseUserCopy1{})
_execCourseUser(db, start, 0) var totalCount int64 = 0
db.Debug().Model(&data.CourseUser{}).Where("create_time >= ?", deleteTime).Count(&totalCount)
_execCourseUser(db, start, 0, totalCount)
db.Debug().Where("create_time < ?", start).Delete(&data.CourseUser{}) db.Debug().Where("create_time < ?", start).Delete(&data.CourseUser{})
} }
func _execCourseUser(db *gorm.DB, timeNow string, index int) { func _execCourseUser(db *gorm.DB, timeNow string, index int, totalCount int64) {
var users []*data.CourseUser var users []*data.CourseUser
db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&users) 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)) fmt.Printf("index: %d, timeNow: %s ,course_user count: %d, totalCount: %d\n", index, timeNow, len(users), totalCount)
if len(users) == 0 { if len(users) == 0 {
return return
} }
@ -206,7 +229,9 @@ func _execCourseUser(db *gorm.DB, timeNow string, index int) {
db.Create(&copyList) db.Create(&copyList)
_execCourseUser(db, timeNow, index+1) //time.Sleep(1500 * time.Millisecond)
_execCourseUser(db, timeNow, index+1, totalCount)
} }
func copyDiscSpinningRecord(db *gorm.DB, timeNow time.Time) { func copyDiscSpinningRecord(db *gorm.DB, timeNow time.Time) {
@ -222,17 +247,20 @@ func copyDiscSpinningRecord(db *gorm.DB, timeNow time.Time) {
db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.DiscSpinningRecordCopy1{}) db.Debug().Where("create_time >= ?", deleteTime).Delete(&data.DiscSpinningRecordCopy1{})
_execDiscSpinningRecord(db, start, 0) var totalCount int64 = 0
db.Debug().Model(&data.DiscSpinningRecord{}).Where("create_time >= ?", deleteTime).Count(&totalCount)
_execDiscSpinningRecord(db, start, 0, totalCount)
db.Debug().Where("create_time < ?", start).Delete(&data.DiscSpinningRecord{}) db.Debug().Where("create_time < ?", start).Delete(&data.DiscSpinningRecord{})
} }
func _execDiscSpinningRecord(db *gorm.DB, timeNow string, index int) { func _execDiscSpinningRecord(db *gorm.DB, timeNow string, index int, totalCount int64) {
var records []*data.DiscSpinningRecord var records []*data.DiscSpinningRecord
db.Debug().Where("create_time < ?", timeNow).Limit(1000).Offset(index * 1000).Find(&records) 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)) fmt.Printf("index: %d, timeNow: %s ,disc_spinning_record count: %d, totalCount: %d\n", index, timeNow, len(records), totalCount)
if len(records) == 0 { if len(records) == 0 {
return return
} }
@ -246,7 +274,9 @@ func _execDiscSpinningRecord(db *gorm.DB, timeNow string, index int) {
db.Create(&copyList) db.Create(&copyList)
_execDiscSpinningRecord(db, timeNow, index+1) //time.Sleep(1500 * time.Millisecond)
_execDiscSpinningRecord(db, timeNow, index+1, totalCount)
} }
// CopyStruct 使用反射复制结构体 // CopyStruct 使用反射复制结构体

View File

@ -54,6 +54,8 @@ func DisconnectDB() {
} }
_ = s.Close() _ = s.Close()
} }
fmt.Println("Disconnect to DB success")
} }
func _loadConfig() { func _loadConfig() {

View File

@ -13,7 +13,8 @@ dev:
- duanju-4 - duanju-4
prod: prod:
host: rm-2vc4acq5369l0y612.mysql.cn-chengdu.rds.aliyuncs.com host: rm-2vc4acq5369l0y612mo.mysql.cn-chengdu.rds.aliyuncs.com
# host: rm-2vc4acq5369l0y612.mysql.cn-chengdu.rds.aliyuncs.com
name: video_user name: video_user
password: VideoUser@1 password: VideoUser@1
port: 3306 port: 3306

View File

@ -8,6 +8,7 @@ import (
) )
func main() { func main() {
_testDbConn()
//_startCopyData() //_startCopyData()
c := cron.New() c := cron.New()
_, err := c.AddFunc("0 4 * * *", func() { _, err := c.AddFunc("0 4 * * *", func() {
@ -25,6 +26,12 @@ func main() {
select {} select {}
} }
func _testDbConn() {
db.ConnectDB()
time.Sleep(10 * time.Second)
db.DisconnectDB()
}
func _startCopyData() { func _startCopyData() {
db.ConnectDB() db.ConnectDB()
db.CopyData() db.CopyData()