func GenerateNewRecord()

in create_titleid/idmapping.go [76:121]


func GenerateNewRecord(ddbClient *dynamodb.Client, tableName *string, filebase *string, maybeProject *string, maybeOctId *int64, attempt int, contentId int32) (*common.IdMappingRecord, error) {
	if attempt > 100 {
		return nil, errors.New("could not create an ID after 100 attempts, giving up")
	}

	newRecord := &common.IdMappingRecord{
		ContentId:  contentId,
		Filebase:   *filebase,
		Project:    maybeProject,
		Lastupdate: time.Now(),
		Octopus_id: maybeOctId,
	}

	newRecord.RegenerateUUID()

	//we will only create a record if there is not a pre-existing primary key
	condition, err := expression.NewBuilder().WithCondition(expression.AttributeNotExists(expression.Name("uuid"))).Build()
	if err != nil {
		return nil, err
	}

	params := dynamodb.PutItemInput{
		Item:                      newRecord.ToDynamoRecord(),
		TableName:                 tableName,
		ConditionExpression:       condition.Condition(),
		ExpressionAttributeValues: condition.Values(),
		ExpressionAttributeNames:  condition.Names(),
	}
	_, err = ddbClient.PutItem(context.Background(), &params)

	if err != nil {
		var ae smithy.APIError
		if errors.As(err, &ae) {
			if ae.ErrorCode() == "ConditionalCheckFailedException" {
				log.Printf("DEBUG Detected ID conflict on generated number %d, re-trying", newRecord.ContentId)
				return GenerateNewRecord(ddbClient, tableName, filebase, maybeProject, maybeOctId, attempt+1, contentId)
			} else {
				return nil, err
			}
		} else {
			return nil, err
		}
	} else {
		return newRecord, nil
	}
}