mirror of
https://github.com/mt-mods/mail.git
synced 2025-07-13 18:01:54 -04:00
add v1 and v2 player db examples and migration test
This commit is contained in:
parent
a5caecf96e
commit
48fa4b04f8
7 changed files with 69 additions and 31 deletions
1
init.lua
1
init.lua
|
@ -58,6 +58,7 @@ mail.migrate()
|
||||||
if minetest.get_modpath("mtt") then
|
if minetest.get_modpath("mtt") then
|
||||||
dofile(MP .. "/mtt.lua")
|
dofile(MP .. "/mtt.lua")
|
||||||
dofile(MP .. "/api.spec.lua")
|
dofile(MP .. "/api.spec.lua")
|
||||||
|
dofile(MP .. "/migrate.spec.lua")
|
||||||
dofile(MP .. "/util/uuid.spec.lua")
|
dofile(MP .. "/util/uuid.spec.lua")
|
||||||
dofile(MP .. "/util/normalize.spec.lua")
|
dofile(MP .. "/util/normalize.spec.lua")
|
||||||
end
|
end
|
||||||
|
|
62
migrate.lua
62
migrate.lua
|
@ -11,26 +11,24 @@ local function migrate_v1_to_v3()
|
||||||
file:close()
|
file:close()
|
||||||
|
|
||||||
for name, oldmessages in pairs(oldmails) do
|
for name, oldmessages in pairs(oldmails) do
|
||||||
|
print("[mail,v1] + migrating player '" .. name .. "'")
|
||||||
local entry = mail.get_storage_entry(name)
|
local entry = mail.get_storage_entry(name)
|
||||||
for _, msg in ipairs(oldmessages) do
|
for _, msg in ipairs(oldmessages) do
|
||||||
if msg.to then
|
table.insert(entry.inbox, {
|
||||||
table.insert(entry.inbox, {
|
id = mail.new_uuid(),
|
||||||
id = mail.new_uuid(),
|
from = msg.sender or msg.from,
|
||||||
from = msg.sender or msg.from,
|
to = msg.to or name,
|
||||||
to = msg.to,
|
subject = msg.subject,
|
||||||
subject = msg.subject,
|
body = msg.body,
|
||||||
body = msg.body,
|
time = msg.time or os.time(),
|
||||||
time = msg.time,
|
})
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
mail.set_storage_entry(name, entry)
|
mail.set_storage_entry(name, entry)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- rename file
|
-- rename file
|
||||||
print("[mail] migration done, renaming old mail.db")
|
print("[mail,v1] migration done, renaming old mail.db")
|
||||||
os.rename(minetest.get_worldpath().."/mail.db", minetest.get_worldpath().."/mail.db.old")
|
os.rename(minetest.get_worldpath().."/mail.db", minetest.get_worldpath().."/mail.db.old")
|
||||||
mail.storage:set_int(STORAGE_VERSION_KEY, 3)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function read_json_file(path)
|
local function read_json_file(path)
|
||||||
|
@ -62,37 +60,39 @@ local function migrate_v2_to_v3()
|
||||||
|
|
||||||
local saneplayername = string.gsub(playername, "[.|/]", "")
|
local saneplayername = string.gsub(playername, "[.|/]", "")
|
||||||
local player_inbox = read_json_file(maildir .. "/" .. saneplayername .. ".json")
|
local player_inbox = read_json_file(maildir .. "/" .. saneplayername .. ".json")
|
||||||
|
print("[mail,v2] + migrating player '" .. playername .. "'")
|
||||||
for _, msg in ipairs(player_inbox) do
|
for _, msg in ipairs(player_inbox) do
|
||||||
if msg.to then
|
table.insert(entry.inbox, {
|
||||||
table.insert(entry.inbox, {
|
id = mail.new_uuid(),
|
||||||
id = mail.new_uuid(),
|
from = msg.sender or msg.from,
|
||||||
from = msg.sender or msg.from,
|
to = msg.to or playername,
|
||||||
to = msg.to,
|
cc = msg.cc,
|
||||||
cc = msg.cc,
|
subject = msg.subject,
|
||||||
subject = msg.subject,
|
body = msg.body,
|
||||||
body = msg.body,
|
time = msg.time or os.time(),
|
||||||
time = msg.time,
|
})
|
||||||
})
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
mail.set_storage_entry(playername, entry)
|
mail.set_storage_entry(playername, entry)
|
||||||
end
|
end
|
||||||
print("[mail] migration done")
|
print("[mail,v2] migration done")
|
||||||
mail.storage:set_int(STORAGE_VERSION_KEY, 3)
|
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function mail.migrate()
|
function mail.migrate()
|
||||||
local v1_file = io.open(minetest.get_worldpath().."/mail.db", "r")
|
-- check for v2 storage first, v1-migration might have set the v3-flag already
|
||||||
if v1_file then
|
|
||||||
-- v1 to v3
|
|
||||||
migrate_v1_to_v3()
|
|
||||||
end
|
|
||||||
|
|
||||||
local version = mail.storage:get_int(STORAGE_VERSION_KEY)
|
local version = mail.storage:get_int(STORAGE_VERSION_KEY)
|
||||||
if version < 3 then
|
if version < 3 then
|
||||||
-- v2 to v3
|
-- v2 to v3
|
||||||
migrate_v2_to_v3()
|
migrate_v2_to_v3()
|
||||||
|
mail.storage:set_int(STORAGE_VERSION_KEY, 3)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- check for v1 storage
|
||||||
|
local v1_file = io.open(minetest.get_worldpath().."/mail.db", "r")
|
||||||
|
if v1_file then
|
||||||
|
-- v1 to v3
|
||||||
|
migrate_v1_to_v3()
|
||||||
|
mail.storage:set_int(STORAGE_VERSION_KEY, 3)
|
||||||
end
|
end
|
||||||
end
|
end
|
28
migrate.spec.lua
Normal file
28
migrate.spec.lua
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
|
||||||
|
mtt.register("migrate v1", function(callback)
|
||||||
|
local entry = mail.get_storage_entry("old_v1_player")
|
||||||
|
assert(entry)
|
||||||
|
assert(#entry.inbox == 1)
|
||||||
|
assert(entry.inbox[1].from == "singleplayer")
|
||||||
|
assert(entry.inbox[1].to == "old_v1_player")
|
||||||
|
assert(entry.inbox[1].subject == "test1")
|
||||||
|
assert(entry.inbox[1].body == "test2")
|
||||||
|
assert(entry.inbox[1].id)
|
||||||
|
assert(entry.inbox[1].time > 0)
|
||||||
|
|
||||||
|
callback()
|
||||||
|
end)
|
||||||
|
|
||||||
|
mtt.register("migrate v2", function(callback)
|
||||||
|
local entry = mail.get_storage_entry("old_v2_player")
|
||||||
|
assert(entry)
|
||||||
|
assert(#entry.inbox == 1)
|
||||||
|
assert(entry.inbox[1].from == "someone-else")
|
||||||
|
assert(entry.inbox[1].to == "old_v2_player")
|
||||||
|
assert(entry.inbox[1].subject == "test1")
|
||||||
|
assert(entry.inbox[1].body == "test2")
|
||||||
|
assert(entry.inbox[1].id)
|
||||||
|
assert(entry.inbox[1].time == 1678467148)
|
||||||
|
|
||||||
|
callback()
|
||||||
|
end)
|
|
@ -1,6 +1,13 @@
|
||||||
ARG ENGINE_VERSION=5.5.0
|
ARG ENGINE_VERSION=5.5.0
|
||||||
FROM registry.gitlab.com/minetest/minetest/server:${ENGINE_VERSION}
|
FROM registry.gitlab.com/minetest/minetest/server:${ENGINE_VERSION}
|
||||||
|
|
||||||
|
# copy old v1 maildb for migration testing
|
||||||
|
COPY ./mail.db /root/.minetest/worlds/world/mail.db
|
||||||
|
# copy old v2 mail-dir and auth.sqlite for migration testing
|
||||||
|
COPY ./old_v2_player.json /root/.minetest/worlds/world/mails/
|
||||||
|
COPY ./auth.sqlite /root/.minetest/worlds/world/auth.sqlite
|
||||||
|
|
||||||
|
|
||||||
USER root
|
USER root
|
||||||
RUN apk add git &&\
|
RUN apk add git &&\
|
||||||
mkdir -p /root/.minetest/worlds/world/worldmods/ &&\
|
mkdir -p /root/.minetest/worlds/world/worldmods/ &&\
|
||||||
|
|
BIN
test/auth.sqlite
Normal file
BIN
test/auth.sqlite
Normal file
Binary file not shown.
1
test/mail.db
Normal file
1
test/mail.db
Normal file
|
@ -0,0 +1 @@
|
||||||
|
local _={};_[1]="singleplayer";return {old_v1_player={{unread=true,subject="test1",sender=_[1],body="test2"}},[_[1]]={}}
|
1
test/old_v2_player.json
Normal file
1
test/old_v2_player.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
[{"body":"test2","sender":"someone-else","subject":"test1","time":1678467148,"unread":false}]
|
Loading…
Add table
Add a link
Reference in a new issue