Skip to content

Instantly share code, notes, and snippets.

@nickva
Last active February 10, 2022 19:45
Show Gist options
  • Save nickva/8a25cf63ffbcd68401659ed91a100127 to your computer and use it in GitHub Desktop.
Save nickva/8a25cf63ffbcd68401659ed91a100127 to your computer and use it in GitHub Desktop.
Prevent regular doc writes to the replication source
#!/bin/sh
# Prevent regular doc writes to the replication source. Used, for example, when
# migrating to a new instance and we want to prevent clients writing to the old
# instance by accident. Design doc writes and all doc reads can still take
# place.
#
# (`http` is the httpie client https://httpie.io/)
DB="http://adm:pass@localhost:15984"
# Reset everything
http -q delete $DB/old
http -q delete $DB/new
http -q delete $DB/_replicator
http -q put $DB/_replicator
set -xe
http -b put $DB/old
http -b put $DB/old/old1 a=b
# Create a VDU to forbid all regular doc writes. Instead of null could emit a
# message where the new instance is.
http -b put $DB/old/_design/rovdu \
validate_doc_update:='"function(newDoc, oldDoc, userCtx){throw({forbidden: null});}"'
# Make sure not to replicate the VDU to the target
http -b put $DB/_replicator/old2new \
source=$DB/old \
target=$DB/new \
selector:='{"$not": {"_id": "_design/rovdu"}}' \
continuous:='true' \
create_target:='true'
# Replication jobs have a random 5 second startup jitter
sleep 6
http -b get $DB/new/_all_docs
# Let's try writing a doc to the source. This should fail with non-0 exit code
http --check-status put $DB/old/old2 c=d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment