--- /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 E5D24431FDD\r
+ for <notmuch@notmuchmail.org>; Thu, 23 Oct 2014 05:31:08 -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 nU-Bdx7Rh0a9 for <notmuch@notmuchmail.org>;\r
+ Thu, 23 Oct 2014 05:31:01 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-3.mit.edu (dmz-mailsec-scanner-3.mit.edu\r
+ [18.9.25.14])\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 0088E431FDE\r
+ for <notmuch@notmuchmail.org>; Thu, 23 Oct 2014 05:30:52 -0700 (PDT)\r
+X-AuditID: 1209190e-f79d46d000003643-71-5448f4fc4b93\r
+Received: from mailhub-auth-2.mit.edu ( [18.7.62.36])\r
+ (using TLS with cipher AES256-SHA (256/256 bits))\r
+ (Client did not present a certificate)\r
+ by dmz-mailsec-scanner-3.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 7A.79.13891.CF4F8445; Thu, 23 Oct 2014 08:30:52 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-2.mit.edu (8.13.8/8.9.2) with ESMTP id s9NCUppt027184; \r
+ Thu, 23 Oct 2014 08:30:51 -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 s9NCUmDP008963\r
+ (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NOT);\r
+ Thu, 23 Oct 2014 08:30:51 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.84)\r
+ (envelope-from <aclements@csail.mit.edu>)\r
+ id 1XhHXP-0007c0-Co; Thu, 23 Oct 2014 08:30:43 -0400\r
+From: Austin Clements <aclements@csail.mit.edu>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 6/9] lib: Implement upgrade to ghost messages feature\r
+Date: Thu, 23 Oct 2014 08:30:38 -0400\r
+Message-Id: <1414067441-29054-7-git-send-email-aclements@csail.mit.edu>\r
+X-Mailer: git-send-email 2.1.0\r
+In-Reply-To: <1414067441-29054-1-git-send-email-aclements@csail.mit.edu>\r
+References: <1414067441-29054-1-git-send-email-aclements@csail.mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFtrLIsWRmVeSWpSXmKPExsUixG6novvni0eIwc5HPBar5/JYXL85k9mB\r
+ yWPnrLvsHs9W3WIOYIrisklJzcksSy3St0vgyjj44ARzwQbZikV75jE3ME4V72Lk5JAQMJFo\r
+ Wb+XBcIWk7hwbz1bFyMXh5DAbCaJrhd3WSCcjYwSe/b3QmXuMEmsa3nFDuEsYZQ4/HwzE0g/\r
+ m4C+xIq1k1hBbBEBaYmdd2eD2cwCjhKf9y9iA7GFBTwktn7bzQ5iswioSkx/+BrM5hVwl5g9\r
+ 8zATxB1yEht2/2cEsTmB6vfsuAPWKwRUs/35QdYJjPwLGBlWMcqm5Fbp5iZm5hSnJusWJyfm\r
+ 5aUW6Rrr5WaW6KWmlG5iBIUSpyTfDsavB5UOMQpwMCrx8FYke4QIsSaWFVfmHmKU5GBSEuWN\r
+ eQAU4kvKT6nMSCzOiC8qzUktPsQowcGsJMIbfwEox5uSWFmVWpQPk5LmYFES5930gy9ESCA9\r
+ sSQ1OzW1ILUIJivDwaEkwcsIjBkhwaLU9NSKtMycEoQ0EwcnyHAeoOEiIDW8xQWJucWZ6RD5\r
+ U4yKUuK8yZ+BEgIgiYzSPLheWKy/YhQHekWYVw2knQeYJuC6XwENZgK5egPY4JJEhJRUA6OQ\r
+ yr2Yihu6K30F3i+zVvjTcdDCKXH5ippb/TrdYrP+ce62+24vcG/RwjUBy/7F5aTrn+oTqXt8\r
+ 58rOqTknLj31mum5bE5IEVvGzR93F35qOL3ioZ+Dq8C2JTe/xi1zdZmarLTrZUHIxlWcO76I\r
+ MKw5Hyi3zPNI65Q3KSePa/0q3Hi1fFto3sJyJZbijERDLeai4kQAHmqfVtACAAA=\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: Thu, 23 Oct 2014 12:31:09 -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 | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--\r
+ 1 file changed, 64 insertions(+), 2 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 92a92d9..b673718 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,13 @@ 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
++ /* The ghost message upgrade converts all thread_id_*\r
++ * metadata values into ghost message documents. */\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 +1386,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 +1459,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