Comparison

mod_migrate/mod_migrate.lua @ 4302:c9e1eee6a948

mod_migrate: Add support for the fictitious pep_data store
author Matthew Wild <mwild1@gmail.com>
date Tue, 15 Dec 2020 23:40:18 +0000
parent 3584:611ac62c5b63
child 4398:a16b689525d7
comparison
equal deleted inserted replaced
4301:bcb2b9adfcde 4302:c9e1eee6a948
1 -- mod_migrate 1 -- mod_migrate
2 2
3 local unpack = table.unpack or unpack; 3 local unpack = table.unpack or unpack; --luacheck: ignore 113/unpack
4 local sm = require"core.storagemanager"; 4 local sm = require"core.storagemanager";
5 local um = require"core.usermanager"; 5 local um = require"core.usermanager";
6 6
7 local function users(store, host) 7 local function users(store, host)
8 if store.users then 8 if store.users then
9 return store:users(); 9 return store:users();
10 else 10 else
11 return um.users(host); 11 return um.users(host);
12 end
13 end
14
15 local function stores(host)
16 if store.users then
17 return store:users();
18 else
19 return um.users(host);
20 end
21 end
22
23 local function migrate_store(host, source_store, store_type, migrate_to, migrate_users)
24 local module = module:context(host);
25 local storage = module:open_store(source_store, store_type);
26 local target = assert(sm.load_driver(host, migrate_to));
27 target = assert(target:open(source_store, store_type));
28
29 local function migrate_user(username)
30 module:log("info", "Migrating %s data for %s", source_store, username);
31 local data, err = storage:get(username);
32 if not data and err then
33 module:log("error", "Could not read data: %s", err);
34 else
35 local ok, err = target:set(username, data);
36 if not ok then
37 module:log("error", "Could not write data: %s", err);
38 end
39 end
40 end
41
42 if store_type == "archive" then
43 function migrate_user(username)
44 module:log("info", "Migrating %s archive items for %s", source_store, username);
45 local count, errs = 0, 0;
46 for id, item, when, with in storage:find(username) do
47 local ok, err = target:append(username, id, item, when, with);
48 if ok then
49 count = count + 1;
50 else
51 module:log("warn", "Error: %s", err);
52 errs = errs + 1;
53 end
54 if ( count + errs ) % 100 == 0 then
55 module:log("info", "%d items migrated, %d errors", count, errs);
56 end
57 end
58 module:log("info", "%d items migrated, %d errors", count, errs);
59 end
60 end
61
62 if migrate_users then
63 for _, username in ipairs(migrate_users) do
64 migrate_user(username);
65 end
66 else
67 xpcall(function()
68 for username in users(storage, host) do
69 migrate_user(username);
70 end
71 end,
72 function (err)
73 module:log("error", "Could not list users, you'll have to supply them as arguments");
74 module:log("error", "The error was: %s", err);
75 end);
12 end 76 end
13 end 77 end
14 78
15 function module.command(arg) 79 function module.command(arg)
16 local host, source_stores, migrate_to = unpack(arg); 80 local host, source_stores, migrate_to = unpack(arg);
20 if not prosody.hosts[host] then 84 if not prosody.hosts[host] then
21 return print(("The host %q is not know by Prosody."):format(host)); 85 return print(("The host %q is not know by Prosody."):format(host));
22 end 86 end
23 sm.initialize_host(host); 87 sm.initialize_host(host);
24 um.initialize_host(host); 88 um.initialize_host(host);
25 local module = module:context(host);
26 for source_store in source_stores:gmatch("[^,]+") do 89 for source_store in source_stores:gmatch("[^,]+") do
27 local store_type = source_store:match("%-(%a+)$"); 90 local store_type = source_store:match("%-(%a+)$");
28 if store_type then 91 if store_type then
29 source_store = source_store:sub(1, -2-#store_type); 92 source_store = source_store:sub(1, -2-#store_type);
30 end 93 end
31 local storage = module:open_store(source_store, store_type); 94 local migrate_users;
32 local target = assert(sm.load_driver(host, migrate_to)); 95 if arg[4] then
33 target = assert(target:open(source_store, store_type)); 96 migrate_users = {};
34 97 for i = 4, #arg do
35 local function migrate_user(username) 98 migrate_users[i-3] = arg[i];
36 module:log("info", "Migrating %s data for %s", source_store, username); 99 end
37 local data, err = storage:get(username); 100 end
38 if not data and err then 101 if source_store == "pep_data" then
39 module:log("error", "Could not read data: %s", err); 102 for store in sm.get_driver(host, source_store):stores(true) do
40 else 103 if store:match("^pep_") then
41 local ok, err = target:set(username, data); 104 print("Migrating "..store);
42 if not ok then 105 migrate_store(host, store, store_type, migrate_to, migrate_users);
43 module:log("error", "Could not write data: %s", err);
44 end 106 end
45 end 107 end
46 end
47
48 if store_type == "archive" then
49 function migrate_user(username)
50 module:log("info", "Migrating %s archive items for %s", source_store, username);
51 local count, errs = 0, 0;
52 for id, item, when, with in storage:find(username) do
53 local ok, err = target:append(username, id, item, when, with);
54 if ok then
55 count = count + 1;
56 else
57 module:log("warn", "Error: %s", err);
58 errs = errs + 1;
59 end
60 if ( count + errs ) % 100 == 0 then
61 module:log("info", "%d items migrated, %d errors", count, errs);
62 end
63 end
64 module:log("info", "%d items migrated, %d errors", count, errs);
65 end
66 end
67
68 if arg[4] then
69 for i = 4, #arg do
70 migrate_user(arg[i]);
71 end
72 else 108 else
73 xpcall(function() 109 migrate_store(host, source_store, store_type, migrate_to, migrate_users);
74 for user in users(storage, host) do
75 migrate_user(user);
76 end
77 end,
78 function (err)
79 module:log("error", "Could not list users, you'll have to supply them as arguments");
80 module:log("error", "The error was: %s", err);
81 end);
82 end 110 end
83 end 111 end
84 end 112 end