Last active
April 16, 2017 06:31
-
-
Save adavis/ea13d2b264a1a59ec34ddf48d076d13c to your computer and use it in GitHub Desktop.
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
@Reusable | |
public class Migration implements RealmMigration | |
{ | |
private Map<Integer, Provider<VersionMigration>> versionMigrations; | |
@Inject | |
Migration (Map<Integer, Provider<VersionMigration>> versionMigrations) | |
{ | |
this.versionMigrations = versionMigrations; | |
} | |
@Override | |
public void migrate (final DynamicRealm realm, long oldVersion, long newVersion) | |
{ | |
for ( int i = ( int ) oldVersion; i < newVersion; i++ ) | |
{ | |
final Provider<VersionMigration> provider = versionMigrations.get( i ); | |
if ( provider != null ) | |
{ | |
VersionMigration versionMigration = provider.get(); | |
versionMigration.migrate( realm, i ); | |
} | |
} | |
} | |
} |
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
@Module | |
public class MigrationsModule | |
{ | |
@Provides | |
@IntoMap | |
@IntKey( 1 ) | |
static VersionMigration provideVersion1Migration () | |
{ | |
return new Version1Migration(); | |
} | |
@Provides | |
@IntoMap | |
@IntKey( 2 ) | |
static VersionMigration provideVersion2Migration () | |
{ | |
return new Version2Migration(); | |
} | |
} |
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
class Version1Migration implements VersionMigration | |
{ | |
private static final String RECIPE = "Recipe"; | |
/************************************************ | |
// Version 2 | |
class Recipe | |
Integer numberOfStars //added | |
************************************************/ | |
@Override | |
public void migrate (DynamicRealm realm, long oldVersion) | |
{ | |
if ( oldVersion == 1 ) | |
{ | |
RealmObjectSchema recipeSchema = getObjectSchema( realm ); | |
recipeSchema.addField( RecipeFields.NUMBER_OF_STARS, Integer.class ) | |
.transform( new RealmObjectSchema.Function() | |
{ | |
@Override | |
public void apply (DynamicRealmObject obj) | |
{ | |
obj.setInt( RecipeFields.NUMBER_OF_STARS, 5 ); | |
} | |
} ); | |
Timber.d( "migration complete" ); | |
} | |
} | |
RealmObjectSchema getObjectSchema (DynamicRealm realm) | |
{ | |
RealmSchema schema = realm.getSchema(); | |
return schema.get( RECIPE ); | |
} | |
} |
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
public class Version1MigrationTest | |
{ | |
@Rule | |
public MockitoRule rule = MockitoJUnit.rule(); | |
@Mock | |
DynamicRealm dynamicRealm; | |
@Mock | |
RealmObjectSchema realmObjectSchema; | |
private Version1Migration migration; | |
@Before | |
public void setUp () | |
{ | |
migration = new Version1Migration() | |
{ | |
@Override | |
RealmObjectSchema getObjectSchema (DynamicRealm realm) | |
{ | |
return realmObjectSchema; | |
} | |
}; | |
} | |
@Test | |
public void shouldNotBeNull () | |
{ | |
assertNotNull( migration ); | |
} | |
@Test | |
public void migrate_shouldDoNothing_whenNotProperOldVersion () | |
{ | |
migration.migrate( dynamicRealm, 2 ); | |
verifyZeroInteractions( dynamicRealm ); | |
} | |
@Test | |
public void migrate_shouldAddField () | |
{ | |
when( realmObjectSchema.addField( anyString(), eq( Integer.class ) ) ).thenReturn( realmObjectSchema ); | |
migration.migrate( dynamicRealm, 1 ); | |
verify( realmObjectSchema ).addField( RecipeFields.NUMBER_OF_STARS, Integer.class ); | |
verify( realmObjectSchema ).transform( any( RealmObjectSchema.Function.class ) ); | |
} | |
} |
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
interface VersionMigration | |
{ | |
void migrate (final DynamicRealm realm, long oldVersion); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment