Set up database version v3 and its migration from v2

+ Check versions to choose v1->v2 or v2->v3
This commit is contained in:
Athozus 2023-03-08 19:03:27 +01:00
parent 41aa75d8fe
commit 0333ba7fe7
No known key found for this signature in database
GPG key ID: B50895022E8484BF
2 changed files with 80 additions and 3 deletions

View file

@ -2,6 +2,9 @@ mail = {
-- api version -- api version
apiversion = 1.1, apiversion = 1.1,
-- database version
dbversion = 3.0,
-- mail directory -- mail directory
maildir = minetest.get_worldpath().."/mails", maildir = minetest.get_worldpath().."/mails",
contactsdir = minetest.get_worldpath().."/mails/contacts" contactsdir = minetest.get_worldpath().."/mails/contacts"

View file

@ -1,7 +1,18 @@
function mail.migrate()
local gen_file_v1 = io.open(minetest.get_worldpath().."/mail.db", "r")
if file then
mail.migrate_v1_to_v2()
end
local info_file_v3 = mail.read_json_file(mail.maildir .. "/mail.info.json")
if not info_file_v3.dbversion then
mail.migrate_v2_to_v3()
end
end
-- migrate from mail.db to player-file-based mailbox -- migrate from mail.db to player-file-based mailbox
function mail.migrate_v1_to_v2()
function mail.migrate()
-- create directory, just in case -- create directory, just in case
minetest.mkdir(mail.maildir) minetest.mkdir(mail.maildir)
minetest.mkdir(mail.contactsdir) minetest.mkdir(mail.contactsdir)
@ -25,8 +36,63 @@ function mail.migrate()
end end
-- migrate from v2 to v3 database
function mail.migrate_v2_to_v3()
minetest.log("info", "[mail] Migration from v2 to v3 database")
minetest.after(0,function()
for playername, value in minetest.get_auth_handler().iterate() do
mail.migrate_contacts_v2_to_v3(playername)
end
end)
mail.migrate_messages_v2_to_v3()
local info_file = mail.read_json_file(mail.maildir .. "/mail.info.json")
mail.write_json_file(mail.maildir .. "/mail.info.json", { dbversion = 3.0 })
end
function mail.migrate_messages_v2_to_v3()
local already_processed = {} -- store messages that are already process to avoid duplicates
minetest.after(0,function()
-- check in every inbox to fetch messages
for playername, value in minetest.get_auth_handler().iterate() do
local saneplayername = string.gsub(playername, "[.|/]", "")
local player_inbox = mail.read_json_file(mail.maildir .. "/" .. saneplayername .. ".json")
for _, msg in ipairs(player_inbox) do
local msg_id = tostring(msg.time) .. msg.sender -- id like "123456789.0singleplayer" -- it presumes that a same sender cannot send two mails within a second
local new_msg = true -- check if that mail was already processed with another player
for _, cur_id in ipairs(already_processed) do
if cur_id == msg_id then
new_msg = false
break
end
end
if new_msg then
local msg_table = {
sender = msg.sender,
to = msg.to,
cc = msg.cc,
subject = msg.subject,
body = msg.body,
time = msg.time,
}
mail.addMessage(msg_table)
table.insert(already_processed, msg_id)
end
end
end
end)
end
function mail.migrate_contacts(playername) function mail.migrate_contacts(playername)
local gen_file_v1 = io.open(minetest.get_worldpath().."/mail.db", "r")
if file then
mail.migrate_contacts_v1_to_v2()
end
-- v2 to v3 directly in general function
end
function mail.migrate_contacts_v1_to_v2(playername)
local file = io.open(mail.getContactsFile(playername), 'r') local file = io.open(mail.getContactsFile(playername), 'r')
if not file then if not file then
-- file doesn't exist! This is a case for Migrate Man! -- file doesn't exist! This is a case for Migrate Man!
@ -48,3 +114,11 @@ function mail.migrate_contacts(playername)
file:close() -- uh, um, nope, let's leave those alone, shall we? file:close() -- uh, um, nope, let's leave those alone, shall we?
end end
end end
function mail.migrate_contacts_v2_to_v3(playername)
local player_contacts = mail.read_json_file(mail.maildir .. "/contacts/" .. playername .. ".json")
for _, c in pairs(player_contacts) do
mail.addContact(playername, { name = c.name, note = c.note })
end
end