Source:
public void modifyPrimaryKeyFieldTypeToIntInMigration() { final String TEMP_FIELD_ID = "temp_id"; buildInitialMigrationSchema(MigrationFieldTypeToInt.CLASS_NAME, false); createObjectsWithOldPrimaryKey(MigrationFieldTypeToInt.CLASS_NAME, true); RealmMigration migration = new RealmMigration() { @Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { realm.getSchema().get(MigrationFieldTypeToInt.CLASS_NAME) .addField("temp_id", int.class) .transform(new StandardRealmObjectSchema.Function() { @Override public void apply(DynamicRealmObject obj) { String fieldValue = obj.getString(MigrationPrimaryKey.FIELD_PRIMARY); if (fieldValue != null && fieldValue.length() != 0) { obj.setInt(TEMP_FIELD_ID, Integer.valueOf(fieldValue).intValue()); } else { obj.deleteFromRealm(); } } }) .removeField(MigrationPrimaryKey.FIELD_PRIMARY) .renameField(TEMP_FIELD_ID, MigrationFieldTypeToInt.FIELD_PRIMARY) .addPrimaryKey(MigrationFieldTypeToInt.FIELD_PRIMARY); } }; RealmConfiguration realmConfig = configFactory.createConfigurationBuilder() .schemaVersion(1) .schema(MigrationFieldTypeToInt.class) .migration(migration) .build(); Realm realm = Realm.getInstance(realmConfig); Table table = realm.getSchema().getTable(MigrationFieldTypeToInt.class); assertTrue(table.hasPrimaryKey()); assertEquals(MigrationFieldTypeToInt.DEFAULT_FIELDS_COUNT, table.getColumnCount()); assertEquals(MigrationFieldTypeToInt.DEFAULT_PRIMARY_INDEX, table.getPrimaryKey()); RealmObjectSchema objectSchema = realm.getSchema().getSchemaForClass(MigrationFieldTypeToInt.class); assertFalse(objectSchema.hasField(MigrationPrimaryKey.FIELD_PRIMARY)); assertEquals(MigrationFieldTypeToInt.FIELD_PRIMARY, objectSchema.getPrimaryKey()); assertEquals(1, realm.where(MigrationFieldTypeToInt.class).count()); assertEquals(12, realm.where(MigrationFieldTypeToInt.class).findFirst().fieldIntPrimary); }

Target:
public void modifyPrimaryKeyFieldTypeToIntInMigration() { final String TEMP_FIELD_ID = "temp_id"; buildInitialMigrationSchema(MigrationFieldTypeToInt.CLASS_NAME, false); createObjectsWithOldPrimaryKey(MigrationFieldTypeToInt.CLASS_NAME, true); RealmMigration migration = new RealmMigration() { @Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { realm.getSchema().get(MigrationFieldTypeToInt.CLASS_NAME) .addField("temp_id", int.class) .transform(new RealmObjectSchema.Function() { @Override public void apply(DynamicRealmObject obj) { String fieldValue = obj.getString(MigrationPrimaryKey.FIELD_PRIMARY); if (fieldValue != null && fieldValue.length() != 0) { obj.setInt(TEMP_FIELD_ID, Integer.valueOf(fieldValue).intValue()); } else { obj.deleteFromRealm(); } } }) .removeField(MigrationPrimaryKey.FIELD_PRIMARY) .renameField(TEMP_FIELD_ID, MigrationFieldTypeToInt.FIELD_PRIMARY) .addPrimaryKey(MigrationFieldTypeToInt.FIELD_PRIMARY); } }; RealmConfiguration realmConfig = configFactory.createConfigurationBuilder() .schemaVersion(1) .schema(MigrationFieldTypeToInt.class) .migration(migration) .build(); Realm realm = Realm.getInstance(realmConfig); Table table = realm.getSchema().getTable(MigrationFieldTypeToInt.class); assertTrue(table.hasPrimaryKey()); assertEquals(MigrationFieldTypeToInt.DEFAULT_FIELDS_COUNT, table.getColumnCount()); assertEquals(MigrationFieldTypeToInt.DEFAULT_PRIMARY_INDEX, table.getPrimaryKey()); RealmObjectSchema objectSchema = realm.getSchema().getSchemaForClass(MigrationFieldTypeToInt.class); assertFalse(objectSchema.hasField(MigrationPrimaryKey.FIELD_PRIMARY)); assertEquals(MigrationFieldTypeToInt.FIELD_PRIMARY, objectSchema.getPrimaryKey()); assertEquals(1, realm.where(MigrationFieldTypeToInt.class).count()); assertEquals(12, realm.where(MigrationFieldTypeToInt.class).findFirst().fieldIntPrimary); }