Mercurial > prosody-modules
comparison mod_migrate_lastlog2/mod_migrate_lastlog2.lua @ 6231:a4d7fefa4a8b
mod_migrate_lastlog2: New module to automatically migrate data to mod_account_activity
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 28 May 2025 16:53:22 +0100 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 6230:ff7d6ee9db20 | 6231:a4d7fefa4a8b |
|---|---|
| 1 -- This module is based on the shell command code from mod_account_activity | |
| 2 | |
| 3 local autoremove = module:get_option_boolean("migrate_lastlog2_auto_remove", true); | |
| 4 | |
| 5 local function do_migration() | |
| 6 local store = module:open_store("account_activity", "keyval+"); | |
| 7 local lastlog2 = module:open_store("lastlog2", "keyval+"); | |
| 8 local n_updated, n_errors, n_skipped = 0, 0, 0; | |
| 9 | |
| 10 local async = require "prosody.util.async"; | |
| 11 | |
| 12 local p = require "prosody.util.promise".new(function (resolve) | |
| 13 local async_runner = async.runner(function () | |
| 14 local n = 0; | |
| 15 for username in lastlog2:items() do | |
| 16 local was_error = nil; | |
| 17 n = n + 1; | |
| 18 if n % 100 == 0 then | |
| 19 module:log("debug", "Processed %d...", n); | |
| 20 async.sleep(0); | |
| 21 end | |
| 22 local lastlog2_data = lastlog2:get(username); | |
| 23 if lastlog2_data then | |
| 24 local current_data, err = store:get(username); | |
| 25 if not current_data then | |
| 26 if not err then | |
| 27 current_data = {}; | |
| 28 else | |
| 29 n_errors = n_errors + 1; | |
| 30 end | |
| 31 end | |
| 32 if current_data then | |
| 33 local imported_timestamp = current_data.timestamp; | |
| 34 local latest; | |
| 35 for k, v in pairs(lastlog2_data) do | |
| 36 if k ~= "registered" and (not latest or v.timestamp > latest) then | |
| 37 latest = v.timestamp; | |
| 38 end | |
| 39 end | |
| 40 if latest and (not imported_timestamp or imported_timestamp < latest) then | |
| 41 local ok, err = store:set_key(username, "timestamp", latest); | |
| 42 if ok then | |
| 43 n_updated = n_updated + 1; | |
| 44 else | |
| 45 module:log("error", "Failed to import %q: %s", username, err); | |
| 46 was_error = true; | |
| 47 n_errors = n_errors + 1; | |
| 48 end | |
| 49 else | |
| 50 n_skipped = n_skipped + 1; | |
| 51 end | |
| 52 end | |
| 53 if autoremove and not was_error then | |
| 54 lastlog2:set(username, nil); | |
| 55 end | |
| 56 end | |
| 57 end | |
| 58 return resolve(("%d accounts imported, %d errors, %d skipped"):format(n_updated, n_errors, n_skipped)); | |
| 59 end); | |
| 60 async_runner:run(true); | |
| 61 end); | |
| 62 return p; | |
| 63 end | |
| 64 | |
| 65 function module.ready() | |
| 66 do_migration(); | |
| 67 end |
