Skip to content

Commit

Permalink
Fix saved version being reset to latest migration version on server w…
Browse files Browse the repository at this point in the history
…hen saved version is ahead and backwards compatible
  • Loading branch information
nezuo committed Aug 6, 2024
1 parent 3523158 commit 01f3ee3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Collection.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ function Collection:load(key, defaultUserIds)
return "retry", "Could not acquire lock"
end

local savedVersion = value.migrationVersion

local migrationOk, migrated, lastCompatibleVersion = Migration.migrate(self.options.migrations, value)
if not migrationOk then
return "fail", migrated
Expand All @@ -125,7 +127,7 @@ function Collection:load(key, defaultUserIds)
end

local data = {
migrationVersion = #self.options.migrations,
migrationVersion = math.max(savedVersion, #self.options.migrations),
lastCompatibleVersion = lastCompatibleVersion,
lockId = lockId,
data = migrated,
Expand Down
25 changes: 25 additions & 0 deletions src/init.test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,31 @@ return function(x)
-- This shouldn't error because v3 is backwards compatible with v1.
collectionWithV1:load("document"):expect()
end)

x.test("keeps saved version", function(context)
local collection = context.lapis.createCollection("collection", {
defaultData = "a",
})

local dataStore = context.dataStoreService.dataStores.collection.global
dataStore:write("document", {
lastCompatibleVersion = 0,
migrationVersion = 1,
data = "b",
})

local document = collection:load("document"):expect()

assertEqual(context.read("collection", "document").migrationVersion, 1)

document:save("document"):expect()

assertEqual(context.read("collection", "document").migrationVersion, 1)

document:close("document"):expect()

assertEqual(context.read("collection", "document").migrationVersion, 1)
end)
end)
end)

Expand Down

0 comments on commit 01f3ee3

Please sign in to comment.