Skip to content

Inconsistent Behavior of Couchbase Counters Across PHP SDK Versions #212

Open
@deminy

Description

@deminy

In legacy PHP SDKs (v2 and v3) for Couchbase, counters can be set as string values, which contrasts with
the behavior in PHP SDK v4 where counters must be strictly integers. This discrepancy can cause unexpected
exceptions, such as DeltaInvalidException, during migration from older SDK versions to v4.

Steps to reproduce the issue: https://github.com/deminy/couchbase-issues/tree/couchbase4/issue-5

Test code used:

<?php

/**
 * This script tests inconsistent behavior of counter operations across PHP SDK v3 and v4.
 *
 * Usage:
 *     docker compose exec -ti client-v3 php ./test.php # for PHP SDK v3
 *     docker compose exec -ti client-v4 php ./test.php # for PHP SDK v4
 */

declare(strict_types=1);

if (is_readable($_SERVER['HOME'] . '/.composer/vendor/autoload.php')) {
    // Load PHP SDK of Couchbase v4 if available.
    require_once $_SERVER['HOME'] . '/.composer/vendor/autoload.php';
}

use Couchbase\Cluster;
use Couchbase\ClusterOptions;
use Couchbase\IncrementOptions;

$options = new ClusterOptions();
$options->credentials('username', 'password');
$cluster    = new Cluster('couchbase://server', $options);
$collection = $cluster->bucket('test')->defaultCollection();
$key        = 'counter';

// First, try to remove the counter if exists.
$collection->removeMulti([$key]);

// Second, insert the counter with a STRING value but not an INTEGER value.
$collection->insert($key, '3');
var_dump($collection->get($key)->content());

// Now, try to increment the counter.
//
// It works fine in PHP SDK v3, but fails in PHP SDK v4 with the following exception thrown out:
//   Couchbase\Exception\DeltaInvalidException: delta_invalid (122): "unable to execute increment"
$collection->binary()->increment($key, new IncrementOptions());
var_dump($collection->get($key)->content());

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions