Couldn’t find a channel for androidx Datastore, so...
# android
s
Couldn’t find a channel for androidx Datastore, so trying out here. Wanted to ask about if anyone has experience with performing Datastore migrations and a concern I had regarding them.
😶 4
Currently, the documentation page does not contain any information or examples regarding doing Datastore version updates, basically doing some breaking change in the storage structure and needing to do a migration for it. Looking at the code, I had a concern where when providing a list of migrations, they seem to happen one by one sequentially while the order in which they happen is that first all
migrate
calls are run, and then all
cleanup
calls are run. Doesn’t this mean that one migration assumes that it will do the two actions sequentially? Like changing some keys, then cleaning them up directly. Since the order isn’t like that, if later a new migration is added to that list which assumes those old keys are cleaned up, it can potentially get conflicts by using the same keys, as when that second migration is running the old one will not have called the cleanup function yet.
r
This doesn’t answer your question but it is related. I was not able to convince myself how the migration worked when I was reading about this so I ended up rolling my own solution (which is part of an app in development but which I have tested on breaking changes I make as I develop, and seems to work well). Instead of making a DataStore of the class I want to write I made a DataStore of String, each String of which is a JSON of the object of the desired class but with a version number pre-pended. I read the string, strip the version number, and deserialize the JSON to the proper version of the class (old versions of which have to be saved). This applies to Proto DataStore and not Preferences DataStore, and it uses Kotlin serialization as outline here: https://medium.com/androiddevelopers/datastore-and-kotlin-serialization-8b25bf0be66c I’m sure this is more complicated than it needs to be but it was one way I could convince myself that I would ship an app that could migrate properly.
🙏 1
s
Thanks for sharing! Damn this does certainly feel like so much more work than what I’d like it to be. appending and stripping version numbers like that sounds way more complicated than what I’d like to do. At that point might as well go with Room/SQLDelight to get proper built-in migration support. I think the migration API itself looks fine, it’s just that I have the concerns which I mentioned before about how the migration itself happens.
r
Yeah, I know it’s not ideal, especially in terms of reinventing the wheel. I had already written the DataStore code and went from there…
🙏 1