From 79d0d2591d7986348abc0f26826fab0ee02cf9ee Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Mon, 7 Apr 2025 20:23:00 +0200 Subject: mod_storage_sql: Delay showing SQL library error until attempted load This should ensure that e.g. failure to load LuaSQLite3 is not logged unless it is needed, since module failures are very verbose. Closes #1919 --- plugins/mod_storage_sql.lua | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'plugins') diff --git a/plugins/mod_storage_sql.lua b/plugins/mod_storage_sql.lua index f053f729..f215ef65 100644 --- a/plugins/mod_storage_sql.lua +++ b/plugins/mod_storage_sql.lua @@ -13,20 +13,24 @@ local t_concat = table.concat; local have_dbisql, dbisql = pcall(require, "prosody.util.sql"); local have_sqlite, sqlite = pcall(require, "prosody.util.sqlite3"); -if not have_dbisql then - module:log("debug", "Could not load LuaDBI: %s", dbisql) - dbisql = nil; -end -if not have_sqlite then - module:log("debug", "Could not load LuaSQLite3: %s", sqlite) - sqlite = nil; -end if not (have_dbisql or have_sqlite) then module:log("error", "LuaDBI or LuaSQLite3 are required for using SQL databases but neither are installed"); module:log("error", "Please install at least one of LuaDBI and LuaSQLite3. See https://prosody.im/doc/depends"); + module:log("debug", "Could not load LuaDBI: %s", dbisql); + module:log("debug", "Could not load LuaSQLite3: %s", sqlite); error("No SQL library available") end +local function get_sql_lib(driver) + if driver == "SQLite3" and have_sqlite then + return sqlite; + elseif have_dbisql then + return dbisql; + else + error(dbisql); + end +end + local noop = function() end local unpack = table.unpack; local function iterator(result) @@ -757,7 +761,7 @@ end local function create_table(engine) -- luacheck: ignore 431/engine - local sql = engine.params.driver == "SQLite3" and sqlite or dbisql; + local sql = get_sql_lib(engine.params.driver); local Table, Column, Index = sql.Table, sql.Column, sql.Index; local ProsodyTable = Table { @@ -798,7 +802,7 @@ end local function upgrade_table(engine, params, apply_changes) -- luacheck: ignore 431/engine local changes = false; if params.driver == "MySQL" then - local sql = dbisql; + local sql = get_sql_lib("MySQL"); local success,err = engine:transaction(function() do local result = assert(engine:execute("SHOW COLUMNS FROM \"prosody\" WHERE \"Field\"='value' and \"Type\"='text'")); @@ -920,7 +924,7 @@ end function module.load() local engines = module:shared("/*/sql/connections"); local params = normalize_params(module:get_option("sql", default_params)); - local sql = params.driver == "SQLite3" and sqlite or dbisql; + local sql = get_sql_lib(params.driver); local db_uri = sql.db2uri(params); engine = engines[db_uri]; if not engine then @@ -1012,7 +1016,7 @@ function module.command(arg) local uris = {}; for host in pairs(prosody.hosts) do -- luacheck: ignore 431/host local params = normalize_params(config.get(host, "sql") or default_params); - local sql = engine.params.driver == "SQLite3" and sqlite or dbisql; + local sql = get_sql_lib(engine.params.driver); uris[sql.db2uri(params)] = params; end print("We will check and upgrade the following databases:\n"); @@ -1028,7 +1032,7 @@ function module.command(arg) -- Upgrade each one for _, params in pairs(uris) do print("Checking "..params.database.."..."); - local sql = params.driver == "SQLite3" and sqlite or dbisql; + local sql = get_sql_lib(params.driver); engine = sql:create_engine(params); upgrade_table(engine, params, true); end -- cgit v1.2.3