github.com/kunlun-qilian/sqlx/v3@v3.0.0/task_test.go (about) 1 package sqlx_test 2 3 import ( 4 "database/sql/driver" 5 "fmt" 6 "testing" 7 8 "github.com/google/uuid" 9 "github.com/kunlun-qilian/sqlx/v3" 10 "github.com/kunlun-qilian/sqlx/v3/builder" 11 "github.com/kunlun-qilian/sqlx/v3/migration" 12 "github.com/onsi/gomega" 13 ) 14 15 func TestWithTasks(t *testing.T) { 16 dbTest := sqlx.NewDatabase("test_for_user_with_tasks") 17 18 for _, connector := range []driver.Connector{ 19 mysqlConnector, 20 postgresConnector, 21 } { 22 db := dbTest.OpenDB(connector) 23 driverName := db.Dialect().DriverName() 24 25 t.Run(driverName, func(t *testing.T) { 26 27 dbTest.Register(&User{}) 28 err := migration.Migrate(db, nil) 29 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 30 31 t.Run("rollback on task err", func(t *testing.T) { 32 taskList := sqlx.NewTasks(db) 33 34 taskList = taskList.With(func(db sqlx.DBExecutor) error { 35 user := User{ 36 Name: uuid.New().String(), 37 Gender: GenderMale, 38 } 39 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 40 return err 41 }) 42 43 taskList = taskList.With(func(db sqlx.DBExecutor) error { 44 subTaskList := sqlx.NewTasks(db) 45 46 subTaskList = subTaskList.With(func(db sqlx.DBExecutor) error { 47 user := User{ 48 Name: uuid.New().String(), 49 Gender: GenderMale, 50 } 51 52 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 53 return err 54 }) 55 56 subTaskList = subTaskList.With(func(db sqlx.DBExecutor) error { 57 return fmt.Errorf("rollback") 58 }) 59 60 return subTaskList.Do() 61 }) 62 63 err := taskList.Do() 64 gomega.NewWithT(t).Expect(err).NotTo(gomega.BeNil()) 65 }) 66 if driverName == "mysql" { 67 t.Run("skip rollback", func(t *testing.T) { 68 taskList := sqlx.NewTasks(db) 69 70 user := User{ 71 Name: uuid.New().String(), 72 Gender: GenderMale, 73 } 74 75 taskList = taskList.With(func(db sqlx.DBExecutor) error { 76 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 77 return err 78 }) 79 80 taskList = taskList.With(func(db sqlx.DBExecutor) error { 81 _, _ = db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 82 return nil 83 }) 84 85 taskList = taskList.With(func(db sqlx.DBExecutor) error { 86 _, err := db.ExecExpr(sqlx.InsertToDB(db, &User{ 87 Name: uuid.New().String(), 88 Gender: GenderMale, 89 }, nil)) 90 return err 91 }) 92 93 err := taskList.Do() 94 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 95 }) 96 } else { 97 t.Run("skip rollback in postgres", func(t *testing.T) { 98 taskList := sqlx.NewTasks(db) 99 100 user := User{ 101 Name: uuid.New().String(), 102 Gender: GenderMale, 103 } 104 105 taskList = taskList.With(func(db sqlx.DBExecutor) error { 106 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 107 return err 108 }) 109 110 taskList = taskList.With(func(db sqlx.DBExecutor) error { 111 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil, builder.OnConflict(builder.Cols("f_name")).DoNothing())) 112 return err 113 }) 114 115 taskList = taskList.With(func(db sqlx.DBExecutor) error { 116 _, err := db.ExecExpr(sqlx.InsertToDB(db, &User{ 117 Name: uuid.New().String(), 118 Gender: GenderMale, 119 }, nil)) 120 return err 121 }) 122 123 err := taskList.Do() 124 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 125 }) 126 } 127 128 t.Run("transaction chain", func(t *testing.T) { 129 taskList := sqlx.NewTasks(db) 130 131 taskList = taskList.With(func(db sqlx.DBExecutor) error { 132 user := User{ 133 Name: uuid.New().String(), 134 Gender: GenderMale, 135 } 136 137 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 138 139 return err 140 }) 141 142 taskList = taskList.With(func(db sqlx.DBExecutor) error { 143 subTaskList := sqlx.NewTasks(db) 144 145 subTaskList = subTaskList.With(func(db sqlx.DBExecutor) error { 146 user := User{ 147 Name: uuid.New().String(), 148 Gender: GenderMale, 149 } 150 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 151 return err 152 }) 153 154 subTaskList = subTaskList.With(func(db sqlx.DBExecutor) error { 155 user := User{ 156 Name: uuid.New().String(), 157 Gender: GenderMale, 158 } 159 _, err := db.ExecExpr(sqlx.InsertToDB(db, &user, nil)) 160 return err 161 }) 162 163 return subTaskList.Do() 164 }) 165 166 err := taskList.Do() 167 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 168 }) 169 db.Tables.Range(func(table *builder.Table, idx int) { 170 _, err := db.ExecExpr(db.Dialect().DropTable(table)) 171 gomega.NewWithT(t).Expect(err).To(gomega.BeNil()) 172 }) 173 }) 174 } 175 }