Software / code / prosody-modules
Comparison
mod_migrate/mod_migrate.lua @ 1813:f02f52a2ee11
mod_migrate: Add support for migrating archives
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Tue, 01 Sep 2015 10:59:43 +0200 |
| parent | 1812:22b799c7b50a |
| child | 1985:7821a6986e68 |
comparison
equal
deleted
inserted
replaced
| 1812:22b799c7b50a | 1813:f02f52a2ee11 |
|---|---|
| 5 local mm = require"core.modulemanager"; | 5 local mm = require"core.modulemanager"; |
| 6 | 6 |
| 7 function module.command(arg) | 7 function module.command(arg) |
| 8 local host, source_store, migrate_to, user = unpack(arg); | 8 local host, source_store, migrate_to, user = unpack(arg); |
| 9 if not migrate_to then | 9 if not migrate_to then |
| 10 return print("Usage: prosodyctl mod_migrate example.com <source-store> <target-driver> [users]*"); | 10 return print("Usage: prosodyctl mod_migrate example.com <source-store>[-<store-type>] <target-driver> [users]*"); |
| 11 end | 11 end |
| 12 sm.initialize_host(host); | 12 sm.initialize_host(host); |
| 13 um.initialize_host(host); | 13 um.initialize_host(host); |
| 14 local module = module:context(host); | 14 local module = module:context(host); |
| 15 local storage = module:open_store(source_store); | 15 local store_type = source_store:match("%-(%a+)$"); |
| 16 if store_type then | |
| 17 source_store = source_store:sub(1, -2-#store_type); | |
| 18 end | |
| 19 local storage = module:open_store(source_store, store_type); | |
| 16 local target = assert(sm.load_driver(host, migrate_to)); | 20 local target = assert(sm.load_driver(host, migrate_to)); |
| 17 target = assert(target:open(source_store)); | 21 target = assert(target:open(source_store, store_type)); |
| 22 | |
| 18 local function migrate_user(username) | 23 local function migrate_user(username) |
| 19 module:log("info", "Migrating data for %s", username); | 24 module:log("info", "Migrating data for %s", username); |
| 20 local data, err = storage:get(username); | 25 local data, err = storage:get(username); |
| 21 assert(data or err==nil, err); | 26 assert(data or err==nil, err); |
| 22 assert(target:set(username, data)); | 27 assert(target:set(username, data)); |
| 28 end | |
| 29 | |
| 30 if store_type == "archive" then | |
| 31 function migrate_user(username) | |
| 32 module:log("info", "Migrating archive items for %s", username); | |
| 33 local count, errs = 0, 0; | |
| 34 for id, item, when, with in storage:find(username) do | |
| 35 local ok, err = target:append(username, id, item, when, with); | |
| 36 if ok then | |
| 37 count = count + 1; | |
| 38 else | |
| 39 module:log("warn", "Error: %s", err); | |
| 40 errs = errs + 1; | |
| 41 end | |
| 42 if ( count + errs ) % 100 == 0 then | |
| 43 module:log("info", "%d items migrated, %d errors", count, errs); | |
| 44 end | |
| 45 end | |
| 46 module:log("info", "%d items migrated, %d errors", count, errs); | |
| 47 end | |
| 23 end | 48 end |
| 24 | 49 |
| 25 if arg[4] then | 50 if arg[4] then |
| 26 for i = 4, #arg do | 51 for i = 4, #arg do |
| 27 migrate_user(arg[i]); | 52 migrate_user(arg[i]); |