Comparison

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
comparison
equal deleted inserted replaced
13830:145b71d1283c 13832:5973a5d22ab5
11 local is_stanza = require"prosody.util.stanza".is_stanza; 11 local is_stanza = require"prosody.util.stanza".is_stanza;
12 local t_concat = table.concat; 12 local t_concat = table.concat;
13 13
14 local have_dbisql, dbisql = pcall(require, "prosody.util.sql"); 14 local have_dbisql, dbisql = pcall(require, "prosody.util.sql");
15 local have_sqlite, sqlite = pcall(require, "prosody.util.sqlite3"); 15 local have_sqlite, sqlite = pcall(require, "prosody.util.sqlite3");
16 if not have_dbisql then
17 module:log("debug", "Could not load LuaDBI: %s", dbisql)
18 dbisql = nil;
19 end
20 if not have_sqlite then
21 module:log("debug", "Could not load LuaSQLite3: %s", sqlite)
22 sqlite = nil;
23 end
24 if not (have_dbisql or have_sqlite) then 16 if not (have_dbisql or have_sqlite) then
25 module:log("error", "LuaDBI or LuaSQLite3 are required for using SQL databases but neither are installed"); 17 module:log("error", "LuaDBI or LuaSQLite3 are required for using SQL databases but neither are installed");
26 module:log("error", "Please install at least one of LuaDBI and LuaSQLite3. See https://prosody.im/doc/depends"); 18 module:log("error", "Please install at least one of LuaDBI and LuaSQLite3. See https://prosody.im/doc/depends");
19 module:log("debug", "Could not load LuaDBI: %s", dbisql);
20 module:log("debug", "Could not load LuaSQLite3: %s", sqlite);
27 error("No SQL library available") 21 error("No SQL library available")
22 end
23
24 local function get_sql_lib(driver)
25 if driver == "SQLite3" and have_sqlite then
26 return sqlite;
27 elseif have_dbisql then
28 return dbisql;
29 else
30 error(dbisql);
31 end
28 end 32 end
29 33
30 local noop = function() end 34 local noop = function() end
31 local unpack = table.unpack; 35 local unpack = table.unpack;
32 local function iterator(result) 36 local function iterator(result)
755 759
756 --- Initialization 760 --- Initialization
757 761
758 762
759 local function create_table(engine) -- luacheck: ignore 431/engine 763 local function create_table(engine) -- luacheck: ignore 431/engine
760 local sql = engine.params.driver == "SQLite3" and sqlite or dbisql; 764 local sql = get_sql_lib(engine.params.driver);
761 local Table, Column, Index = sql.Table, sql.Column, sql.Index; 765 local Table, Column, Index = sql.Table, sql.Column, sql.Index;
762 766
763 local ProsodyTable = Table { 767 local ProsodyTable = Table {
764 name = "prosody"; 768 name = "prosody";
765 Column { name="host", type="TEXT", nullable=false }; 769 Column { name="host", type="TEXT", nullable=false };
796 end 800 end
797 801
798 local function upgrade_table(engine, params, apply_changes) -- luacheck: ignore 431/engine 802 local function upgrade_table(engine, params, apply_changes) -- luacheck: ignore 431/engine
799 local changes = false; 803 local changes = false;
800 if params.driver == "MySQL" then 804 if params.driver == "MySQL" then
801 local sql = dbisql; 805 local sql = get_sql_lib("MySQL");
802 local success,err = engine:transaction(function() 806 local success,err = engine:transaction(function()
803 do 807 do
804 local result = assert(engine:execute("SHOW COLUMNS FROM \"prosody\" WHERE \"Field\"='value' and \"Type\"='text'")); 808 local result = assert(engine:execute("SHOW COLUMNS FROM \"prosody\" WHERE \"Field\"='value' and \"Type\"='text'"));
805 if result:rowcount() > 0 then 809 if result:rowcount() > 0 then
806 changes = true; 810 changes = true;
918 end 922 end
919 923
920 function module.load() 924 function module.load()
921 local engines = module:shared("/*/sql/connections"); 925 local engines = module:shared("/*/sql/connections");
922 local params = normalize_params(module:get_option("sql", default_params)); 926 local params = normalize_params(module:get_option("sql", default_params));
923 local sql = params.driver == "SQLite3" and sqlite or dbisql; 927 local sql = get_sql_lib(params.driver);
924 local db_uri = sql.db2uri(params); 928 local db_uri = sql.db2uri(params);
925 engine = engines[db_uri]; 929 engine = engines[db_uri];
926 if not engine then 930 if not engine then
927 module:log("debug", "Creating new engine %s", db_uri); 931 module:log("debug", "Creating new engine %s", db_uri);
928 engine = sql:create_engine(params, function (engine) -- luacheck: ignore 431/engine 932 engine = sql:create_engine(params, function (engine) -- luacheck: ignore 431/engine
1010 if command == "upgrade" then 1014 if command == "upgrade" then
1011 -- We need to find every unique dburi in the config 1015 -- We need to find every unique dburi in the config
1012 local uris = {}; 1016 local uris = {};
1013 for host in pairs(prosody.hosts) do -- luacheck: ignore 431/host 1017 for host in pairs(prosody.hosts) do -- luacheck: ignore 431/host
1014 local params = normalize_params(config.get(host, "sql") or default_params); 1018 local params = normalize_params(config.get(host, "sql") or default_params);
1015 local sql = engine.params.driver == "SQLite3" and sqlite or dbisql; 1019 local sql = get_sql_lib(engine.params.driver);
1016 uris[sql.db2uri(params)] = params; 1020 uris[sql.db2uri(params)] = params;
1017 end 1021 end
1018 print("We will check and upgrade the following databases:\n"); 1022 print("We will check and upgrade the following databases:\n");
1019 for _, params in pairs(uris) do 1023 for _, params in pairs(uris) do
1020 print("", "["..params.driver.."] "..params.database..(params.host and " on "..params.host or "")); 1024 print("", "["..params.driver.."] "..params.database..(params.host and " on "..params.host or ""));
1026 return; 1030 return;
1027 end 1031 end
1028 -- Upgrade each one 1032 -- Upgrade each one
1029 for _, params in pairs(uris) do 1033 for _, params in pairs(uris) do
1030 print("Checking "..params.database.."..."); 1034 print("Checking "..params.database.."...");
1031 local sql = params.driver == "SQLite3" and sqlite or dbisql; 1035 local sql = get_sql_lib(params.driver);
1032 engine = sql:create_engine(params); 1036 engine = sql:create_engine(params);
1033 upgrade_table(engine, params, true); 1037 upgrade_table(engine, params, true);
1034 end 1038 end
1035 print("All done!"); 1039 print("All done!");
1036 elseif command then 1040 elseif command then