mod_storage_sql: Log error when failing to update MySQL schema
[prosody.git] / plugins / mod_storage_sql.lua
index 1e9284714cde1c9848304b71d4fac3d326368038..57331ac0a7c40895bd63990c5b07f1754b963bf7 100644 (file)
@@ -3,16 +3,16 @@
 
 DB Tables:
        Prosody - key-value, map
-               | host | user | store | key | subkey | type | value |
+               | host | user | store | key | type | value |
        ProsodyArchive - list
                | host | user | store | key | time | stanzatype | jsonvalue |
 
 Mapping:
        Roster - Prosody
-               | host | user | "roster" | "contactjid" | item-subkey | type | value |
-               | host | user | "roster" | NULL | NULL | "json" | roster[false] data |
+               | host | user | "roster" | "contactjid" | type | value |
+               | host | user | "roster" | NULL | "json" | roster[false] data |
        Account - Prosody
-               | host | user | "accounts" | "username" | NULL | type | value |
+               | host | user | "accounts" | "username" | type | value |
 
        Offline - ProsodyArchive
                | host | user | "offline" | "contactjid" | time | "message" | json|XML |
@@ -25,41 +25,122 @@ local tonumber = tonumber;
 local pairs = pairs;
 local next = next;
 local setmetatable = setmetatable;
-local json = { stringify = function(s) return require"util.serialization".serialize(s) end, parse = require"util.serialization".deserialze };
+local xpcall = xpcall;
+local json = require "util.json";
 
-local connection = ...;
+local DBI;
+local connection;
 local host,user,store = module.host;
+local params = module:get_option("sql");
 
-do -- process options to get a db connection
-       local DBI = require "DBI";
+local resolve_relative_path = require "core.configmanager".resolve_relative_path;
 
-       local params = module:get_option("sql") or { driver = "SQLite3", database = "prosody.sqlite" };
-       assert(params and params.driver and params.database, "invalid params");
+local function test_connection()
+       if not connection then return nil; end
+       if connection:ping() then
+               return true;
+       else
+               module:log("debug", "Database connection closed");
+               connection = nil;
+       end
+end
+local function connect()
+       if not test_connection() then
+               prosody.unlock_globals();
+               local dbh, err = DBI.Connect(
+                       params.driver, params.database,
+                       params.username, params.password,
+                       params.host, params.port
+               );
+               prosody.lock_globals();
+               if not dbh then
+                       module:log("debug", "Database connection failed: %s", tostring(err));
+                       return nil, err;
+               end
+               module:log("debug", "Successfully connected to database");
+               dbh:autocommit(false); -- don't commit automatically
+               connection = dbh;
+               return connection;
+       end
+end
+
+local function create_table()
+       local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);";
+       if params.driver == "PostgreSQL" then
+               create_sql = create_sql:gsub("`", "\"");
+       elseif params.driver == "MySQL" then
+               create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT");
+       end
        
+       local stmt = connection:prepare(create_sql);
+       if stmt then
+               local ok = stmt:execute();
+               local commit_ok = connection:commit();
+               if ok and commit_ok then
+                       module:log("info", "Initialized new %s database with prosody table", params.driver);
+                       local index_sql = "CREATE INDEX `prosody_index` ON `prosody` (`host`, `user`, `store`, `key`)";
+                       if params.driver == "PostgreSQL" then
+                               index_sql = index_sql:gsub("`", "\"");
+                       elseif params.driver == "MySQL" then
+                               index_sql = index_sql:gsub("`([,)])", "`(20)%1");
+                       end
+                       local stmt, err = connection:prepare(index_sql);
+                       local ok, commit_ok, commit_err;
+                       if stmt then
+                               ok, err = stmt:execute();
+                               commit_ok, commit_err = connection:commit();
+                       end
+                       if not(ok and commit_ok) then
+                               module:log("warn", "Failed to create index (%s), lookups may not be optimised", err or commit_err);
+                       end
+               else -- COMPAT: Upgrade tables from 0.8.0
+                       -- Failed to create, but check existing MySQL table here
+                       local stmt = connection:prepare("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'");
+                       local ok = stmt:execute();
+                       local commit_ok = connection:commit();
+                       if ok and commit_ok then
+                               if stmt:rowcount() > 0 then
+                                       local stmt = connection:prepare("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT");
+                                       local ok = stmt:execute();
+                                       local commit_ok = connection:commit();
+                                       if ok and commit_ok then
+                                               module:log("info", "Database table automatically upgraded");
+                                       end
+                               end
+                               repeat until not stmt:fetch();
+                       else
+                               module:log("error", "Failed to upgrade database schema, please see http://prosody.im/doc/mysql for help");
+                       end
+               end
+       end
+end
+
+do -- process options to get a db connection
+       local ok;
        prosody.unlock_globals();
-       local dbh, err = DBI.Connect(
-               params.driver, params.database,
-               params.username, params.password,
-               params.host, params.port
-       );
+       ok, DBI = pcall(require, "DBI");
+       if not ok then
+               package.loaded["DBI"] = {};
+               module:log("error", "Failed to load the LuaDBI library for accessing SQL databases: %s", DBI);
+               module:log("error", "More information on installing LuaDBI can be found at http://prosody.im/doc/depends#luadbi");
+       end
        prosody.lock_globals();
