421 end |
421 end |
422 |
422 |
423 --- Initialization |
423 --- Initialization |
424 |
424 |
425 |
425 |
426 local function create_table(name) |
426 local function create_table(engine, name) -- luacheck: ignore 431/engine |
427 local Table, Column, Index = sql.Table, sql.Column, sql.Index; |
427 local Table, Column, Index = sql.Table, sql.Column, sql.Index; |
428 |
428 |
429 local ProsodyTable = Table { |
429 local ProsodyTable = Table { |
430 name= name or "prosody"; |
430 name= name or "prosody"; |
431 Column { name="host", type="TEXT", nullable=false }; |
431 Column { name="host", type="TEXT", nullable=false }; |
456 engine:transaction(function() |
456 engine:transaction(function() |
457 ProsodyArchiveTable:create(engine); |
457 ProsodyArchiveTable:create(engine); |
458 end); |
458 end); |
459 end |
459 end |
460 |
460 |
461 local function upgrade_table(params, apply_changes) |
461 local function upgrade_table(engine, params, apply_changes) -- luacheck: ignore 431/engine |
462 local changes = false; |
462 local changes = false; |
463 if params.driver == "MySQL" then |
463 if params.driver == "MySQL" then |
464 local success,err = engine:transaction(function() |
464 local success,err = engine:transaction(function() |
465 local result = engine:execute("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'"); |
465 local result = engine:execute("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'"); |
466 if result:rowcount() > 0 then |
466 if result:rowcount() > 0 then |
538 local engines = module:shared("/*/sql/connections"); |
538 local engines = module:shared("/*/sql/connections"); |
539 local params = normalize_params(module:get_option("sql", default_params)); |
539 local params = normalize_params(module:get_option("sql", default_params)); |
540 engine = engines[sql.db2uri(params)]; |
540 engine = engines[sql.db2uri(params)]; |
541 if not engine then |
541 if not engine then |
542 module:log("debug", "Creating new engine"); |
542 module:log("debug", "Creating new engine"); |
543 engine = sql:create_engine(params, function (engine) |
543 engine = sql:create_engine(params, function (engine) -- luacheck: ignore 431/engine |
544 if module:get_option("sql_manage_tables", true) then |
544 if module:get_option("sql_manage_tables", true) then |
545 -- Automatically create table, ignore failure (table probably already exists) |
545 -- Automatically create table, ignore failure (table probably already exists) |
546 -- FIXME: we should check in information_schema, etc. |
546 -- FIXME: we should check in information_schema, etc. |
547 create_table(); |
547 create_table(engine); |
548 -- Check whether the table needs upgrading |
548 -- Check whether the table needs upgrading |
549 if upgrade_table(params, false) then |
549 if upgrade_table(engine, params, false) then |
550 module:log("error", "Old database format detected. Please run: prosodyctl mod_%s upgrade", module.name); |
550 module:log("error", "Old database format detected. Please run: prosodyctl mod_%s upgrade", module.name); |
551 return false, "database upgrade needed"; |
551 return false, "database upgrade needed"; |
552 end |
552 end |
553 end |
553 end |
554 end); |
554 end); |
581 end |
581 end |
582 -- Upgrade each one |
582 -- Upgrade each one |
583 for _, params in pairs(uris) do |
583 for _, params in pairs(uris) do |
584 print("Checking "..params.database.."..."); |
584 print("Checking "..params.database.."..."); |
585 engine = sql:create_engine(params); |
585 engine = sql:create_engine(params); |
586 upgrade_table(params, true); |
586 upgrade_table(engine, params, true); |
587 end |
587 end |
588 print("All done!"); |
588 print("All done!"); |
589 elseif command then |
589 elseif command then |
590 print("Unknown command: "..command); |
590 print("Unknown command: "..command); |
591 else |
591 else |