Software / code / prosody
Comparison
plugins/mod_storage_sql.lua @ 12846:1638991caed2
mod_storage_sql: Support SQLite3 without LuaDBI
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Thu, 19 Jan 2023 16:56:12 +0100 |
| parent | 12830:0c3184378032 |
| child | 12872:a20923f7d5fd |
comparison
equal
deleted
inserted
replaced
| 12845:f306336b7e99 | 12846:1638991caed2 |
|---|---|
| 1 | 1 |
| 2 -- luacheck: ignore 212/self | 2 -- luacheck: ignore 212/self |
| 3 | 3 |
| 4 local deps = require "util.dependencies"; | |
| 4 local cache = require "util.cache"; | 5 local cache = require "util.cache"; |
| 5 local json = require "util.json"; | 6 local json = require "util.json"; |
| 6 local sql = require "util.sql"; | 7 local sqlite = deps.softreq "util.sqlite3"; |
| 8 local dbisql = (sqlite and deps.softreq or require) "util.sql"; | |
| 7 local xml_parse = require "util.xml".parse; | 9 local xml_parse = require "util.xml".parse; |
| 8 local uuid = require "util.uuid"; | 10 local uuid = require "util.uuid"; |
| 9 local resolve_relative_path = require "util.paths".resolve_relative_path; | 11 local resolve_relative_path = require "util.paths".resolve_relative_path; |
| 10 local jid_join = require "util.jid".join; | 12 local jid_join = require "util.jid".join; |
| 11 | 13 |
| 690 | 692 |
| 691 --- Initialization | 693 --- Initialization |
| 692 | 694 |
| 693 | 695 |
| 694 local function create_table(engine) -- luacheck: ignore 431/engine | 696 local function create_table(engine) -- luacheck: ignore 431/engine |
| 697 local sql = engine.params.driver == "SQLite3" and sqlite or dbisql; | |
| 695 local Table, Column, Index = sql.Table, sql.Column, sql.Index; | 698 local Table, Column, Index = sql.Table, sql.Column, sql.Index; |
| 696 | 699 |
| 697 local ProsodyTable = Table { | 700 local ProsodyTable = Table { |
| 698 name = "prosody"; | 701 name = "prosody"; |
| 699 Column { name="host", type="TEXT", nullable=false }; | 702 Column { name="host", type="TEXT", nullable=false }; |
| 730 end | 733 end |
| 731 | 734 |
| 732 local function upgrade_table(engine, params, apply_changes) -- luacheck: ignore 431/engine | 735 local function upgrade_table(engine, params, apply_changes) -- luacheck: ignore 431/engine |
| 733 local changes = false; | 736 local changes = false; |
| 734 if params.driver == "MySQL" then | 737 if params.driver == "MySQL" then |
| 738 local sql = dbisql; | |
| 735 local success,err = engine:transaction(function() | 739 local success,err = engine:transaction(function() |
| 736 do | 740 do |
| 737 local result = assert(engine:execute("SHOW COLUMNS FROM \"prosody\" WHERE \"Field\"='value' and \"Type\"='text'")); | 741 local result = assert(engine:execute("SHOW COLUMNS FROM \"prosody\" WHERE \"Field\"='value' and \"Type\"='text'")); |
| 738 if result:rowcount() > 0 then | 742 if result:rowcount() > 0 then |
| 739 changes = true; | 743 changes = true; |
| 829 end | 833 end |
| 830 | 834 |
| 831 function module.load() | 835 function module.load() |
| 832 local engines = module:shared("/*/sql/connections"); | 836 local engines = module:shared("/*/sql/connections"); |
| 833 local params = normalize_params(module:get_option("sql", default_params)); | 837 local params = normalize_params(module:get_option("sql", default_params)); |
| 838 local sql = params.driver == "SQLite3" and sqlite or dbisql; | |
| 834 local db_uri = sql.db2uri(params); | 839 local db_uri = sql.db2uri(params); |
| 835 engine = engines[db_uri]; | 840 engine = engines[db_uri]; |
| 836 if not engine then | 841 if not engine then |
| 837 module:log("debug", "Creating new engine %s", db_uri); | 842 module:log("debug", "Creating new engine %s", db_uri); |
| 838 engine = sql:create_engine(params, function (engine) -- luacheck: ignore 431/engine | 843 engine = sql:create_engine(params, function (engine) -- luacheck: ignore 431/engine |
| 867 if command == "upgrade" then | 872 if command == "upgrade" then |
| 868 -- We need to find every unique dburi in the config | 873 -- We need to find every unique dburi in the config |
| 869 local uris = {}; | 874 local uris = {}; |
| 870 for host in pairs(prosody.hosts) do -- luacheck: ignore 431/host | 875 for host in pairs(prosody.hosts) do -- luacheck: ignore 431/host |
| 871 local params = normalize_params(config.get(host, "sql") or default_params); | 876 local params = normalize_params(config.get(host, "sql") or default_params); |
| 877 local sql = engine.params.driver == "SQLite3" and sqlite or dbisql; | |
| 872 uris[sql.db2uri(params)] = params; | 878 uris[sql.db2uri(params)] = params; |
| 873 end | 879 end |
| 874 print("We will check and upgrade the following databases:\n"); | 880 print("We will check and upgrade the following databases:\n"); |
| 875 for _, params in pairs(uris) do | 881 for _, params in pairs(uris) do |
| 876 print("", "["..params.driver.."] "..params.database..(params.host and " on "..params.host or "")); | 882 print("", "["..params.driver.."] "..params.database..(params.host and " on "..params.host or "")); |
| 882 return; | 888 return; |
| 883 end | 889 end |
| 884 -- Upgrade each one | 890 -- Upgrade each one |
| 885 for _, params in pairs(uris) do | 891 for _, params in pairs(uris) do |
| 886 print("Checking "..params.database.."..."); | 892 print("Checking "..params.database.."..."); |
| 893 local sql = params.driver == "SQLite3" and sqlite or dbisql; | |
| 887 engine = sql:create_engine(params); | 894 engine = sql:create_engine(params); |
| 888 upgrade_table(engine, params, true); | 895 upgrade_table(engine, params, true); |
| 889 end | 896 end |
| 890 print("All done!"); | 897 print("All done!"); |
| 891 elseif command then | 898 elseif command then |