[Bug]: Attachments are not correctly replicated when a document is recreated with same id and attachment name
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 40/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- docker, erlang
- Domain
- databases
Research direction
The issue is in CouchDB's replication logic for attachments. Start by examining the replication module (likely in src/couch_replicator or similar). Look at how attachments are handled when a document is deleted and recreated with the same ID. The test steps provide a reproducible scenario; run the docker commands to see the bug. Check the attachment digest and revpos fields during replication. The fix likely involves ensuring attachment data is updated correctly when a document revision is replicated after a deletion.
Written by the indexing model from the issue text.
Description
Version
3.5.2
Describe the problem you're encountering
When a document with an attachment is deleted and then recreated with the same id and attachment name but different contents, the updated attachment data is not replicated correctly if the deletion is not replicated separately first. This happens with both transient and continuous replication if continuous replication fails for some reason (like temporary connection issue) before the new document is inserted.
Expected Behaviour
I would expect attachment data to be updated in the replication target similarly to how it's updated in the source.
I would probably also expect that when recreating a document with an attachment, the revpos field in _attachments would match the revision number, but this isn't really something I care about as a user.
Steps to Reproduce
These steps require docker and jq and use transient replication for convenience.
Start two couchdb docker containers on the same docker network:
docker network create couchdb-test
docker run --name couch1 -d -p 5984:5984 --network couchdb-test -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=admin couchdb
docker run --name couch2 -d -p 5985:5984 --network couchdb-test -e COUCHDB_USER=admin -e COUCHDB_PASSWORD=admin couchdb
Create test database in couch1 with a single document with an attachment:
curl -u admin:admin -X PUT 'http://localhost:5984/test'
curl -u admin:admin -X PUT 'http://localhost:5984/test/testdoc' \
-H 'Content-Type: application/json' \
-d '{"_id":"testdoc","_attachments":{"file":{"content_type":"text/plain","data":"YWFhYWEK"}}}'
Replicate test from couch1 to couch2 and check the data:
curl -u admin:admin -X POST 'http://localhost:5984/_replicate' \
-H 'Content-Type: application/json' \
-d '{"source":"http://admin:admin@couch1:5984/test","target":"http://admin:admin@couch2:5984/test","create_target":true,"continuous":false}'
curl -s -u admin:admin "http://localhost:5984/test/testdoc" | jq
# {
# "_id": "testdoc",
# "_rev": "1-186e7841f8df12ab5cf28dad9d2033b8",
# "_attachments": {
# "file": {
# "content_type": "text/plain",
# "revpos": 1,
# "digest": "md5-oQV6i1d2MQFpml+YQVk7QA==",
# "length": 5,
# "stub": true
# }
# }
# }
curl -s -u admin:admin "http://localhost:5985/test/testdoc" | jq
# {
# "_id": "testdoc",
# "_rev": "1-186e7841f8df12ab5cf28dad9d2033b8",
# "_attachments": {
# "file": {
# "content_type": "text/plain",
# "revpos": 1,
# "digest": "md5-oQV6i1d2MQFpml+YQVk7QA==",
# "length": 5,
# "stub": true
# }
# }
# }
Delete the document in couch1 and recreate it with different attachment contents:
curl -u admin:admin -X DELETE \
"http://localhost:5984/test/testdoc?rev=$(curl -s -u admin:admin "http://localhost:5984/test/testdoc" | jq '._rev')"
curl -u admin:admin -X PUT 'http://localhost:5984/test/testdoc' \
-H 'Content-Type: application/json' \
-d '{"_id":"testdoc","_attachments":{"file":{"content_type":"text/plain","data":"YmJiYmIK"}}}'
Replicate test from couch1 to couch2 and check the data:
# Replicate "test" database from couch1 to couch2
curl -u admin:admin -X POST 'http://localhost:5984/_replicate' \
-H 'Content-Type: application/json' \
-d '{"source":"http://admin:admin@couch1:5984/test","target":"http://admin:admin@couch2:5984/test","create_target":true,"continuous":false}'
curl -s -u admin:admin "http://localhost:5984/test/testdoc" | jq
# {
# "_id": "testdoc",
# "_rev": "3-95d08e7c58dd79d6617d19f49bf836e0",
# "_attachments": {
# "file": {
# "content_type": "text/plain",
# "revpos": 1,
# "digest": "md5-imQ12rhoOgQEok8WoOwkyg==",
# "length": 6,
# "stub": true
# }
# }
# }
curl -s -u admin:admin "http://localhost:5985/test/testdoc" | jq
# {
# "_id": "testdoc",
# "_rev": "3-95d08e7c58dd79d6617d19f49bf836e0",
# "_attachments": {
# "file": {
# "content_type": "text/plain",
# "revpos": 1,
# "digest": "md5-oQV6i1d2MQFpml+YQVk7QA==",
# "length": 5,
# "stub": true
# }
# }
# }
Document revisions match, but attachment data differs!
Clean up the docker containers and networks:
docker rm $(docker stop couch1 couch2)
docker network rm couchdb-test
Your Environment
We are running couchdb in docker in a single node setup with database instances replicating data between each other in ad hoc "clusters".
Additional Context
I'm happy to provide more context if needed.
- Dominant language
- Erlang
- Stars
- 7k
- Forks
- 1.1k
- Avg merge
- 5h 24m
- Merged PRs (30d)
- 10
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from apache/couchdb
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 40/100
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 45/100
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
enhancement needs-triage
Difficulty 3/5 1-2 days Newbie friendliness 55/100
-
bug needs-triage
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
xinnan-tech/xiaozhi-fde-talk#263 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 90/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
avniproject/avni-server#1074 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100