--- /dev/null
+Return-Path: <aclements@csail.mit.edu>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 1A5FD429E2F\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 07:19:45 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id PRYSfKQx5Qfk for <notmuch@notmuchmail.org>;\r
+ Fri, 3 Oct 2014 07:19:38 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-6.mit.edu (dmz-mailsec-scanner-6.mit.edu\r
+ [18.7.68.35])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 3AE2A431FCF\r
+ for <notmuch@notmuchmail.org>; Fri, 3 Oct 2014 07:19:26 -0700 (PDT)\r
+X-AuditID: 12074423-f799d6d00000337c-eb-542eb06dbcd7\r
+Received: from mailhub-auth-3.mit.edu ( [18.9.21.43])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-6.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id A3.B6.13180.D60BE245; Fri, 3 Oct 2014 10:19:25 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-3.mit.edu (8.13.8/8.9.2) with ESMTP id s93EJO7q018553; \r
+ Fri, 3 Oct 2014 10:19:25 -0400\r
+Received: from drake.dyndns.org\r
+ (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
+ [216.15.114.40]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id s93EJLNP023322\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Fri, 3 Oct 2014 10:19:24 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+ (envelope-from <aclements@csail.mit.edu>)\r
+ id 1Xa3hZ-0002K7-9F; Fri, 03 Oct 2014 10:19:21 -0400\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 08/11] lib: Implement upgrade to ghost messages feature\r
+Date: Fri, 3 Oct 2014 10:19:15 -0400\r
+Message-Id: <1412345958-8278-9-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1412345958-8278-1-git-send-email-aclements@csail.mit.edu>\r
+References: <1412345958-8278-1-git-send-email-aclements@csail.mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrIIsWRmVeSWpSXmKPExsUixCmqrZu7QS/E4PBWaYvrN2cyOzB6PFt1\r
+ izmAMYrLJiU1J7MstUjfLoErY+HO7YwFB2QqPk2fztTAuEusi5GTQ0LAROLbol5GCFtM4sK9\r
+ 9WxdjFwcQgKzmST+tm9ignA2MEqc+L4WKnObSWLxzXlgLUICSxglds/yALHZBPQlVqydxApi\r
+ iwhIS+y8OxvI5uBgFlCT+NOlAhIWFnCXuPTuIgtImEVAVeLvOiuQMK+Am8SDf0eZII6Qk9iw\r
+ +z/YdE6g8gt3n7JBbHKTWHWsm20CI/8CRoZVjLIpuVW6uYmZOcWpybrFyYl5ealFumZ6uZkl\r
+ eqkppZsYQSHD7qK8g/HPQaVDjAIcjEo8vB9u6IYIsSaWFVfmHmKU5GBSEuWtXKwXIsSXlJ9S\r
+ mZFYnBFfVJqTWnyIUYKDWUmEd8FKoBxvSmJlVWpRPkxKmoNFSZx30w++ECGB9MSS1OzU1ILU\r
+ IpisDAeHkgRv+3qgRsGi1PTUirTMnBKENBMHJ8hwHqDhYDW8xQWJucWZ6RD5U4yKUuK8D9cB\r
+ JQRAEhmleXC9sJh+xSgO9IowbxdIOw8wHcB1vwIazAQ0+J29LsjgkkSElFQD45XCh7JZ/34X\r
+ r9KWEC1/WbR0jkz8ybSzxW6fa9Oa1/Axf5x2NHSB97x3wgHbOhnuKZao7DnQfNXmlZhAp9+E\r
+ greFqYenu02L0cridH20u1bcXnljJp+fdZuuLB+rxES2Z1sCZ53SF114KzP2ikePjvjvKXGt\r
+ nkHhwdJPtqS7iCUVLE+NCedSYinOSDTUYi4qTgQAP4SZg8QCAAA=\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 03 Oct 2014 14:19:45 -0000\r
+\r
+From: Austin Clements <amdragon@mit.edu>\r
+\r
+Somehow this is the first upgrade pass that actually does *any* error\r
+checking, so this also adds the bit of necessary infrastructure to\r
+handle that.\r
+---\r
+ lib/database.cc | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--\r
+ 1 file changed, 62 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index fdcc526..ff6a7f6 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -1231,6 +1231,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ notmuch_bool_t timer_is_active = FALSE;\r
+ enum _notmuch_features target_features, new_features;\r
+ notmuch_status_t status;\r
++ notmuch_private_status_t private_status;\r
+ unsigned int count = 0, total = 0;\r
+ \r
+ status = _notmuch_database_ensure_writable (notmuch);\r
+@@ -1275,6 +1276,11 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ for (t = db->allterms_begin ("XTIMESTAMP"); t != t_end; t++)\r
+ ++total;\r
+ }\r
++ if (new_features & NOTMUCH_FEATURE_GHOSTS) {\r
++ t_end = db->metadata_keys_end ("thread_id_");\r
++ for (t = db->metadata_keys_begin ("thread_id_"); t != t_end; ++t)\r
++ ++total;\r
++ }\r
+ \r
+ /* Perform the upgrade in a transaction. */\r
+ db->begin_transaction (true);\r
+@@ -1378,10 +1384,64 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ }\r
+ }\r
+ \r
++ /* Perform metadata upgrades. */\r
++\r
++ /* Prior to NOTMUCH_FEATURE_GHOSTS, thread IDs for missing\r
++ * messages were stored as database metadata. Change these to\r
++ * ghost messages.\r
++ */\r
++ if (new_features & NOTMUCH_FEATURE_GHOSTS) {\r
++ notmuch_message_t *message;\r
++ std::string message_id, thread_id;\r
++\r
++ t_end = db->metadata_keys_end (NOTMUCH_METADATA_THREAD_ID_PREFIX);\r
++ for (t = db->metadata_keys_begin (NOTMUCH_METADATA_THREAD_ID_PREFIX);\r
++ t != t_end; ++t) {\r
++ if (do_progress_notify) {\r
++ progress_notify (closure, (double) count / total);\r
++ do_progress_notify = 0;\r
++ }\r
++\r
++ message_id = (*t).substr (\r
++ strlen (NOTMUCH_METADATA_THREAD_ID_PREFIX));\r
++ thread_id = db->get_metadata (*t);\r
++\r
++ /* Create ghost message */\r
++ message = _notmuch_message_create_for_message_id (\r
++ notmuch, message_id.c_str (), &private_status);\r
++ if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {\r
++ /* Document already exists; ignore the stored thread ID */\r
++ } else if (private_status ==\r
++ NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {\r
++ private_status = _notmuch_message_initialize_ghost (\r
++ message, thread_id.c_str ());\r
++ if (! private_status)\r
++ _notmuch_message_sync (message);\r
++ }\r
++\r
++ if (private_status) {\r
++ fprintf (stderr,\r
++ "Upgrade failed while creating ghost messages.\n");\r
++ status = COERCE_STATUS (private_status, "Unexpected status from _notmuch_message_initialize_ghost");\r
++ goto DONE;\r
++ }\r
++\r
++ /* Clear saved metadata thread ID */\r
++ db->set_metadata (*t, "");\r
++\r
++ ++count;\r
++ }\r
++ }\r
++\r
++ status = NOTMUCH_STATUS_SUCCESS;\r
+ db->set_metadata ("features", _print_features (local, notmuch->features));\r
+ db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));\r
+ \r
+- db->commit_transaction ();\r
++ DONE:\r
++ if (status == NOTMUCH_STATUS_SUCCESS)\r
++ db->commit_transaction ();\r
++ else\r
++ db->cancel_transaction ();\r
+ \r
+ if (timer_is_active) {\r
+ /* Now stop the timer. */\r
+@@ -1397,7 +1457,7 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,\r
+ }\r
+ \r
+ talloc_free (local);\r
+- return NOTMUCH_STATUS_SUCCESS;\r
++ return status;\r
+ }\r
+ \r
+ notmuch_status_t\r
+-- \r
+2.1.0\r
+\r