Skip to content

python sdk 4.3.5 collection.repalce issue #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Tan-Fei-Fei opened this issue Apr 8, 2025 · 1 comment
Open

python sdk 4.3.5 collection.repalce issue #62

Tan-Fei-Fei opened this issue Apr 8, 2025 · 1 comment

Comments

@Tan-Fei-Fei
Copy link

replace(
key=new_data["id"],
value=new_data,
opts=ReplaceOptions(
# cas=old.cas, # This parameter does not work as expected (does not take effect) in Couchbase Python SDK 4.3.5
durability=ServerDurability(
level=DurabilityLevel.PERSIST_TO_MAJORITY
)
),
cas=old_cas, # This parameter works as expected (takes effect) in Couchbase Python SDK 4.3.5
)

Explanation:

In the Couchbase Python SDK 4.3.5, there is an issue where specifying the 'cas' parameter inside the ReplaceOptions object

(e.g., cas=old.cas) does not function correctly and does not take effect. However, passing the 'cas' parameter directly

to the replace function (outside of the ReplaceOptions object) works as intended.

@thejcfactor
Copy link
Contributor

Hi @Tan-Fei-Fei -- Can you provide some more details on how you are validating if the CAS is working properly?

Maybe there is a typo in your example? What does not work is cas=old.cas and what does work is cas=old_cas which do not appear to be the same thing.

We test cas w/ various operation's options blocks (including replace()), so I would be surprised if passing in cas to ReplaceOptions is not working. I have provided a small sample below and the output is as I would expect (e.g. no CasMismatchException when providing the correct cas and then a CasMismatchException with an incorrect cas).

from couchbase.auth import PasswordAuthenticator
from couchbase.cluster import Cluster
from couchbase.exceptions import CasMismatchException
from couchbase.options import ClusterOptions, ReplaceOptions

def main() -> None:
    # start user info
    endpoint = 'localhost'
    username = 'Administrator'
    password = 'password'
    bucket_name = 'default'
    connstr = f'couchbase://{endpoint}'
    # end user info

    auth = PasswordAuthenticator(username, password)
    cluster = Cluster(connstr, ClusterOptions(auth))
    bucket = cluster.bucket("default")
    collection = bucket.default_collection()

    test_key = 'test-key'
    collection.upsert(test_key, {'foo': 'bar'})
    get_res = collection.get(test_key)
    print(f'Get result: {get_res.content_as[str]}')
    get_cas = get_res.cas
    print(f'CAS: {get_cas}')
    collection.replace(test_key, {'foo': 'baz'}, ReplaceOptions(cas=get_cas))
    get_res1 = collection.get(test_key)
    print(f'Get result: {get_res1.content_as[str]}')
    collection.upsert(test_key, {'foo': 'bar'})
    try:
        collection.replace(test_key, {'foo': 'baz'}, ReplaceOptions(cas=get_cas))
    except CasMismatchException as e:
        print(f'CAS mismatch exception: {e}')
    collection.remove(test_key)


if __name__ == '__main__':
    main()

output (error output is reduced for simplicity):

Get result: {'foo': 'bar'}
CAS: 1744679141054808064
Get result: {'foo': 'baz'}
CAS mismatch exception: CasMismatchException(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants