From e88547c47db3f507bde454b7b40ee94def7f488b Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Mon, 29 Aug 2016 19:01:54 -0400 Subject: [PATCH] Initial fixes, bump the version --- metrik/__init__.py | 2 +- metrik/merge.py | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/metrik/__init__.py b/metrik/__init__.py index c195454..9728c0a 100644 --- a/metrik/__init__.py +++ b/metrik/__init__.py @@ -1,2 +1,2 @@ -__version__ = '0.3.7' +__version__ = '0.3.8' __release__ = __version__ \ No newline at end of file diff --git a/metrik/merge.py b/metrik/merge.py index 53265ff..d351d04 100644 --- a/metrik/merge.py +++ b/metrik/merge.py @@ -2,6 +2,8 @@ from pymongo import MongoClient import logging import argparse +from metrik import __version__ + def open_connection(host, port): return MongoClient(host=host, port=port) @@ -10,26 +12,26 @@ def open_connection(host, port): def merge(con1, con2, database_name='metrik'): database1 = con1[database_name] database2 = con2[database_name] - collections = database1.collection_names() + collections = database1.collection_names(include_system_collections=False) for collection_name in collections: collection1 = database1[collection_name] collection2 = database2[collection_name] for item in collection1.find(): - if collection2.find({'_id': item['_id']}) is None: + if '_id' in item and collection2.find_one({'_id': item['_id']}) is None: collection2.save(item) collection1.delete_one({'_id': item['_id']}) else: - logging.warning('Not moving item {} as the same ID already' - ' exists in the `right` database.'.format( - item['_id'] + logging.warning("Not moving item '{}' as the same ID already" + " exists in the `right` database.".format( + item.get('_id', '') )) def main(): parser = argparse.ArgumentParser() - parser.add_argument('-h', '--host-1', dest='host1', + parser.add_argument('-g', '--host-1', dest='host1', required=True, help='The `left` database to copy from') - parser.add_argument('-g', '--host-2', dest='host2', + parser.add_argument('-f', '--host-2', dest='host2', required=True, help='The `right` database to copy into') parser.add_argument('-p', '--port-1', default=27017, dest='port1', type=int, help='The port number of the `left` database') @@ -37,10 +39,14 @@ def main(): help='The port number of the `right` database') parser.add_argument('-d', '--database', default='metrik', help='The database to merge from one host to the other') + parser.add_argument('-v', '--version', action='version', version=__version__) args = parser.parse_args() + con1 = open_connection(args.host1, args.port1) con2 = open_connection(args.host2, args.port2) merge(con1, con2, args.database) + con1.close() + con2.close() if __name__ == '__main__':