mirror of
https://github.com/mt-mods/mail.git
synced 2025-07-15 10:46:41 -04:00
re-enable migrations
This commit is contained in:
parent
b1ccd494f7
commit
50c1415d36
2 changed files with 51 additions and 16 deletions
|
@ -36,11 +36,6 @@ Overview:
|
||||||
* `v2` every player has its own (in-) mailbox in the `<worldfolder>/mails/<playername>.json` file
|
* `v2` every player has its own (in-) mailbox in the `<worldfolder>/mails/<playername>.json` file
|
||||||
* `v3` every player has an entry in the `<playername>` modstorage (inbox, outbox, contacts)
|
* `v3` every player has an entry in the `<playername>` 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
|
# Dependencies
|
||||||
* None
|
* None
|
||||||
|
|
||||||
|
|
62
migrate.lua
62
migrate.lua
|
@ -1,6 +1,38 @@
|
||||||
|
|
||||||
local STORAGE_VERSION_KEY = "@@version"
|
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 function read_json_file(path)
|
||||||
local file = io.open(path, "r")
|
local file = io.open(path, "r")
|
||||||
local content = {}
|
local content = {}
|
||||||
|
@ -12,21 +44,13 @@ local function read_json_file(path)
|
||||||
return content
|
return content
|
||||||
end
|
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
|
-- 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)
|
minetest.mkdir(mail.maildir) -- if necessary (eg. first login)
|
||||||
print("[mail] Migration from v2 to v3 database")
|
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
|
for playername, _ in minetest.get_auth_handler().iterate() do
|
||||||
local entry = mail.get_storage_entry(playername)
|
local entry = mail.get_storage_entry(playername)
|
||||||
|
|
||||||
|
@ -53,5 +77,21 @@ function mail.migrate_v2_to_v3()
|
||||||
|
|
||||||
mail.set_storage_entry(playername, entry)
|
mail.set_storage_entry(playername, entry)
|
||||||
end
|
end
|
||||||
|
print("[mail] migration done")
|
||||||
|
mail.storage:set_int(STORAGE_VERSION_KEY, 3)
|
||||||
end)
|
end)
|
||||||
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
|
Loading…
Add table
Add a link
Reference in a new issue