Fertogo / Pebble-workout-timer

Easily create Pebble-guided workouts from phone or computer
12 stars 6 forks source link

Add version flag/Persist storage migration #72

Closed Fertogo closed 8 years ago

Fertogo commented 8 years ago

Some flag saved to persistent storage on the Pebble to make data migrations much easier.

Automatic updates will retain persistent storage between app versions. This ensures that your users will not be frustrated by updates silently deleting their data and settings. However, this puts the onus on developers to ensure your app behaves well when presented with an old version of its persistent storage.

We recommend that you do this by versioning your persistent storage, and incrementing the version every time its structure changes.

The easiest way to do this versioning will be to create a new key in your persistent storage containing the app version. If you have not already versioned your storage, you should simply check for the version key and assume its absence to mean "version zero".

Once you know the "storage version" of your app, you can perform some migration to make it match your current format. If the version is too old, or you have multiple "version zero" formats that you cannot otherwise distinguish, you may instead just delete the old keys -

Also, I'll need to make sure to clear all memory if an app was running an older version.

Fertogo commented 8 years ago

It looms from here that the number of totalWorkouts was stored in key 0. If this key exists I should delete all possible keys that could exist for the old version. That should be all keys [0,21] and [337,344]

  //Menu data
    int totalworkouts = atoi(readFromStorage(0)); 
    NUM_FIRST_MENU_ITEMS = totalworkouts; 
    for (int i = 0; i<totalworkouts; i++) { //Populate workout_names array
      char * temp = readFromStorage(i+1); 
      //workout_names[i] =   temp;   // This does not work because of issues with pointers. (Every element becomes the last)
      workout_names[i]= malloc(sizeof(char)*(strlen(temp)+1)); // Save workout titles
      strcpy(workout_names[i],  temp);
    }  

From there I can save the current version in any unused key (except 0!).