add v1 and v2 player db examples and migration test

This commit is contained in:
BuckarooBanzay 2023-03-29 08:24:56 +02:00 committed by Athozus
parent a5caecf96e
commit 48fa4b04f8
No known key found for this signature in database
GPG key ID: B50895022E8484BF
7 changed files with 69 additions and 31 deletions

View file

@ -58,6 +58,7 @@ mail.migrate()
if minetest.get_modpath("mtt") then
dofile(MP .. "/mtt.lua")
dofile(MP .. "/api.spec.lua")
dofile(MP .. "/migrate.spec.lua")
dofile(MP .. "/util/uuid.spec.lua")
dofile(MP .. "/util/normalize.spec.lua")
end

View file

@ -11,26 +11,24 @@ local function migrate_v1_to_v3()
file:close()
for name, oldmessages in pairs(oldmails) do
print("[mail,v1] + migrating player '" .. name .. "'")
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,
to = msg.to or name,
subject = msg.subject,
body = msg.body,
time = msg.time,
time = msg.time or os.time(),
})
end
end
mail.set_storage_entry(name, entry)
end
-- 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")
mail.storage:set_int(STORAGE_VERSION_KEY, 3)
end
local function read_json_file(path)
@ -62,37 +60,39 @@ local function migrate_v2_to_v3()
local saneplayername = string.gsub(playername, "[.|/]", "")
local player_inbox = read_json_file(maildir .. "/" .. saneplayername .. ".json")
print("[mail,v2] + migrating player '" .. playername .. "'")
for _, msg in ipairs(player_inbox) do
if msg.to then
table.insert(entry.inbox, {
id = mail.new_uuid(),
from = msg.sender or msg.from,
to = msg.to,
to = msg.to or playername,
cc = msg.cc,
subject = msg.subject,
body = msg.body,
time = msg.time,
time = msg.time or os.time(),
})
end
end
mail.set_storage_entry(playername, entry)
end
print("[mail] migration done")
mail.storage:set_int(STORAGE_VERSION_KEY, 3)
print("[mail,v2] migration done")
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
-- check for v2 storage first, v1-migration might have set the v3-flag already
local version = mail.storage:get_int(STORAGE_VERSION_KEY)
if version < 3 then
-- 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

28
migrate.spec.lua Normal file
View 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)

View file

@ -1,6 +1,13 @@
ARG ENGINE_VERSION=5.5.0
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
RUN apk add git &&\
mkdir -p /root/.minetest/worlds/world/worldmods/ &&\

BIN
test/auth.sqlite Normal file

Binary file not shown.

1
test/mail.db Normal file
View 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
View file

@ -0,0 +1 @@
[{"body":"test2","sender":"someone-else","subject":"test1","time":1678467148,"unread":false}]