Last active
May 29, 2019 16:20
-
-
Save brunobord/5695039 to your computer and use it in GitHub Desktop.
localStorage migration mechanism. please, help me make it better if possible.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -------- 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); | |
} | |
}; | |
} |
@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
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.