Skip to content

Instantly share code, notes, and snippets.

@adavis
Last active April 16, 2017 06:31
Show Gist options
  • Save adavis/ea13d2b264a1a59ec34ddf48d076d13c to your computer and use it in GitHub Desktop.
Save adavis/ea13d2b264a1a59ec34ddf48d076d13c to your computer and use it in GitHub Desktop.
@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 );
}
}
}
}
@Module
public class MigrationsModule
{
@Provides
@IntoMap
@IntKey( 1 )
static VersionMigration provideVersion1Migration ()
{
return new Version1Migration();
}
@Provides
@IntoMap
@IntKey( 2 )
static VersionMigration provideVersion2Migration ()
{
return new Version2Migration();
}
}
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 );
}
}
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 ) );
}
}
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