@@ -164,9 +164,14 @@ func seedAuditLogs(db *sql.DB, rng *rand.Rand, n int) {
164164
165165// bulkInsert inserts n rows into table, committing every txSize rows.
166166func bulkInsert (db * sql.DB , n , txSize int , table , cols string , row func (i int ) []any ) {
167- // Enable WAL + relaxed sync for fast inserts.
168- db .Exec ("PRAGMA journal_mode=WAL" )
169- db .Exec ("PRAGMA synchronous=NORMAL" )
167+ // Enable WAL + relaxed sync for fast inserts. Errors here are non-fatal;
168+ // inserts will still succeed, just potentially slower.
169+ if _ , err := db .Exec ("PRAGMA journal_mode=WAL" ); err != nil {
170+ log .Printf ("warn: WAL pragma on %s: %v" , table , err )
171+ }
172+ if _ , err := db .Exec ("PRAGMA synchronous=NORMAL" ); err != nil {
173+ log .Printf ("warn: synchronous pragma on %s: %v" , table , err )
174+ }
170175
171176 // Count placeholders from cols.
172177 count := 1
@@ -179,20 +184,38 @@ func bulkInsert(db *sql.DB, n, txSize int, table, cols string, row func(i int) [
179184 for i := 1 ; i < count ; i ++ {
180185 ph += ",?"
181186 }
182- sql := fmt .Sprintf ("INSERT INTO %s (%s) VALUES (%s)" , table , cols , ph )
187+ insertSQL := fmt .Sprintf ("INSERT INTO %s (%s) VALUES (%s)" , table , cols , ph )
183188
184- tx , _ := db .Begin ()
185- stmt , _ := tx .Prepare (sql )
189+ tx , err := db .Begin ()
190+ if err != nil {
191+ log .Fatalf ("begin tx for %s: %v" , table , err )
192+ }
193+ stmt , err := tx .Prepare (insertSQL )
194+ if err != nil {
195+ log .Fatalf ("prepare stmt for %s: %v" , table , err )
196+ }
186197 for i := 1 ; i <= n ; i ++ {
187- stmt .Exec (row (i )... )
198+ if _ , err := stmt .Exec (row (i )... ); err != nil {
199+ log .Fatalf ("insert row %d into %s: %v" , i , table , err )
200+ }
188201 if i % txSize == 0 {
189- tx .Commit ()
190- tx , _ = db .Begin ()
191- stmt , _ = tx .Prepare (sql )
202+ if err := tx .Commit (); err != nil {
203+ log .Fatalf ("commit tx for %s at row %d: %v" , table , i , err )
204+ }
205+ tx , err = db .Begin ()
206+ if err != nil {
207+ log .Fatalf ("begin tx for %s: %v" , table , err )
208+ }
209+ stmt , err = tx .Prepare (insertSQL )
210+ if err != nil {
211+ log .Fatalf ("prepare stmt for %s: %v" , table , err )
212+ }
192213 log .Printf (" %s: %d / %d" , table , i , n )
193214 }
194215 }
195- tx .Commit ()
216+ if err := tx .Commit (); err != nil {
217+ log .Fatalf ("final commit for %s: %v" , table , err )
218+ }
196219 log .Printf (" %s: %d / %d (done)" , table , n , n )
197220}
198221
@@ -225,10 +248,12 @@ func mutateDev(dev *sql.DB, rng *rand.Rand, oRows, pRows int) {
225248 ts := time .Now ().UTC ().Format (time .RFC3339 )
226249 for i := 0 ; i < 3 ; i ++ {
227250 id := oRows + 1 + i
228- dev .Exec (`INSERT INTO orders (id,customer_id,product_id,quantity,amount,status,created_at,updated_at)
229- VALUES (?,?,?,?,?,?,?,?)` ,
251+ if _ , err := dev .Exec (
252+ `INSERT INTO orders (id,customer_id,product_id,quantity,amount,status,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?)` ,
230253 id , rng .Intn (50_000 )+ 1 , rng .Intn (10_000 )+ 1 , 1 , 99.99 , "pending" , ts , ts ,
231- )
254+ ); err != nil {
255+ log .Printf ("warn: insert new dev order %d: %v" , id , err )
256+ }
232257 }
233258
234259 log .Println (" 10 order amounts changed, 5 product prices changed, 3 new orders in dev" )
0 commit comments