]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Check user_version upon start.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 20 Jun 2015 22:11:24 +0000 (23:11 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 20 Jun 2015 22:11:24 +0000 (23:11 +0100)
src/libstat/backends/sqlite3_backend.c

index c85d774d8d8c13b94e4112ae2b42fc5363f4692b..5a791b6aaccb61d5c250a68313e735a609a9ff21 100644 (file)
@@ -287,9 +287,11 @@ rspamd_sqlite3_opendb (const gchar *path, const ucl_object_t *opts,
 {
        struct rspamd_stat_sqlite3_db *bk;
        sqlite3 *sqlite;
+       sqlite3_stmt *stmt;
        gint rc, flags;
        static const char sqlite_wal[] = "PRAGMA journal_mode=WAL;",
-                       fallback_journal[] = "PRAGMA journal_mode=OFF;";
+                       fallback_journal[] = "PRAGMA journal_mode=OFF;",
+                       user_version[] = "PRAGMA user_version;";
 
        flags = SQLITE_OPEN_READWRITE;
 
@@ -306,6 +308,23 @@ rspamd_sqlite3_opendb (const gchar *path, const ucl_object_t *opts,
                return NULL;
        }
 
+       if (sqlite3_exec (sqlite, sqlite_wal, NULL, NULL, NULL) != SQLITE_OK) {
+               msg_warn ("WAL mode is not supported, locking issues might occur");
+               sqlite3_exec (sqlite, fallback_journal, NULL, NULL, NULL);
+       }
+
+       /* Check user_version */
+       g_assert (sqlite3_prepare_v2 (sqlite, user_version, -1, &stmt, NULL)
+                       == SQLITE_OK);
+       g_assert (sqlite3_step (stmt) == SQLITE_ROW);
+
+       if (sqlite3_column_int (stmt, 0) != atoi (SQLITE3_SCHEMA_VERSION)) {
+               msg_warn ("bad sqlite database: %s, try to recreate it", path);
+               create = TRUE;
+       }
+
+       sqlite3_finalize (stmt);
+
        if (create) {
                if (sqlite3_exec (sqlite, create_tables_sql, NULL, NULL, NULL) != SQLITE_OK) {
                        g_set_error (err, rspamd_sqlite3_quark (),
@@ -317,11 +336,6 @@ rspamd_sqlite3_opendb (const gchar *path, const ucl_object_t *opts,
                }
        }
 
-       if (sqlite3_exec (sqlite, sqlite_wal, NULL, NULL, NULL) != SQLITE_OK) {
-               msg_warn ("WAL mode is not supported, locking issues might occur");
-               sqlite3_exec (sqlite, fallback_journal, NULL, NULL, NULL);
-       }
-
        bk = g_slice_alloc0 (sizeof (*bk));
        bk->sqlite = sqlite;
        bk->prstmt = g_slice_alloc0 (sizeof (prepared_stmts));