-       assert(dbh, err);
+       if not ok or not DBI.Connect then
+               return; -- Halt loading of this module
+       end
 
-       dbh:autocommit(false); -- don't commit automatically
-       connection = dbh;
+       params = params or { driver = "SQLite3" };
        
-       if params.driver == "SQLite3" then -- auto initialize
-               local stmt = assert(connection:prepare("SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='Prosody';"));
-               local ok = assert(stmt:execute());
-               local count = stmt:fetch()[1];
-               if count == 0 then
-                       local stmt = assert(connection:prepare("CREATE TABLE Prosody (host TEXT, user TEXT, store TEXT, key TEXT, subkey TEXT, type TEXT, value TEXT);"));
-                       assert(stmt:execute());
-                       assert(connection:commit());
-                       module:log("debug", "Initialized new SQLite3 database");
-               end
-               --print("===", json.stringify())
+       if params.driver == "SQLite3" then
+               params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite");
        end
+       
+       assert(params.driver and params.database, "Both the SQL driver and the database need to be specified");
+       
+       assert(connect());
+       
+       -- Automatically create table, ignore failure (table probably already exists)
+       create_table();
 end
 
 local function serialize(value)
@@ -67,7 +148,7 @@ local function serialize(value)
        if t == "string" or t == "boolean" or t == "number" then
                return t, tostring(value);
        elseif t == "table" then
-               local value,err = json.stringify(value);
+               local value,err = json.encode(value);
                if value then return "json", value; end
                return nil, err;
        end
@@ -79,17 +160,22 @@ local function deserialize(t, value)
                if value == "true" then return true;
                elseif value == "false" then return false; end
        elseif t == "number" then return tonumber(value);
-       elseif value == "json" then
-               return json.parse(value);
+       elseif t == "json" then
+               return json.decode(value);
        end
 end
 
 local function getsql(sql, ...)
+       if params.driver == "PostgreSQL" then
+               sql = sql:gsub("`", "\"");
+       end
        -- do prepared statement stuff
        local stmt, err = connection:prepare(sql);
-       if not stmt then return nil, err; end
+       if not stmt and not test_connection() then error("connection failed"); end
+       if not stmt then module:log("error", "QUERY FAILED: %s %s", err, debug.traceback()); return nil, err; end
        -- run query
-       local ok, err = stmt:execute(host, user, store, ...);
+       local ok, err = stmt:execute(host or "", user or "", store or "", ...);
+       if not ok and not test_connection() then error("connection failed"); end
        if not ok then return nil, err; end
        
        return stmt;
@@ -103,7 +189,7 @@ local function transact(...)
        -- ...
 end
 local function rollback(...)
-       connection:rollback(); -- FIXME check for rollback error?
+       if connection then connection:rollback(); end -- FIXME check for rollback error?
        return ...;
 end
 local function commit(...)
@@ -111,12 +197,9 @@ local function commit(...)
        return ...;
 end
 
-local keyval_store = {};
-keyval_store.__index = keyval_store;
-function keyval_store:get(username)
-       user,store = username,self.store;
-       local stmt, err = getsql("SELECT * FROM Prosody WHERE host=? AND user=? AND store=? AND subkey=NULL");
-       if not stmt then return nil, err; end
+local function keyval_store_get()
+       local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?");
+       if not stmt then return rollback(nil, err); end
        
        local haveany;
        local result = {};
@@ -124,28 +207,27 @@ function keyval_store:get(username)
                haveany = true;
                local k = row.key;
                local v = deserialize(row.type, row.value);
-               if v then
-                       if k then result[k] = v; elseif type(v) == "table" then
+               if k and v then
+                       if k ~= "" then result[k] = v; elseif type(v) == "table" then
                                for a,b in pairs(v) do
                                        result[a] = b;
                                end
                        end
                end
        end
-       return haveany and result or nil;
+       return commit(haveany and result or nil);
 end
-function keyval_store:set(username, data)
-       user,store = username,self.store;
-       -- start transaction
-       local affected, err = setsql("DELETE FROM Prosody WHERE host=? AND user=? AND store=? AND subkey=NULL");
+local function keyval_store_set(data)
+       local affected, err = setsql("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=?");
+       if not affected then return rollback(affected, err); end
        
        if data and next(data) ~= nil then
                local extradata = {};
                for key, value in pairs(data) do
-                       if type(key) == "string" then
+                       if type(key) == "string" and key ~= "" then
                                local t, value = serialize(value);
                                if not t then return rollback(t, value); end
-                               local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,type,value) VALUES (?,?,?,?,?,?)", key, t, value);
+                               local ok, err = setsql("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value);
                                if not ok then return rollback(ok, err); end
                        else
                                extradata[key] = value;
@@ -154,63 +236,84 @@ function keyval_store:set(username, data)
                if next(extradata) ~= nil then
                        local t, extradata = serialize(extradata);
                        if not t then return rollback(t, extradata); end
-                       local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,type,value) VALUES (?,?,?,?,?,?)", nil, t, extradata);
+                       local ok, err = setsql("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", "", t, extradata);
                        if not ok then return rollback(ok, err); end
                end
        end
        return commit(true);
 end
 
