diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-06-25 17:32:56 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-06-25 17:33:19 +0100 |
commit | 92d7bc00c8830deefd4e8946dc59b8f3f6cbae22 (patch) | |
tree | 11aff43c017cf118182cbf448fc1965edc781097 | |
parent | c2020b7117b7998627468d0167c6c4b27f613954 (diff) | |
download | rspamd-92d7bc00c8830deefd4e8946dc59b8f3f6cbae22.tar.gz rspamd-92d7bc00c8830deefd4e8946dc59b8f3f6cbae22.zip |
Fix db creating.
-rw-r--r-- | src/libstat/backends/sqlite3_backend.c | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/src/libstat/backends/sqlite3_backend.c b/src/libstat/backends/sqlite3_backend.c index f443d1aa7..f2b5a226c 100644 --- a/src/libstat/backends/sqlite3_backend.c +++ b/src/libstat/backends/sqlite3_backend.c @@ -73,6 +73,7 @@ static const char *create_tables_sql = "CONSTRAINT tid UNIQUE (token, user, language) ON CONFLICT REPLACE" ");" "CREATE UNIQUE INDEX IF NOT EXISTS un ON users(name);" + "CREATE INDEX IF NOT EXISTS tok ON tokens(token);" "CREATE UNIQUE INDEX IF NOT EXISTS ln ON languages(name);" "PRAGMA user_version=" SQLITE3_SCHEMA_VERSION ";" "INSERT INTO users(id, name, learns) VALUES(0, '" SQLITE3_DEFAULT "',0);" @@ -332,8 +333,8 @@ rspamd_sqlite3_opendb (const gchar *path, const ucl_object_t *opts, if ((rc = sqlite3_open_v2 (path, &sqlite, flags, NULL)) != SQLITE_OK) { g_set_error (err, rspamd_sqlite3_quark (), - rc, "cannot open sqlite db %s: %d", - path, rc); + rc, "cannot open sqlite db %s: %s", + path, sqlite3_errstr (rc)); return NULL; } @@ -344,17 +345,19 @@ rspamd_sqlite3_opendb (const gchar *path, const ucl_object_t *opts, } /* Check user_version */ - g_assert (sqlite3_prepare_v2 (sqlite, user_version, -1, &stmt, NULL) - == SQLITE_OK); - g_assert (sqlite3_step (stmt) == SQLITE_ROW); + if (!create) { + 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; + } - 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); } - sqlite3_finalize (stmt); - if (create) { if (sqlite3_exec (sqlite, create_tables_sql, NULL, NULL, NULL) != SQLITE_OK) { g_set_error (err, rspamd_sqlite3_quark (), @@ -579,9 +582,11 @@ void rspamd_sqlite3_finalize_learn (struct rspamd_task *task, gpointer runtime, gpointer ctx) { - struct rspamd_stat_sqlite3_db *bk = runtime; + struct rspamd_stat_sqlite3_rt *rt = runtime; + struct rspamd_stat_sqlite3_db *bk; - g_assert (bk != NULL); + g_assert (rt != NULL); + bk = rt->db; if (bk->in_transaction) { rspamd_sqlite3_run_prstmt (bk, RSPAMD_STAT_BACKEND_TRANSACTION_COMMIT); @@ -595,11 +600,12 @@ gulong rspamd_sqlite3_total_learns (struct rspamd_task *task, gpointer runtime, gpointer ctx) { - struct rspamd_stat_sqlite3_db *bk = runtime; + struct rspamd_stat_sqlite3_rt *rt = runtime; + struct rspamd_stat_sqlite3_db *bk; guint64 res; - g_assert (bk != NULL); - + g_assert (rt != NULL); + bk = rt->db; rspamd_sqlite3_run_prstmt (bk, RSPAMD_STAT_BACKEND_GET_LEARNS, &res); return res; |