diff --git a/erpnext/patches/repair_tools/fix_naming_series_records_lost_by_reload.py b/erpnext/patches/repair_tools/fix_naming_series_records_lost_by_reload.py index 7fb54b3cc8..981ffd0c29 100644 --- a/erpnext/patches/repair_tools/fix_naming_series_records_lost_by_reload.py +++ b/erpnext/patches/repair_tools/fix_naming_series_records_lost_by_reload.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals import frappe import json +import re from frappe.model.naming import make_autoname from frappe.utils import cint from frappe.utils.email_lib import sendmail_to_system_managers @@ -202,3 +203,45 @@ def rename_docs(): sendmail_to_system_managers("[Important] [ERPNext] Renamed Documents via Patch", content) +def fix_comments(): + renamed_docs_comments = frappe.db.sql("""select name, comment, comment_doctype, comment_docname + from `tabComment` where comment like 'Renamed from **%** to %' + order by comment_doctype, comment_docname""", as_dict=True) + + # { "comment_doctype": [("old_comment_docname", "new_comment_docname", ['comment1', 'comment2', ...])] } + comments_to_rename = {} + + for comment in renamed_docs_comments: + old_comment_docname, new_comment_docname = re.findall("""Renamed from \*\*([^\*]*)\*\* to (.*)""", comment.comment)[0] + if not frappe.db.exists(comment.comment_doctype, old_comment_docname): + orphaned_comments = frappe.db.sql_list("""select comment from `tabComment` + where comment_doctype=%s and comment_docname=%s""", (comment.comment_doctype, old_comment_docname)) + if orphaned_comments: + to_rename = (old_comment_docname, new_comment_docname, orphaned_comments) + comments_to_rename.setdefault(comment.comment_doctype, []).append(to_rename) + + for doctype in comments_to_rename: + if not comments_to_rename[doctype]: + continue + + print + print "Fix comments for", doctype, ":" + for (old_comment_docname, new_comment_docname, comments) in comments_to_rename[doctype]: + print + print old_comment_docname, "-->", new_comment_docname + print "\n".join(comments) + + print + confirm = raw_input("do it? (yes / anything else): ") + if confirm=="yes": + for (old_comment_docname, new_comment_docname, comments) in comments_to_rename[doctype]: + fix_comment(doctype, old_comment_docname, new_comment_docname) + print "Fixed" + + frappe.db.commit() + +def fix_comment(comment_doctype, old_comment_docname, new_comment_docname): + frappe.db.sql("""update `tabComment` set comment_docname=%s + where comment_doctype=%s and comment_docname=%s""", + (new_comment_docname, comment_doctype, old_comment_docname)) + diff --git a/erpnext/selling/doctype/customer/test_customer.py b/erpnext/selling/doctype/customer/test_customer.py index e2273bc59c..50ec56515c 100644 --- a/erpnext/selling/doctype/customer/test_customer.py +++ b/erpnext/selling/doctype/customer/test_customer.py @@ -41,11 +41,27 @@ class TestCustomer(unittest.TestCase): self.assertEquals(value, details.get(key)) def test_rename(self): + for name in ("_Test Customer 1", "_Test Customer 1 Renamed"): + frappe.db.sql("""delete from `tabComment` where comment_doctype=%s and comment_docname=%s""", + ("Customer", name)) + + comment = frappe.new_doc("Comment") + comment.update({ + "comment": "Test Comment for Rename", + "comment_doctype": "Customer", + "comment_docname": "_Test Customer 1" + }) + comment.insert() + frappe.rename_doc("Customer", "_Test Customer 1", "_Test Customer 1 Renamed") self.assertTrue(frappe.db.exists("Customer", "_Test Customer 1 Renamed")) self.assertFalse(frappe.db.exists("Customer", "_Test Customer 1")) + # test that comment gets renamed + self.assertEquals(frappe.db.get_value("Comment", + {"comment_doctype": "Customer", "comment_docname": "_Test Customer 1 Renamed"}), comment.name) + frappe.rename_doc("Customer", "_Test Customer 1 Renamed", "_Test Customer 1")