-local map_store = {};
-map_store.__index = map_store;
-function map_store:get(username, key)
+local keyval_store = {};
+keyval_store.__index = keyval_store;
+function keyval_store:get(username)
        user,store = username,self.store;
-       local stmt, err = getsql("SELECT * FROM Prosody WHERE host=? AND user=? AND store=? AND key=?", key);
-       if not stmt then return nil, err; end
+       if not connection and not connect() then return nil, "Unable to connect to database"; end
+       local success, ret, err = xpcall(keyval_store_get, debug.traceback);
+       if not connection and connect() then
+               success, ret, err = xpcall(keyval_store_get, debug.traceback);
+       end
+       if success then return ret, err; else return rollback(nil, ret); end
+end
+function keyval_store:set(username, data)
+       user,store = username,self.store;
+       if not connection and not connect() then return nil, "Unable to connect to database"; end
+       local success, ret, err = xpcall(function() return keyval_store_set(data); end, debug.traceback);
+       if not connection and connect() then
+               success, ret, err = xpcall(function() return keyval_store_set(data); end, debug.traceback);
+       end
+       if success then return ret, err; else return rollback(nil, ret); end
+end
+
+local function map_store_get(key)
+       local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
+       if not stmt then return rollback(nil, err); end
        
        local haveany;
        local result = {};
        for row in stmt:rows(true) do
                haveany = true;
-               local k = row.subkey;
+               local k = row.key;
                local v = deserialize(row.type, row.value);
-               if v then
-                       if k then result[k] = v; elseif type(v) == "table" then
+               if k and v then
+                       if k ~= "" then result[k] = v; elseif type(v) == "table" then
                                for a,b in pairs(v) do
                                        result[a] = b;
                                end
                        end
                end
        end
-       return haveany and result or nil;
+       return commit(haveany and result[key] or nil);
 end
-function map_store:set(username, key, data)
-       user,store = username,self.store;
-       -- start transaction
-       local affected, err = setsql("DELETE FROM Prosody WHERE host=? AND user=? AND store=? AND key=?", key);
+local function map_store_set(key, data)
+       local affected, err = setsql("DELETE FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
+       if not affected then return rollback(affected, err); end
        
        if data and next(data) ~= nil then
-               local extradata = {};
-               for subkey, value in pairs(data) do
-                       if type(subkey) == "string" then
-                               local t, value = serialize(value);
-                               if not t then return rollback(t, value); end
-                               local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,subkey,type,value) VALUES (?,?,?,?,?,?)", key, subkey, t, value);
-                               if not ok then return rollback(ok, err); end
-                       else
-                               extradata[subkey] = value;
-                       end
-               end
-               if next(extradata) ~= nil then
-                       local t, extradata = serialize(extradata);
-                       if not t then return rollback(t, extradata); end
-                       local ok, err = setsql("INSERT INTO Prosody (host,user,store,key,subkey,type,value) VALUES (?,?,?,?,?,?)", key, nil, t, extradata);
+               if type(key) == "string" and key ~= "" then
+                       local t, value = serialize(data);
+                       if not t then return rollback(t, value); end
+                       local ok, err = setsql("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)", key, t, value);
                        if not ok then return rollback(ok, err); end
+               else
+                       -- TODO non-string keys
                end
        end
        return commit(true);
 end
 
+local map_store = {};
+map_store.__index = map_store;
+function map_store:get(username, key)
+       user,store = username,self.store;
+       local success, ret, err = xpcall(function() return map_store_get(key); end, debug.traceback);
+       if success then return ret, err; else return rollback(nil, ret); end
+end
+function map_store:set(username, key, data)
+       user,store = username,self.store;
+       local success, ret, err = xpcall(function() return map_store_set(key, data); end, debug.traceback);
+       if success then return ret, err; else return rollback(nil, ret); end
+end
+
 local list_store = {};
 list_store.__index = list_store;
 function list_store:scan(username, from, to, jid, typ)
@@ -219,10 +322,10 @@ function list_store:scan(username, from, to, jid, typ)
        local cols = {"from", "to", "jid", "typ"};
        local vals = { from ,  to ,  jid ,  typ };
        local stmt, err;
-       local query = "SELECT * FROM ProsodyArchive WHERE host=? AND user=? AND store=?";
+       local query = "SELECT * FROM `prosodyarchive` WHERE `host`=? AND `user`=? AND `store`=?";
        
        query = query.." ORDER BY time";
-       --local stmt, err = getsql("SELECT * FROM Prosody WHERE host=? AND user=? AND store=? AND key=?", key);
+       --local stmt, err = getsql("SELECT * FROM `prosody` WHERE `host`=? AND `user`=? AND `store`=? AND `key`=?", key or "");
        
        return nil, "not-implemented"
 end