Software /
code /
prosody
Comparison
plugins/mod_storage_sql.lua @ 7758:2b305ec8c146
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 02 Dec 2016 11:13:05 +0100 |
parent | 7359:a5a080c12c96 |
parent | 7757:437fb77e5ded |
child | 7857:db48b1697234 |
comparison
equal
deleted
inserted
replaced
7747:0e442402cebc | 7758:2b305ec8c146 |
---|---|
5 local sql = require "util.sql"; | 5 local sql = require "util.sql"; |
6 local xml_parse = require "util.xml".parse; | 6 local xml_parse = require "util.xml".parse; |
7 local uuid = require "util.uuid"; | 7 local uuid = require "util.uuid"; |
8 local resolve_relative_path = require "util.paths".resolve_relative_path; | 8 local resolve_relative_path = require "util.paths".resolve_relative_path; |
9 | 9 |
10 local stanza_mt = require"util.stanza".stanza_mt; | 10 local is_stanza = require"util.stanza".is_stanza; |
11 local getmetatable = getmetatable; | |
12 local t_concat = table.concat; | 11 local t_concat = table.concat; |
13 local function is_stanza(x) return getmetatable(x) == stanza_mt; end | |
14 | 12 |
15 local noop = function() end | 13 local noop = function() end |
16 local unpack = unpack | 14 local unpack = unpack |
17 local function iterator(result) | 15 local function iterator(result) |
18 return function(result_) | 16 return function(result_) |
433 end | 431 end |
434 end | 432 end |
435 return changes; | 433 return changes; |
436 end | 434 end |
437 | 435 |
436 local function normalize_database(driver, database) | |
437 if driver == "SQLite3" and database ~= ":memory:" then | |
438 return resolve_relative_path(prosody.paths.data or ".", database or "prosody.sqlite"); | |
439 end | |
440 return database; | |
441 end | |
442 | |
438 local function normalize_params(params) | 443 local function normalize_params(params) |
439 if params.driver == "SQLite3" then | 444 return { |
440 if params.database ~= ":memory:" then | 445 driver = assert(params.driver, "Configuration error: Both the SQL driver and the database need to be specified"); |
441 params.database = resolve_relative_path(prosody.paths.data or ".", params.database or "prosody.sqlite"); | 446 database = assert(normalize_database(params.driver, params.database), "Configuration error: Both the SQL driver and the database need to be specified"); |
442 end | 447 username = params.username; |
443 end | 448 password = params.password; |
444 assert(params.driver and params.database, "Configuration error: Both the SQL driver and the database need to be specified"); | 449 host = params.host; |
445 return params; | 450 port = params.port; |
451 }; | |
446 end | 452 end |
447 | 453 |
448 function module.load() | 454 function module.load() |
449 if prosody.prosodyctl then return; end | 455 if prosody.prosodyctl then return; end |
450 local engines = module:shared("/*/sql/connections"); | 456 local engines = module:shared("/*/sql/connections"); |
476 local command = table.remove(arg, 1); | 482 local command = table.remove(arg, 1); |
477 if command == "upgrade" then | 483 if command == "upgrade" then |
478 -- We need to find every unique dburi in the config | 484 -- We need to find every unique dburi in the config |
479 local uris = {}; | 485 local uris = {}; |
480 for host in pairs(prosody.hosts) do | 486 for host in pairs(prosody.hosts) do |
481 local params = config.get(host, "sql") or default_params; | 487 local params = normalize_params(config.get(host, "sql") or default_params); |
482 uris[sql.db2uri(params)] = params; | 488 uris[sql.db2uri(params)] = params; |
483 end | 489 end |
484 print("We will check and upgrade the following databases:\n"); | 490 print("We will check and upgrade the following databases:\n"); |
485 for _, params in pairs(uris) do | 491 for _, params in pairs(uris) do |
486 print("", "["..params.driver.."] "..params.database..(params.host and " on "..params.host or "")); | 492 print("", "["..params.driver.."] "..params.database..(params.host and " on "..params.host or "")); |
496 print("Checking "..params.database.."..."); | 502 print("Checking "..params.database.."..."); |
497 engine = sql:create_engine(params); | 503 engine = sql:create_engine(params); |
498 upgrade_table(params, true); | 504 upgrade_table(params, true); |
499 end | 505 end |
500 print("All done!"); | 506 print("All done!"); |
507 elseif command then | |
508 print("Unknown command: "..command); | |
501 else | 509 else |
502 print("Unknown command: "..command); | 510 print("Available commands:"); |
503 end | 511 print("","upgrade - Perform database upgrade"); |
504 end | 512 end |
513 end |