Diff

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
line wrap: on
line diff
--- a/mod_migrate/mod_migrate.lua	Tue Sep 01 10:30:08 2015 +0200
+++ b/mod_migrate/mod_migrate.lua	Tue Sep 01 10:59:43 2015 +0200
@@ -7,14 +7,19 @@
 function module.command(arg)
 	local host, source_store, migrate_to, user = unpack(arg);
 	if not migrate_to then
-		return print("Usage: prosodyctl mod_migrate example.com <source-store> <target-driver> [users]*");
+		return print("Usage: prosodyctl mod_migrate example.com <source-store>[-<store-type>] <target-driver> [users]*");
 	end
 	sm.initialize_host(host);
 	um.initialize_host(host);
 	local module = module:context(host);
-	local storage = module:open_store(source_store);
+	local store_type = source_store:match("%-(%a+)$");
+	if store_type then
+		source_store = source_store:sub(1, -2-#store_type);
+	end
+	local storage = module:open_store(source_store, store_type);
 	local target = assert(sm.load_driver(host, migrate_to));
-	target = assert(target:open(source_store));
+	target = assert(target:open(source_store, store_type));
+
 	local function migrate_user(username)
 		module:log("info", "Migrating data for %s", username);
 		local data, err = storage:get(username);
@@ -22,6 +27,26 @@
 		assert(target:set(username, data));
 	end
 
+	if store_type == "archive" then
+		function migrate_user(username)
+			module:log("info", "Migrating archive items for %s", username);
+			local count, errs = 0, 0;
+			for id, item, when, with in storage:find(username) do
+				local ok, err = target:append(username, id, item, when, with);
+				if ok then
+					count = count + 1;
+				else
+					module:log("warn", "Error: %s", err);
+					errs = errs + 1;
+				end
+				if ( count + errs ) % 100 == 0 then
+					module:log("info", "%d items migrated, %d errors", count, errs);
+				end
+			end
+			module:log("info", "%d items migrated, %d errors", count, errs);
+		end
+	end
+
 	if arg[4] then
 		for i = 4, #arg do
 			migrate_user(arg[i]);