Skip to content

Instantly share code, notes, and snippets.

@brunobord
Last active May 29, 2019 16:20
Show Gist options
  • Save brunobord/5695039 to your computer and use it in GitHub Desktop.
Save brunobord/5695039 to your computer and use it in GitHub Desktop.
localStorage migration mechanism. please, help me make it better if possible.
// -------- Database section
// current version
var __version__ = '1.0.0';
var __versions__ = {
'1.0.1': migrate_1_0_1,
'1.0.2': migrate_1_0_2,
'1.0.3': migrate_1_0_3,
}
function migrate_1_0_1() {
console.log('going into migrate_1_0_1');
}
function migrate_1_0_2() {
console.log('going into migrate_1_0_2');
}
function migrate_1_0_3() {
console.log('going into migrate_1_0_3');
}
function migrate() {
if (!localStorage.getItem('db:version')) {
localStorage.setItem('db:version', '1.0.0');
}
for (var version in __versions__) {
var current_version = localStorage.getItem('db:version');
if (version > current_version) {
var callback = __versions__[version];
callback();
localStorage.setItem('db:version', version);
}
};
}
@piecioshka
Copy link

At first wrap with enviroument :) use strict mode :)

@brunobord
Copy link
Author

mmm... I was more thinking about functional advices, rather than formal ones. But thx for the suggestion, I may try strict mode whenever I can.

@richardscarrott
Copy link

@brunobord I know this is old but just in case anybody else stumbles across this, there is an issue with the version comparison here because '1.0.11' > '1.0.2' // false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment