diff --git a/README.md b/README.md index 02561fd..628a315 100644 --- a/README.md +++ b/README.md @@ -36,11 +36,6 @@ Overview: * `v2` every player has its own (in-) mailbox in the `/mails/.json` file * `v3` every player has an entry in the `` modstorage (inbox, outbox, contacts) -Mails in the v1 format are supported until commit `b0a5bc7e47ec1c75339e65ec07d0a0ac2b17288b`. -Everything after that assumes either the v2 or v3 is used. - -For a v1 to v3 migration the version in `b0a5bc7e47ec1c75339e65ec07d0a0ac2b17288b` has to be at leas run once (startup). - # Dependencies * None diff --git a/migrate.lua b/migrate.lua index a76f801..0623b97 100644 --- a/migrate.lua +++ b/migrate.lua @@ -1,6 +1,38 @@ local STORAGE_VERSION_KEY = "@@version" +local function migrate_v1_to_v3() + local file = io.open(minetest.get_worldpath().."/mail.db", "r") + assert(file) + print("[mail] Migration from v1 to v3 database") + + local data = file:read("*a") + local oldmails = minetest.deserialize(data) + file:close() + + for name, oldmessages in pairs(oldmails) do + local entry = mail.get_storage_entry(name) + for _, msg in ipairs(oldmessages) do + if msg.to then + table.insert(entry.inbox, { + id = mail.new_uuid(), + from = msg.sender or msg.from, + to = msg.to, + subject = msg.subject, + body = msg.body, + time = msg.time, + }) + end + end + mail.set_storage_entry(name, entry) + end + + -- rename file + print("[mail] migration done, renaming old mail.db") + os.rename(minetest.get_worldpath().."/mail.db", minetest.get_worldpath().."/mail.db.old") + mail.storage:set_int(STORAGE_VERSION_KEY, 3) +end + local function read_json_file(path) local file = io.open(path, "r") local content = {} @@ -12,21 +44,13 @@ local function read_json_file(path) return content end - -function mail.migrate() - local version = mail.storage:get_int(STORAGE_VERSION_KEY) - if version < 3 then - mail.migrate_v2_to_v3() - mail.storage:set_int(STORAGE_VERSION_KEY, 3) - end -end - -- migrate from v2 to v3 database -function mail.migrate_v2_to_v3() +local function migrate_v2_to_v3() minetest.mkdir(mail.maildir) -- if necessary (eg. first login) print("[mail] Migration from v2 to v3 database") - minetest.after(0,function() + -- defer execution until auth-handler ready (first server-step) + minetest.after(0, function() for playername, _ in minetest.get_auth_handler().iterate() do local entry = mail.get_storage_entry(playername) @@ -53,5 +77,21 @@ function mail.migrate_v2_to_v3() mail.set_storage_entry(playername, entry) end + print("[mail] migration done") + mail.storage:set_int(STORAGE_VERSION_KEY, 3) end) end + +function mail.migrate() + local v1_file = io.open(minetest.get_worldpath().."/mail.db", "r") + if v1_file then + -- v1 to v3 + migrate_v1_to_v3() + end + + local version = mail.storage:get_int(STORAGE_VERSION_KEY) + if version < 3 then + -- v2 to v3 + migrate_v2_to_v3() + end +end \ No newline at end of file