Created
March 16, 2011 22:57
-
-
Save anonymous/873505 to your computer and use it in GitHub Desktop.
compare 2 dbs, maybe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from pymongo import Connection | |
import subprocess | |
import sys | |
def advance(cur) | |
try: | |
return cur.next() | |
except StopIteration: | |
return None | |
def main(): | |
# Master ought to come first. | |
(host1, host2, dbname1) = sys.argv[1:5] | |
# These are hooey that, by murphy's law, I'd have to write if I | |
# didn't write them. | |
if (len(sys.argv)) > 4: | |
dbname2 = sys.argv[5] | |
else: | |
dbname2 = dbname1 | |
if (len(sys.argv)) > 5: | |
port1 = sys.argv[6] | |
else: | |
port1="27017" | |
if (len(sys.argv)) > 6: | |
port2 = sys.argv[7] | |
else: | |
port2="27017" | |
# Make two connections, | |
conn1 = Connection(host1, int(port1)) | |
conn2 = Connection(host2, int(port2)) | |
# Get two dbs | |
db1 = conn1[dbname1] | |
db2 = conn2[dbname2] | |
for collname in db1.collection_names(): | |
msg1="Missing from %s: %s %%s" % (host1, collname) | |
msg2="Missing from %s: %s %%s" % (host2, collname) | |
# Get two collections. | |
coll1=db1[collname] | |
coll2=db2[collname] | |
# Get two cursors. | |
cur1=coll1.find(snapshot=True).sort("_id") | |
cur2=coll2.find(snapshot=True).sort("_id") | |
while True: | |
doc1 = advance(cur1) | |
if not doc1: # no more in cur1, exhaust cur2 | |
for doc2 in cur2: | |
print msg1 % doc2["_id"] | |
break | |
doc2 = advance(cur2) | |
if not doc2: | |
for doc1 in cur1: | |
print msg2 % doc1["_id"] | |
break | |
id1=doc1["_id"] | |
id2=doc2["_id"] | |
if id1 == id2: # same obj | |
if doc1 != doc2: | |
print "Skew: %s %s" % (collname, id1) | |
elif (id1 < id2): | |
print msg1 % id1 | |
for doc1 in cur1: | |
id1 = doc1["_id"] | |
if id1 == id2: | |
break | |
else: | |
print msg1 % id1 | |
else: # id1 > id2 | |
print msg2 % id2 | |
for doc2 in cur2: | |
id2 = doc2["_id"] | |
if id1 == id2: | |
break | |
else: | |
print msg2 % id2 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment