Diff

plugins/mod_storage_sql.lua @ 13832:5973a5d22ab5 13.0

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
author Kim Alvefur <zash@zash.se>
date Mon, 07 Apr 2025 20:23:00 +0200
parent 13784:b30eaa74d35b
child 13833:497efa2cbf2c
line wrap: on
line diff
--- a/plugins/mod_storage_sql.lua	Wed Apr 09 10:54:01 2025 +0100
+++ b/plugins/mod_storage_sql.lua	Mon Apr 07 20:23:00 2025 +0200
@@ -13,20 +13,24 @@
 
 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 @@
 
 
 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 @@
 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 @@
 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 @@
 		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 @@
 		-- 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