How to deal with data structure changes when it comes to Datastore?

How do you deal with data structures changing in datastores?

For example, imagine I have the following:

session = {
	health = 100,
	experience = 1999,
	Inventory = {
		Glove = 1,
		Bat = 2,		
	}
}

after a big update on the Test Place, I now have the following:

session = {
	health = 100,
	experience = 1999,
	rating = 5, -- NEW
	Inventory = {
		glove = 1,
		bat = 2,		
	},
	Rewards = { -NEW
		streak = true, -- NEW
		record = false,	 -- NEW
	}
}

I have added session.rating, session.Rewards.Streak and session.Rewards.Record.

How can I update ALL the existing players so they now have this new data structure?

I’m aware there are different ways of doing this, I’m curious to see what is suggested.

One way I would not like to do it, is by adding an IF-ELSE to check if Player already has this structure and then add it. I’m hoping for a more sophisticated way (scalable) of doing it.

Appreciate your feedback!

Something I have done - although this has been for projects during testing - is add a function to transform the data into the new structure. This will take effect when players join, which means you don’t update “dead” accounts. However, this may be problematic if you change your structure multiple times. Then you’d need to call functions to change it from your different formats (ex. a->b->c) which quickly becomes tedious and in-effective, since you would have to support every format you have ever had.

2 Likes

@ifkpop , exactly, that’s what I’m afraid of. That can come to bite you really hard in the future.

That is the only real way to do it. Add in a few extra for expansion also maybe.