def push_initial_data(connection, overwrite=False): documents = applications.get_initial_data_registry().itervalues() for doc in documents: try: yield connection.save_document(doc) except ConflictError: if not overwrite: log.error('script', 'Document with id %s already exists!', doc.doc_id) else: yield _update_old(connection, doc) design_docs = view.generate_design_docs() for design_doc in design_docs: try: yield connection.save_document(design_doc) except ConflictError: yield _update_old(connection, design_doc)
def push_initial_data(connection, overwrite=False, push_design_docs=True): documents = applications.get_initial_data_registry().itervalues() for doc in documents: try: yield connection.save_document(doc) except ConflictError: fetched = yield connection.get_document(doc.doc_id) if fetched.compare_content(doc): continue if not overwrite: log.warning('script', 'Document with id %s already exists! ' 'Use --force, Luck!', doc.doc_id) else: log.info('script', 'Updating old version of the document, ' 'id: %s', doc.doc_id) rev = yield connection.get_revision(doc.doc_id) doc.rev = rev yield connection.save_document(doc) if not push_design_docs: return design_docs = view.generate_design_docs() for design_doc in design_docs: try: yield connection.save_document(design_doc) except ConflictError: fetched = yield connection.get_document(design_doc.doc_id) if fetched.compare_content(design_doc): continue log.warning('script', 'The design document %s changed. ' 'Use "feat-service upgrade" to push the new revisions ' 'and restart the service in organised manner.', design_doc.doc_id) # calculate a diff for debugging purpose diffs = dict() for what in ('views', 'filters'): diffs[what] = dict() a = getattr(design_doc, what) b = getattr(fetched, what) diff = set(a.keys()) - set(b.keys()) for key in diff: diffs[what][key] = (a[key], None) diff = set(b.keys()) - set(a.keys()) for key in diff: diffs[what][key] = (None, b[key]) for name in set(a.keys()).intersection(set(b.keys())): if a[name] != b[name]: diffs[what][name] = (a[name], b[name]) def strcode(x): if not x: return '' if isinstance(x, (str, unicode)): return x return "\n".join("%s: %s" % t for t in x.items()) for what in diffs: for name in diffs[what]: log.info('script', '%s code changed. \nOLD: \n%s\n\nNEW:\n%s\n', what, strcode(diffs[what][name][1]), strcode(diffs[what][name][0]))