From 01b8d1667a22e00dad022dbaed75d9e2eacd5238 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 21 Feb 2016 18:05:54 +0100 Subject: tests: Add minimal test for util.random that checks that it returns the number of bytes requested --- tests/test.lua | 1 + tests/test_util_random.lua | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 tests/test_util_random.lua diff --git a/tests/test.lua b/tests/test.lua index 4863c4e7..0fcc4907 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -23,6 +23,7 @@ function run_all_tests() dotest "util.cache" dotest "util.throttle" dotest "util.uuid" + dotest "util.random" dosingletest("test_sasl.lua", "latin1toutf8"); dosingletest("test_utf8.lua", "valid"); diff --git a/tests/test_util_random.lua b/tests/test_util_random.lua new file mode 100644 index 00000000..79572ef8 --- /dev/null +++ b/tests/test_util_random.lua @@ -0,0 +1,10 @@ +-- Makes no attempt at testing how random the bytes are, +-- just that it returns the number of bytes requested + +function bytes(bytes) + assert_is(bytes(16)); + + for i = 1, 255 do + assert_equal(i, #bytes(i)); + end +end -- cgit v1.2.3 From 91b41656897ad4daa8627e6cb4eda68a8c44733a Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 21 Feb 2016 19:01:26 +0100 Subject: mod_storage_sql: Lower message about new engine creation to debug level --- plugins/mod_storage_sql.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index 50d34a23..a11523d0 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -453,7 +453,7 @@ function module.load() local params = normalize_params(module:get_option("sql", default_params)); engine = engines[sql.db2uri(params)]; if not engine then - module:log("info", "Creating new engine"); + module:log("debug", "Creating new engine"); engine = sql:create_engine(params, function (engine) if module:get_option("sql_manage_tables", true) then -- Automatically create table, ignore failure (table probably already exists) -- cgit v1.2.3 From 2af32832aeb6ad8b1786f5a81f8bdd2f735e0969 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 21 Feb 2016 19:25:01 +0100 Subject: util.sql: Raw query debug logging (needs to be explicitly enabled by a plugin) --- util/sql.lua | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/util/sql.lua b/util/sql.lua index 2d5e1774..b4d14537 100644 --- a/util/sql.lua +++ b/util/sql.lua @@ -147,6 +147,11 @@ local result_mt = { __index = { rowcount = function(self) return self.__stmt:rowcount(); end; } }; +local function debugquery(where, sql, ...) + local i = 0; local a = {...} + log("debug", "[%s] %s", where, sql:gsub("%?", function () i = i + 1; local v = a[i]; if type(v) == "string" then v = ("%q"):format(v); end return tostring(v); end)); +end + function engine:execute_query(sql, ...) if self.params.driver == "PostgreSQL" then sql = sql:gsub("`", "\""); @@ -172,6 +177,26 @@ engine.insert = engine.execute_update; engine.select = engine.execute_query; engine.delete = engine.execute_update; engine.update = engine.execute_update; +local function debugwrap(name, f) + return function (self, sql, ...) + debugquery(name, sql, ...) + return f(self, sql, ...) + end +end +function engine:debug(enable) + self._debug = enable; + if enable then + engine.insert = debugwrap("insert", engine.execute_update); + engine.select = debugwrap("select", engine.execute_query); + engine.delete = debugwrap("delete", engine.execute_update); + engine.update = debugwrap("update", engine.execute_update); + else + engine.insert = engine.execute_update; + engine.select = engine.execute_query; + engine.delete = engine.execute_update; + engine.update = engine.execute_update; + end +end function engine:_transaction(func, ...) if not self.conn then local ok, err = self:connect(); @@ -221,7 +246,9 @@ function engine:_create_index(index) if index.unique then sql = sql:gsub("^CREATE", "CREATE UNIQUE"); end - --print(sql); + if self._debug then + debugquery("create", sql); + end return self:execute(sql); end function engine:_create_table(table) @@ -252,6 +279,9 @@ function engine:_create_table(table) elseif self.params.driver == "MySQL" then sql = sql:gsub(";$", (" CHARACTER SET '%s' COLLATE '%s_bin';"):format(self.charset, self.charset)); end + if self._debug then + debugquery("create", sql); + end local success,err = self:execute(sql); if not success then return success,err; end for i,v in ipairs(table.__table__) do -- cgit v1.2.3 From 70c1c2a643db61dbc65b4bf77996e8337d8612ed Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 21 Feb 2016 19:29:00 +0100 Subject: mod_storage_sql: Remove debug logging of archive queries --- plugins/mod_storage_sql.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index a11523d0..af2bcef4 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -275,7 +275,6 @@ function archive_store:find(username, query) end sql_query = sql_query:format(t_concat(where, " AND "), query.reverse and "DESC" or "ASC", query.limit and " LIMIT ?" or ""); - module:log("debug", sql_query); return engine:select(sql_query, unpack(args)); end); if not ok then return ok, result end @@ -301,7 +300,6 @@ function archive_store:delete(username, query) archive_where(query, args, where); archive_where_id_range(query, args, where); sql_query = sql_query:format(t_concat(where, " AND ")); - module:log("debug", sql_query); return engine:delete(sql_query, unpack(args)); end); end -- cgit v1.2.3 From 71d2dced8dd98ce1b1bf67e2ebbae981cf85287c Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 21 Feb 2016 19:29:31 +0100 Subject: mod_debug_sql: Small plugin that enables raw SQL query logging (for debugging) --- plugins/mod_debug_sql.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 plugins/mod_debug_sql.lua diff --git a/plugins/mod_debug_sql.lua b/plugins/mod_debug_sql.lua new file mode 100644 index 00000000..7bbbbd88 --- /dev/null +++ b/plugins/mod_debug_sql.lua @@ -0,0 +1,25 @@ +-- Enables SQL query logging +-- +-- luacheck: ignore 213/uri + +local engines = module:shared("/*/sql/connections"); + +for uri, engine in pairs(engines) do + engine:debug(true); +end + +setmetatable(engines, { + __newindex = function (t, uri, engine) + engine:debug(true); + rawset(t, uri, engine); + end +}); + +function module.unload() + setmetatable(engines, nil); + for uri, engine in pairs(engines) do + engine:debug(false); + end +end + + -- cgit v1.2.3 From 28585f40e30b5aabe1fe4f1814dfe49115352035 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sun, 21 Feb 2016 19:30:45 +0100 Subject: usermanager: Shortcircuit user existence check if they have existing sessions --- core/usermanager.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/core/usermanager.lua b/core/usermanager.lua index 0d8d7f91..d5132662 100644 --- a/core/usermanager.lua +++ b/core/usermanager.lua @@ -81,6 +81,7 @@ local function set_password(username, password, host) end local function user_exists(username, host) + if hosts[host].sessions[username] then return true; end return hosts[host].users.user_exists(username); end -- cgit v1.2.3 From a1bba9eeab3263061a2b1e1634847b3ed4002e20 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 22 Feb 2016 15:23:27 +0100 Subject: mod_storage_sql: Treat non-existent archive IDs as beyound the end of the archive (fixes #624) (tested on sqlite3 only) --- plugins/mod_storage_sql.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index af2bcef4..bf3c1c7b 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -235,12 +235,12 @@ local function archive_where_id_range(query, args, where) local args_len = #args -- Before or after specific item, exclusive if query.after then -- keys better be unique! - where[#where+1] = "`sort_id` > (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1)" + where[#where+1] = "`sort_id` > COALESCE((SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1), 0)" args[args_len+1], args[args_len+2], args[args_len+3], args[args_len+4] = query.after, args[1], args[2], args[3]; args_len = args_len + 4 end if query.before then - where[#where+1] = "`sort_id` < (SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1)" + where[#where+1] = "`sort_id` < COALESCE((SELECT `sort_id` FROM `prosodyarchive` WHERE `key` = ? AND `host` = ? AND `user` = ? AND `store` = ? LIMIT 1), (SELECT MAX(`sort_id`)+1 FROM `prosodyarchive`))" args[args_len+1], args[args_len+2], args[args_len+3], args[args_len+4] = query.before, args[1], args[2], args[3]; end end -- cgit v1.2.3