mod_migrate/mod_migrate.lua
changeset 1989 7821a6986e68
parent 1817 f02f52a2ee11
child 2032 530bf8ef2e55
equal deleted inserted replaced
1988:2ed6f6eeeaba 1989:7821a6986e68
     3 local sm = require"core.storagemanager";
     3 local sm = require"core.storagemanager";
     4 local um = require"core.usermanager";
     4 local um = require"core.usermanager";
     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_stores, 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>[-<store-type>] <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 store_type = source_store:match("%-(%a+)$");
    15 	for source_store in source_stores:gmatch("[^,]+") do
    16 	if store_type then
    16 		local store_type = source_store:match("%-(%a+)$");
    17 		source_store = source_store:sub(1, -2-#store_type);
    17 		if store_type then
    18 	end
    18 			source_store = source_store:sub(1, -2-#store_type);
    19 	local storage = module:open_store(source_store, store_type);
    19 		end
    20 	local target = assert(sm.load_driver(host, migrate_to));
    20 		local storage = module:open_store(source_store, store_type);
    21 	target = assert(target:open(source_store, store_type));
    21 		local target = assert(sm.load_driver(host, migrate_to));
       
    22 		target = assert(target:open(source_store, store_type));
    22 
    23 
    23 	local function migrate_user(username)
    24 		local function migrate_user(username)
    24 		module:log("info", "Migrating data for %s", username);
    25 			module:log("info", "Migrating data for %s", username);
    25 		local data, err = storage:get(username);
    26 			local data, err = storage:get(username);
    26 		assert(data or err==nil, err);
    27 			assert(data or err==nil, err);
    27 		assert(target:set(username, data));
    28 			assert(target:set(username, data));
    28 	end
    29 		end
    29 
    30 
    30 	if store_type == "archive" then
    31 		if store_type == "archive" then
    31 		function migrate_user(username)
    32 			function migrate_user(username)
    32 			module:log("info", "Migrating archive items for %s", username);
    33 				module:log("info", "Migrating archive items for %s", username);
    33 			local count, errs = 0, 0;
    34 				local count, errs = 0, 0;
    34 			for id, item, when, with in storage:find(username) do
    35 				for id, item, when, with in storage:find(username) do
    35 				local ok, err = target:append(username, id, item, when, with);
    36 					local ok, err = target:append(username, id, item, when, with);
    36 				if ok then
    37 					if ok then
    37 					count = count + 1;
    38 						count = count + 1;
    38 				else
    39 					else
    39 					module:log("warn", "Error: %s", err);
    40 						module:log("warn", "Error: %s", err);
    40 					errs = errs + 1;
    41 						errs = errs + 1;
       
    42 					end
       
    43 					if ( count + errs ) % 100 == 0 then
       
    44 						module:log("info", "%d items migrated, %d errors", count, errs);
       
    45 					end
    41 				end
    46 				end
    42 				if ( count + errs ) % 100 == 0 then
    47 				module:log("info", "%d items migrated, %d errors", count, errs);
    43 					module:log("info", "%d items migrated, %d errors", count, errs);
       
    44 				end
       
    45 			end
    48 			end
    46 			module:log("info", "%d items migrated, %d errors", count, errs);
       
    47 		end
    49 		end
    48 	end
       
    49 
    50 
    50 	if arg[4] then
    51 		if arg[4] then
    51 		for i = 4, #arg do
    52 			for i = 4, #arg do
    52 			migrate_user(arg[i]);
    53 				migrate_user(arg[i]);
    53 		end
    54 			end
    54 	else
    55 		else
    55 		for user in um.users(host) do
    56 			for user in um.users(host) do
    56 			migrate_user(user);
    57 				migrate_user(user);
       
    58 			end
    57 		end
    59 		end
    58 	end
    60 	end
    59 end
    61 end