NeoMutt  2022-04-29-178-g3b62e6
Teaching an old dog new tricks
DOXYGEN
schema.c File Reference

Autocrypt database schema. More...

#include "config.h"
#include <stddef.h>
#include <sqlite3.h>
#include "private.h"
#include "mutt/lib.h"
+ Include dependency graph for schema.c:

Go to the source code of this file.

Functions

int mutt_autocrypt_schema_init (void)
 Set up an Autocrypt database. More...
 
int mutt_autocrypt_schema_update (void)
 Update the version number of the Autocrypt database schema. More...
 

Detailed Description

Autocrypt database schema.

Authors
  • Kevin J. McCarthy

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file schema.c.

Function Documentation

◆ mutt_autocrypt_schema_init()

int mutt_autocrypt_schema_init ( void  )

Set up an Autocrypt database.

Return values
0Success
-1Error

Definition at line 40 of file schema.c.

41{
42 char *errmsg = NULL;
43
44 const char *schema = "BEGIN TRANSACTION; "
45
46 "CREATE TABLE account ("
47 "email_addr text primary key not null, "
48 "keyid text, "
49 "keydata text, "
50 "prefer_encrypt int, "
51 "enabled int);"
52
53 "CREATE TABLE peer ("
54 "email_addr text primary key not null, "
55 "last_seen int, "
56 "autocrypt_timestamp int, "
57 "keyid text, "
58 "keydata text, "
59 "prefer_encrypt int, "
60 "gossip_timestamp int, "
61 "gossip_keyid text, "
62 "gossip_keydata text);"
63
64 "CREATE TABLE peer_history ("
65 "peer_email_addr text not null, "
66 "email_msgid text, "
67 "timestamp int, "
68 "keydata text);"
69
70 "CREATE INDEX peer_history_email "
71 "ON peer_history ("
72 "peer_email_addr);"
73
74 "CREATE TABLE gossip_history ("
75 "peer_email_addr text not null, "
76 "sender_email_addr text, "
77 "email_msgid text, "
78 "timestamp int, "
79 "gossip_keydata text);"
80
81 "CREATE INDEX gossip_history_email "
82 "ON gossip_history ("
83 "peer_email_addr);"
84
85 "CREATE TABLE schema ("
86 "version int);"
87
88 "INSERT into schema (version) values (1);"
89
90 "COMMIT TRANSACTION";
91
92 if (sqlite3_exec(AutocryptDB, schema, NULL, NULL, &errmsg) != SQLITE_OK)
93 {
94 mutt_debug(LL_DEBUG1, "mutt_autocrypt_schema_init() returned %s\n", errmsg);
95 sqlite3_free(errmsg);
96 return -1;
97 }
98 return 0;
99}
sqlite3 * AutocryptDB
Definition: db.c:52
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
+ Here is the caller graph for this function:

◆ mutt_autocrypt_schema_update()

int mutt_autocrypt_schema_update ( void  )

Update the version number of the Autocrypt database schema.

Return values
0Success
-1Error

Definition at line 106 of file schema.c.

107{
108 sqlite3_stmt *stmt = NULL;
109 int rc = -1;
110
111 if (sqlite3_prepare_v2(AutocryptDB, "SELECT version FROM schema;", -1, &stmt, NULL) != SQLITE_OK)
112 goto cleanup;
113
114 if (sqlite3_step(stmt) != SQLITE_ROW)
115 goto cleanup;
116
117 int version = sqlite3_column_int(stmt, 0);
118
119 if (version > 1)
120 {
121 /* L10N: The autocrypt database keeps track of schema version numbers.
122 This error occurs if the version number is too high.
123 Presumably because this is an old version of NeoMutt and the
124 database was upgraded by a future version. */
125 mutt_error(_("Autocrypt database version is too new"));
126 goto cleanup;
127 }
128
129 /* TODO: schema version upgrades go here.
130 * Bump one by one, each update inside a transaction. */
131
132 rc = 0;
133
134cleanup:
135 sqlite3_finalize(stmt);
136 return rc;
137}
#define mutt_error(...)
Definition: logging.h:87
#define _(a)
Definition: message.h:28
+ Here is the caller graph for this function: