re-enable migrations

This commit is contained in:
BuckarooBanzay 2023-03-28 22:07:25 +02:00 committed by Athozus
parent b1ccd494f7
commit 50c1415d36
No known key found for this signature in database
GPG key ID: B50895022E8484BF
2 changed files with 51 additions and 16 deletions

View file

@ -36,11 +36,6 @@ Overview:
* `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)
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

View file

@ -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,20 +44,12 @@ 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")
-- 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