Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions DataStore2/SavingMethods/MigrateOrderedBackups.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
----------------------------
--[[ NEW FILE BY ROBLOX ]]--
----------------------------

--[[
MigrateOrderedBackups.Lua

Roblox Migration Module:

This script will take DataStore2 data stored with `OrderedBackups` and migrate it to a standard Roblox Data Store.

MigrateOrderedBackups:Get() will try to retrieve migrated data from the migrated Data Store, and if that data does not exist, it will
instead retrieve it from the OrderedBackups Data Stores.

MigrateOrderedBackups:Set() will send data to standard Roblox Data Store named rather than to the OrderedBackups Data Stores.

By simply changing the setting from `OrderedBackups` to `MigrateOrderedBackups` the experience should see a smooth data migration with no extra
action needed by the developer.
--]]


--------------
-- Services --
--------------
local DataStoreServiceRetriever = require(script.Parent.Parent.DataStoreServiceRetriever)
local Promise = require(script.Parent.Parent.Promise)
local Settings = require(script.Parent.Parent.Settings)
local OrderedBackups = require(script.Parent.OrderedBackups)

----------------------
-- Class Definition --
----------------------
local MigrateOrderedBackups = {}
MigrateOrderedBackups.__index = MigrateOrderedBackups

--[[
Gets the data in the DataStore.
This will be data in the migrated data store, if it exists. Otherwise, it
will fallback to retrieving the data from the OrderedBackups Data Stores.
--]]
function MigrateOrderedBackups:Get()

-- try retrieving from Standard Data Store `[dataStoreName]`
return Promise.async(function(resolve)
resolve(self.migratedDataStore:GetAsync(self.dataStore2.UserId))
end):andThen(function(data)
-- return data if it was found
if data ~= nil then
return data
else
-- try retrieving from `OrderedBackups` Data Stores
return self.orderedBackups:Get()
end
end)
end

--[[
Save the data to the DataStore.
This function saves to the migrated data store, unlike the `OrderedBackups`
method.
---------------------------------------------------------------------------

value: the value to save to the data store.
--]]
function MigrateOrderedBackups:Set(value)

-- store to Standard Data Store `[dataStoreName]` with [userId]:[value]
return Promise.async(function(resolve)
self.migratedDataStore:SetAsync(self.dataStore2.UserId, value)
resolve()
end)
end

--[[
Class constructor.
------------------

dataStore2: The DataStore2 instance that utilizes this saving method.
--]]
function MigrateOrderedBackups.new(dataStore2)
local dataStoreService = DataStoreServiceRetriever.Get()
local dataStoreKey = dataStore2.Name .. "/" .. dataStore2.UserId -- name of the original `OrderedBackups` Data Stores
local migratedDataStoreKey = dataStore2.Name
local migratedDataStoreScope = Settings.MigrationDataStoreScope or "global" -- the optionally defined scope -- see `Settings.lua` to set this value

local info = {
dataStore2 = dataStore2,
dataStore = dataStoreService:GetDataStore(dataStoreKey),
orderedDataStore = dataStoreService:GetOrderedDataStore(dataStoreKey),
migratedDataStore = dataStoreService:GetDataStore(migratedDataStoreKey, migratedDataStoreScope),
orderedBackups = OrderedBackups.new(dataStore2)
}

return setmetatable(info, MigrateOrderedBackups)
end

-- export the class definition
return MigrateOrderedBackups
11 changes: 10 additions & 1 deletion DataStore2/SavingMethods/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
----------------------------
--[[ MODIFIED BY ROBLOX ]]--
----------------------------

return {
Standard = require(script.Standard),
OrderedBackups = require(script.OrderedBackups),
}

-------------------------
--[[ ADDED BY ROBLOX ]]--
MigrateOrderedBackups = require(script.MigrateOrderedBackups)
-------------------------
}
14 changes: 13 additions & 1 deletion DataStore2/Settings.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
----------------------------
--[[ MODIFIED BY ROBLOX ]]--
----------------------------

return {
-- What saving method you would like to use
-- Possible options:
-- OrderedBackups: The berezaa method that ensures prevention of data loss
-- Standard: Standard data stores. Equivalent to :GetDataStore(key):GetAsync(UserId)
SavingMethod = "OrderedBackups",
-- [[ ADDED BY ROBLOX ]] MigrateOrderedBackups: The Roblox-endorsed saving method that use Roblox Data Stores more efficiently and compatible with OrderedBackups
SavingMethod = "MigrateOrderedBackups",

-------------------------
--[[ ADDED BY ROBLOX ]]--
MigrationDataStoreScope = "global"
-- Here, you can set the `MigrationDataStoreScope` that will be applied on
-- the migrated data store.
-------------------------
}