NeoMutt  2021-02-05-666-ge300cd
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.

Definition at line 38 of file schema.c.

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

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