Skip to content

Commit

Permalink
Merge pull request #60 from nezuo/fix-backwards-compatible-bug
Browse files Browse the repository at this point in the history
Fix saved version being reset to latest migration version on server when saved version is ahead and backwards compatible
  • Loading branch information
nezuo authored Aug 6, 2024
2 parents 3523158 + 4e80246 commit 829e26c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

## Unreleased Changes
* Added `Collection:read` to view a document's data without editing or session locking it. ([#59])
* **IMPORTANT**: Fixed bug that resets a saved migration version that is ahead of the server's version and is backwards compatible. ([#60])

[#59]: https://github.com/nezuo/lapis/pull/59
[#60]: https://github.com/nezuo/lapis/pull/60

## 0.3.1 - July 6, 2024
* Added `Document:keyInfo()`. It returns the last updated `DataStoreKeyInfo` returned from loading, saving, or closing the document. ([#50])
Expand Down
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 829e26c

Please sign in to comment.