What is the best practice for store migration #10
-
Like rename/add/remove some properties from version to version |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
It's hard to answer this question without specific context, but maybe conceptualizing how Boutique persists its data will help figure out the best approach for you. When you add an item to the
There may be more strategies but it's important to know what exactly you're trying to accomplish before figuring out how to approach the problem, if you can share more about that. |
Beta Was this translation helpful? Give feedback.
-
I've done this extensively on an important project with success. It can work, but you have to plan out a strategy. There are a couple of approaches you might consider. If your data needs to be migrated from older versions (i.e. you can't just blow away the cache and save again with fresh data from the server) then you need some sort of versioning scheme. Option A: Versioning in the modelThis requires you to store the version of a model in the model's Codable data, so you can intelligently reason about the data that is being hydrated from data. Something like: struct Widget: Codable {
let createdAt: Date
enum Version: String {
case v1
case v2 // changed date format from Int seconds to ISO8601
}
init(from decoder: Decoder) throws {
// do it manually, first decoding the version
}
} This approach pretty much requires you implement the decoding logic yourself so you have control over how to parse given different versions. Option 2: Version the filesMaybe you have a I don't love this approach because it sort of pollutes your namespaces with version numbers. Option 3: Migrate the filesThis one is a little tricky, as you need to consider all possible versions going back to the first one. So make sure you do them in order (v1 to v2, v2 to v3, etc). For this you might use Hope this gives you some ideas on how to continue. |
Beta Was this translation helpful? Give feedback.
I've done this extensively on an important project with success. It can work, but you have to plan out a strategy.
There are a couple of approaches you might consider. If your data needs to be migrated from older versions (i.e. you can't just blow away the cache and save again with fresh data from the server) then you need some sort of versioning scheme.
Option A: Versioning in the model
This requires you to store the version of a model in the model's Codable data, so you can intelligently reason about the data that is being hydrated from data.
Something like: