@@ -0,0 +1,2289 @@
+#!/usr/bin/env python%0Aimport argparse%0A%0Aimport pymongo%0A%0Afrom bhs_api import create_app%0Afrom bhs_api.utils import SEARCHABLE_COLLECTIONS%0Afrom bhs_api.item import get_collection_id_field, get_item_slug%0A%0A%0Adef parse_args():%0A    parser = argparse.ArgumentParser(description=%0A                    'copy the slugs from one db to another.%5C%0AThe mongo host is specified in app_server.yaml')%0A    parser.add_argument('--fromdb',%0A                        help='the db from which to copy the slugs')%0A    return parser.parse_args()%0A%0Aif __name__ == '__main__':%0A%0A    args = parse_args()%0A    app, conf = create_app()%0A    fromdb = app.client_data_db%5Bargs.fromdb%5D%0A    todb = app.data_db%0A%0A%0A    for c_name in SEARCHABLE_COLLECTIONS:%0A        print(%22starting work on %22 + c_name)%0A        # in the process we might create duplicate index so remove them for now%0A        try:%0A            todb%5Bc_name%5D.drop_index('Slug.He_1')%0A        except pymongo.errors.OperationFailure:%0A            pass%0A        try:%0A            todb%5Bc_name%5D.drop_index('Slug.En_1')%0A        except pymongo.errors.OperationFailure:%0A            pass%0A        id_field = get_collection_id_field(c_name)%0A        # loop on all docs with a slug%0A        for from_doc in fromdb%5Bc_name%5D.find(%7B'Slug': %7B'$exists': True,%0A                                                     '$ne': %7B%7D%7D%7D):%0A            to_doc = app.data_db%5Bc_name%5D.find_one(%0A                %7Bid_field: from_doc%5Bid_field%5D%7D)%0A            if not to_doc:%0A                print(%22missing %7B%7D%22.format(get_item_slug(from_doc)))%0A                continue%0A            if from_doc%5B'Slug'%5D != to_doc%5B'Slug'%5D:%0A                try:%0A                    todb%5Bc_name%5D.update_one(%7B'_id': to_doc%5B'_id'%5D%7D,%0A                                                %7B'$set':%0A                                                    %7B'Slug': from_doc%5B'Slug'%5D%7D%0A                                                %7D)%0A                except pymongo.errors.DuplicateKeyError as e:%0A                    import pdb; pdb.set_trace()%0A                print('changed %7B%7D to %7B%7D'%0A                    .format(get_item_slug(to_doc).encode('utf8'),%0A                            get_item_slug(from_doc).encode('utf8')))%0A        todb%5Bc_name%5D.create_index(%22Slug.He%22, unique=True, sparse=True)%0A        todb%5Bc_name%5D.create_index(%22Slug.En%22, unique=True, sparse=True)%0A%0A

add the copy_slugs script