Last active
April 24, 2017 04:01
-
-
Save aashreys/ef0fe9fd1f0db5e974f8525fd69f2bd0 to your computer and use it in GitHub Desktop.
A class for managing application migration across updates
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
/** | |
* A simple class for managing application migration across updates. | |
* | |
* Created by aashreys on 24/04/17. | |
*/ | |
public class Migrator { | |
private static final String KEY_LAST_VERSION = "migrator_key_last_version"; | |
private static final int CURRENT_VERSION = Version.V10; | |
private final KeyValueStore keyValueStore; | |
@Inject | |
public Migrator(KeyValueStore keyValueStore) { | |
this.keyValueStore = keyValueStore; | |
} | |
private int getLastVersion() { | |
return keyValueStore.getInt(KEY_LAST_VERSION, Version.V0); | |
} | |
public void migrate() { | |
int lastVersion = getLastVersion(); | |
switch (lastVersion) { | |
case Version.V0: | |
migrateFromV0ToV10(); | |
// Add more migrations here | |
case CURRENT_VERSION: | |
break; | |
} | |
completeMigration(); | |
} | |
private void completeMigration() { | |
keyValueStore.getInt(KEY_LAST_VERSION, CURRENT_VERSION); | |
} | |
private void migrateFromV0ToV10() { | |
// Migrate stuff here | |
} | |
private interface Version { | |
int V0 = 0; | |
int V10 = 10; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment