How to migrate Realm in Android

3.8k views Asked by At

So recently I got into a giant issue of how to migrate stuff in Realm. I finally solved the problem, so I decided to post it on stackoverflow. Please note that Realm is upgrading their system very soon, so the answer will probably soon be outdated.

The code is for Realm 80.3.

2

There are 2 answers

4
Nathan On BEST ANSWER

This is the code I used:

try {
    realm = Realm.getInstance(context, "database.realm");
    Log.i("Database2", context.getFilesDir().getPath());
} catch (RealmMigrationNeededException ex) {
    Realm.migrateRealmAtPath(context.getFileStreamPath("database.realm").getPath(), new RealmMigration() {
        @Override
        public long execute(Realm realm, long version) {
            Log.i("Database", "Yay!");
            // Migrate stuff...
            Table table = realm.getTable(RealmTestObjectTwo.class);
            table.addColumn(ColumnType.INTEGER, "key");
            table.addColumn(ColumnType.STRING, "value");
            table.setPrimaryKey("key");
            version += 1;
            // End Migrate Stuff...
            return version;
        }
    });
}

The thing that got me was that Realm expects you to provide the full path to the database file.

Hopefully this post will help someone!

0
Dashrath Rathod On

Following class may help you to migrate realm database tables (0.86.0+)

public class Migration
        implements RealmMigration {
    @Override
    public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
        Log.e("TAG", oldVersion + "-" + newVersion);
        RealmSchema realmSchema = realm.getSchema();
        if(oldVersion == 0) {
            realmSchema.get("FestivalModel").addField("timestamp", Long.class, FieldAttribute.REQUIRED);
            oldVersion++;
        }
        if(oldVersion == 1) {
            //add new fields - match_image,lineup_image,festival_end_date,isMatchedFestival
            RealmObjectSchema festivalscema = realmSchema.get("FestivalModel");

            List<String> list = new ArrayList<>(festivalscema.getFieldNames());
            Log.e("Fields", list.toString());
            festivalscema
                    .addField("isMatchedFestival", Integer.class, FieldAttribute.REQUIRED)
                    .addField("match_image", String.class)
                    .addField("lineup_image", String.class)
                    .addField("festival_end_date", String.class)

                    .transform(new RealmObjectSchema.Function() {
                        @Override
                        public void apply(DynamicRealmObject obj) {
                            //obj.set("isMatchedFestival", "1");
                            DynamicRealmObject festivalModel = realm.createObject("FestivalModel");
                            festivalModel.setInt("name", 1);

                        }
                    });

            // Create a new class - ArtistFestivalModel
            RealmObjectSchema artistFestivalModelSchema = realmSchema.create("ArtistFestivalModel")
                    .addField("id", String.class, FieldAttribute.PRIMARY_KEY)
                    .addField("name", String.class)
                    .addField("description", String.class)
                    .addField("profile_image", String.class)
                    .addField("match_image", String.class)
                    .addField("lineup_image", String.class)
                    .addField("festival_date", String.class)
                    .addField("festival_end_date", String.class)
                    .addField("official_url", String.class)
                    .addField("address", String.class)
                    .addField("latitude", Double.class, FieldAttribute.REQUIRED)
                    .addField("longitude", Double.class, FieldAttribute.REQUIRED)
                    .addField("created_date", String.class)
                    .addField("modified_date", String.class)
                    .addField("is_deleted", String.class)
                    .addField("timestamp", Long.class, FieldAttribute.REQUIRED);

            // Create a new class - ArtistFestivalModel
            RealmObjectSchema festivalLineupSchema = realmSchema.create("FestivalLineup")
                    .addField("id", String.class, FieldAttribute.PRIMARY_KEY)
                    .addField("name", String.class)
                    .addField("profile_image", String.class)
                    .addField("official_url", String.class)
                    .addField("created_date", String.class)
                    .addField("modified_date", String.class)
                    .addField("is_deleted", String.class)
                    .addField("lineup", String.class)
                    .addField("FestivalId", String.class)
                    .addField("is_liked", Boolean.class, FieldAttribute.REQUIRED);

            /** 1st uploaded version with 0
             *  2nd uploaded version with 1
             *  3rd uploaded version with 19
             */
            oldVersion = 19;
        }
        if(oldVersion == 20) {
            //add new fields - match_image,lineup_image,festival_end_date,isMatchedFestival
            RealmObjectSchema festivalscema = realmSchema.get("FestivalModel");
            List<String> list = new ArrayList<>(festivalscema.getFieldNames());
            Log.e("Fields", list.toString());
            festivalscema
                    .addField("isMatchedFestival", Integer.class, FieldAttribute.REQUIRED)
                    .addField("match_image", String.class)
                    .addField("lineup_image", String.class)
                    .addField("festival_end_date", String.class)
                    .transform(new RealmObjectSchema.Function() {
                        @Override
                        public void apply(DynamicRealmObject obj) {
                            //obj.set("isMatchedFestival", "1");
                            DynamicRealmObject festivalModel = realm.createObject("FestivalModel");
                            festivalModel.setInt("name", 1);
                        }
                    });

            // Create a new class - ArtistFestivalModel

            RealmObjectSchema artistFestivalModelSchema = realmSchema.create("ArtistFestivalModel")
                    .addField("id", String.class, FieldAttribute.PRIMARY_KEY)
                    .addField("name", String.class)
                    .addField("description", String.class)
                    .addField("profile_image", String.class)
                    .addField("match_image", String.class)
                    .addField("lineup_image", String.class)
                    .addField("festival_date", String.class)
                    .addField("festival_end_date", String.class)
                    .addField("official_url", String.class)
                    .addField("address", String.class)
                    .addField("latitude", Double.class, FieldAttribute.REQUIRED)
                    .addField("longitude", Double.class, FieldAttribute.REQUIRED)
                    .addField("created_date", String.class)
                    .addField("modified_date", String.class)
                    .addField("is_deleted", String.class)
                    .addField("timestamp", Long.class, FieldAttribute.REQUIRED);

            // Create a new class - ArtistFestivalModel

            RealmObjectSchema festivalLineupSchema = realmSchema.create("FestivalLineup")
                    .addField("id", String.class, FieldAttribute.PRIMARY_KEY)
                    .addField("name", String.class)
                    .addField("profile_image", String.class)
                    .addField("official_url", String.class)
                    .addField("created_date", String.class)
                    .addField("modified_date", String.class)
                    .addField("is_deleted", String.class)
                    .addField("lineup", String.class)
                    .addField("FestivalId", String.class)
                    .addField("is_liked", Boolean.class, FieldAttribute.REQUIRED);

            /** 1st uploaded version with 0
             *  2nd uploaded version with 1
             *  3rd uploaded version with 19
             */
            oldVersion++;
        }
    }
}