[Patch v5 5/6] notmuch-new: backup tags before database upgrade
[notmuch-archives.git] / ec / d3c2fa4c469aa24ca1686a16c7e2c6deb8c444
1 Return-Path: <bremner@tethera.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 47756431FD6\r
6         for <notmuch@notmuchmail.org>; Tue,  1 Apr 2014 18:17:07 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id L04FC8ZF3xH4 for <notmuch@notmuchmail.org>;\r
16         Tue,  1 Apr 2014 18:17:03 -0700 (PDT)\r
17 Received: from yantan.tethera.net (yantan.tethera.net [199.188.72.155])\r
18         (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id D61E1431FDA\r
21         for <notmuch@notmuchmail.org>; Tue,  1 Apr 2014 18:16:47 -0700 (PDT)\r
22 Received: from remotemail by yantan.tethera.net with local (Exim 4.80)\r
23         (envelope-from <bremner@tethera.net>)\r
24         id 1WV9nL-00073I-Bz; Tue, 01 Apr 2014 22:16:47 -0300\r
25 Received: (nullmailer pid 18507 invoked by uid 1000); Wed, 02 Apr 2014\r
26         01:16:27 -0000\r
27 From: David Bremner <david@tethera.net>\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [Patch v5 5/6] notmuch-new: backup tags before database upgrade\r
30 Date: Tue,  1 Apr 2014 22:16:20 -0300\r
31 Message-Id: <1396401381-18128-6-git-send-email-david@tethera.net>\r
32 X-Mailer: git-send-email 1.9.0\r
33 In-Reply-To: <1396401381-18128-1-git-send-email-david@tethera.net>\r
34 References: <1396401381-18128-1-git-send-email-david@tethera.net>\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.13\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Wed, 02 Apr 2014 01:17:08 -0000\r
48 \r
49 All we do here is calculate the backup filename, and call the existing\r
50 dump routine.\r
51 \r
52 Also take the opportity to add a message about being safe to\r
53 interrupt.\r
54 ---\r
55  notmuch-new.c        | 29 ++++++++++++++++++++++++++++-\r
56  test/T530-upgrade.sh |  4 +++-\r
57  2 files changed, 31 insertions(+), 2 deletions(-)\r
58 \r
59 diff --git a/notmuch-new.c b/notmuch-new.c\r
60 index 82acf69..e0431c6 100644\r
61 --- a/notmuch-new.c\r
62 +++ b/notmuch-new.c\r
63 @@ -989,8 +989,35 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
64             return EXIT_FAILURE;\r
65  \r
66         if (notmuch_database_needs_upgrade (notmuch)) {\r
67 -           if (add_files_state.verbosity >= VERBOSITY_NORMAL)\r
68 +           time_t now = time (NULL);\r
69 +           struct tm *gm_time = gmtime (&now);\r
70 +\r
71 +           /* since dump files are written atomically, the amount of\r
72 +            * harm from overwriting one within a second seems\r
73 +            * relatively small. */\r
74 +\r
75 +           const char *backup_name =\r
76 +               talloc_asprintf (notmuch, "%s/backup-%04d-%02d-%02d-%02d:%02d:%02d.gz",\r
77 +                                dot_notmuch_path,\r
78 +                                gm_time->tm_year + 1900,\r
79 +                                gm_time->tm_mon + 1,\r
80 +                                gm_time->tm_mday,\r
81 +                                gm_time->tm_hour,\r
82 +                                gm_time->tm_min,\r
83 +                                gm_time->tm_sec);\r
84 +\r
85 +           if (add_files_state.verbosity >= VERBOSITY_NORMAL) {\r
86                 printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n");\r
87 +               printf ("This process is safe to interrupt.\n");\r
88 +               printf ("Backing up tags to %s\n", backup_name);\r
89 +           }\r
90 +\r
91 +           if (notmuch_database_dump (notmuch, backup_name, "",\r
92 +                                      DUMP_FORMAT_BATCH_TAG, TRUE)) {\r
93 +               fprintf (stderr, "Backup failed. Aborting upgrade");\r
94 +               return EXIT_FAILURE;\r
95 +           }\r
96 +\r
97             gettimeofday (&add_files_state.tv_start, NULL);\r
98             notmuch_database_upgrade (notmuch,\r
99                                       add_files_state.verbosity >= VERBOSITY_NORMAL ? upgrade_print_progress : NULL,\r
100 diff --git a/test/T530-upgrade.sh b/test/T530-upgrade.sh\r
101 index 67bbf31..d46e3d1 100755\r
102 --- a/test/T530-upgrade.sh\r
103 +++ b/test/T530-upgrade.sh\r
104 @@ -26,9 +26,11 @@ output=$(notmuch search path:foo)\r
105  test_expect_equal "$output" ""\r
106  \r
107  test_begin_subtest "database upgrade from format version 1"\r
108 -output=$(notmuch new)\r
109 +output=$(notmuch new | sed -e 's/^Backing up tags to .*$/Backing up tags to FILENAME/')\r
110  test_expect_equal "$output" "\\r
111  Welcome to a new version of notmuch! Your database will now be upgraded.\r
112 +This process is safe to interrupt.\r
113 +Backing up tags to FILENAME\r
114  Your notmuch database has now been upgraded to database format version 2.\r
115  No new mail."\r
116  \r
117 -- \r
118 1.9.0\r
119 \r