migrator: Refactor out individual item migrator for code deduplication
authorKim Alvefur <zash@zash.se>
Sun, 09 Jan 2022 15:50:21 +0100
changeset 12168 85f03b29ec72
parent 12167 59b65cc6312f
child 12169 79a51a48bdaf
migrator: Refactor out individual item migrator for code deduplication
tools/migration/prosody-migrator.lua
--- a/tools/migration/prosody-migrator.lua	Sun Jan 09 16:15:21 2022 +0100
+++ b/tools/migration/prosody-migrator.lua	Sun Jan 09 15:50:21 2022 +0100
@@ -168,6 +168,21 @@
 	return assert(sm.load_driver(host, conf.type));
 end
 
+local migrate_once = {
+	keyval = function(origin, destination, user)
+		local data, err = origin:get(user);
+		assert(not err, err);
+		assert(destination:set(user, data));
+	end;
+	archive = function(origin, destination, user)
+		local iter, err = origin:find(user);
+		assert(iter, err);
+		for id, item, when, with in iter do
+			assert(destination:append(user, id, item, when, with));
+		end
+	end;
+}
+
 local migration_runner = async.runner(function (job)
 	for host, stores in pairs(job.input.hosts) do
 		prosody.hosts[host] = startup.make_host(host);
@@ -186,26 +201,13 @@
 			local origin = assert(input_driver:open(store, typ));
 			local destination = assert(output_driver:open(store, typ));
 
+			local migrate = assert(migrate_once[typ], "Unknown store type: "..typ);
 			if typ == "keyval" then -- host data
-				local data, err = origin:get(nil);
-				assert(not err, err);
-				assert(destination:set(nil, data));
+				migrate(origin, destination, nil);
 			end
 
 			for user in users(origin, host) do
-				if typ == "keyval" then
-					local data, err = origin:get(user);
-					assert(not err, err);
-					assert(destination:set(user, data));
-				elseif typ == "archive" then
-					local iter, err = origin:find(user);
-					assert(iter, err);
-					for id, item, when, with in iter do
-						assert(destination:append(user, id, item, when, with));
-					end
-				else
-					error("Don't know how to migrate data of type '"..typ.."'.");
-				end
+				migrate(origin, destination, user);
 			end
 		end
 	end