diff --git a/CHANGELOG.md b/CHANGELOG.md index 67c88a141..99bfb8689 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Retry policy now takes into account query idempotency (CASSGO-27) - Don't return error to caller with RetryType Ignore (CASSGO-28) +- Enable Session.ExecuteBatchCAS() to return underlying scan errors (CASSGO-47) ## [1.7.0] - 2024-09-23 diff --git a/cassandra_test.go b/cassandra_test.go index ec6969190..92f821cdb 100644 --- a/cassandra_test.go +++ b/cassandra_test.go @@ -503,6 +503,18 @@ func TestCAS(t *testing.T) { t.Fatal("scan:", err) } } + + notAppliedBatch := session.Batch(LoggedBatch) + notAppliedBatch.Query("INSERT INTO cas_table (title, revid, last_modified) VALUES (?, ?, ?) IF NOT EXISTS", title, revid, modified) + // This record is already inserted into the table so C* should return [applied] = false and previous record state, + // but we didn't provide any destination variables to handle result, so it should return an error + if applied, _, err := session.ExecuteBatchCAS(notAppliedBatch); err == nil { + t.Fatal("should fail because of lacking of destination variables") + } else if applied { + t.Fatalf("insert should have not been applied") + } else if !strings.Contains(err.Error(), "gocql: not enough columns to scan into") { + t.Fatalf("should have failed because of invalid destination variables, but failed because: %v", err) + } } func TestDurationType(t *testing.T) { diff --git a/session.go b/session.go index d04a13672..42e5121ae 100644 --- a/session.go +++ b/session.go @@ -769,7 +769,7 @@ func (s *Session) ExecuteBatch(batch *Batch) error { // ExecuteBatchCAS executes a batch operation and returns true if successful and // an iterator (to scan additional rows if more than one conditional statement) // was sent. -// Further scans on the interator must also remember to include +// Further scans on the iterator must also remember to include // the applied boolean as the first argument to *Iter.Scan func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bool, iter *Iter, err error) { iter = s.executeBatch(batch) @@ -785,7 +785,7 @@ func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bo iter.Scan(&applied) } - return applied, iter, nil + return applied, iter, iter.err } // MapExecuteBatchCAS executes a batch operation much like ExecuteBatchCAS,