Skip to content

Commit 8e70b05

Browse files
committed
Enable Session.ExecuteBatchCAS() to return underlying scan errors
The patch enables Session.ExecuteBatchCAS() to return an error which may happen during rows scanning. Patch by Bohdan Siryk; reviewed by <> for CASSGO-47
1 parent 37030fb commit 8e70b05

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222

2323
- Retry policy now takes into account query idempotency (CASSGO-27)
2424
- Don't return error to caller with RetryType Ignore (CASSGO-28)
25+
- Enable Session.ExecuteBatchCAS() to return underlying scan errors (CASSGO-47)
2526

2627
## [1.7.0] - 2024-09-23
2728

cassandra_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,18 @@ func TestCAS(t *testing.T) {
503503
t.Fatal("scan:", err)
504504
}
505505
}
506+
507+
notAppliedBatch := session.Batch(LoggedBatch)
508+
notAppliedBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES (?, ?, ?) IF NOT EXISTS", title, revid, modified)
509+
// This record is already inserted into the table so C* should return [applied] = false and previous record state,
510+
// but we didn't provide any destination variables to handle result, so it should return an error
511+
if applied, _, err := session.ExecuteBatchCAS(successBatch); err == nil {
512+
t.Fatal("should fail because of lacking of destination variables")
513+
} else if applied {
514+
t.Fatalf("insert should have not been applied")
515+
} else if !strings.Contains(err.Error(), "gocql: not enough columns to scan into") {
516+
t.Fatalf("should have failed because of invalid destination variables, but failed because: %v", err)
517+
}
506518
}
507519

508520
func TestDurationType(t *testing.T) {

session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ func (s *Session) ExecuteBatch(batch *Batch) error {
769769
// ExecuteBatchCAS executes a batch operation and returns true if successful and
770770
// an iterator (to scan additional rows if more than one conditional statement)
771771
// was sent.
772-
// Further scans on the interator must also remember to include
772+
// Further scans on the iterator must also remember to include
773773
// the applied boolean as the first argument to *Iter.Scan
774774
func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bool, iter *Iter, err error) {
775775
iter = s.executeBatch(batch)
@@ -785,7 +785,7 @@ func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bo
785785
iter.Scan(&applied)
786786
}
787787

788-
return applied, iter, nil
788+
return applied, iter, iter.err
789789
}
790790

791791
// MapExecuteBatchCAS executes a batch operation much like ExecuteBatchCAS,

0 commit comments

Comments
 (0)