voutasaurus
voutasaurus

Reputation: 3288

dynamodb updateitem with conditionexpression return whether item updated

I would like to update an item under certain conditions and then I would like to know whether the item was updated when UpdateItem returns.

The documentation seems contradictory to me.

On this page: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html in the "Conditional Update" example it says "All of the item's attributes, as they appear after the update, are returned in the response."

On this page: https://godoc.org/github.com/aws/aws-sdk-go/service/dynamodb#UpdateItemOutput it says that Attributes is "A map of attribute values as they appeared before the UpdateItem operation"

I don't really want either of these. What I want is a bool that says whether or not there was an update.

This is where my brain is at now:

out, err := db.DynamoDB.UpdateItem(&dynamodb.UpdateItemInput{
    TableName: tableName,
    Key: map[string]*dynamodb.AttributeValue{
        "KeyName": {S: aws.String(keyname)},
    },
    ExpressionAttributeNames: map[string]*string{
        "#lock": aws.String("Lock"),
    },
    ExpressionAttributeValues: map[string]*string{
        ":now":     aws.String(compfmt(time.Now())),
        ":promise": aws.String(compfmt(time.Now().Add(30 * time.Second))),
    },
    ConditionExpression: aws.String("attribute_not_exist(#lock) OR :now > #lock"),
    UpdateExpression:    aws.String("SET #lock = :promise"),
})

Upvotes: 3

Views: 8378

Answers (3)

Carlos Martinez
Carlos Martinez

Reputation: 4510

There are now constants to compare the errors rather than using the hardcoded string as in other answers:

result, err := svc.UpdateItem(input)
if err != nil {
    if aerr, ok := err.(awserr.Error); ok {
        switch aerr.Code() {
        case dynamodb.ErrCodeConditionalCheckFailedException:
            fmt.Println(dynamodb.ErrCodeConditionalCheckFailedException, aerr.Error())
        default:
            fmt.Println(aerr.Error())
        }
    }
}

Upvotes: 3

Andy McCall
Andy McCall

Reputation: 464

One way to do this is to check the Code on the awserr

import "github.com/aws/aws-sdk-go/aws/awserr"
func Lock()(bool, error) {
    //Create value v
    _, err := db.DynamoDB.UpdateItem(v)
    if err != nil {
         if ae, ok := err.(awserr.RequestFailure); ok && ae.Code() == "ConditionalCheckFailedException" {
             return false, nil
         }
        return false, err
    }
    return true, nil
}

Upvotes: 5

voutasaurus
voutasaurus

Reputation: 3288

Turns out what I wanted to do was check the error to see if it contained the string ConditionalCheckFailedException.

func Lock() (bool, error) {
  ...
  _, err := db.DynamoDB.UpdateItem(v)
  if err != nil {
    if strings.Contains(err.Error(), "ConditionalCheckFailedException") {
      return false, nil
    }
    return false, err
  }
  return true, nil
}

Upvotes: 1

Related Questions