代码目录:
main.go
go.mod
go.sum
makeData/makeData.go
main.go:
package main
import ( "fmt" "strconv" "strings" //"mysqltaskdirdemo/makeData" "gorm.io/driver/mysql" "gorm.io/gorm" ) type VsTask struct { ID uint Sha256 string FileType uint8 } type VsDir struct { ID uint VsTaskId string } func CreateVsDir(db *gorm.DB) error { // Note the use of tx as the database handle once you are within a transaction //get min id from vs_tasks //get five records from vs_tasks //insert into this five records to vs_dirs //delete this five records from vs_tasks //commit tx := db.Begin() defer func() { if r := recover(); r != nil { tx.Rollback() } }() if err := tx.Error; err != nil { return err } var ID string if err := tx.Raw("SELECT MIN(ID) FROM vs_tasks").Scan(&ID).Error; err != nil { tx.Rollback() return err } var IDs []string IDnum, err := strconv.Atoi(ID) if err != nil { panic(err) } upID := IDnum + 4 if err := tx.Raw("select ID from vs_tasks WHERE ID >= ? and ID <= ? for update", ID, upID).Scan(&IDs).Error; err != nil { tx.Rollback() return err } VsTaskIdStr := strings.Join(IDs