1

所以最近我遇到了一个如何在 Realm 中迁移东西的大问题。我终于解决了这个问题,所以我决定将它发布在stackoverflow上。请注意,Realm 很快就会升级他们的系统,所以答案可能很快就会过时。

该代码适用于 Realm 80.3。

4

2 回答 2

2

这是我使用的代码:

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;
        }
    });
}

让我感动的是,Realm 希望您提供数据库文件的完整路径。

希望这篇文章对某人有所帮助!

于 2015-06-16T01:18:57.340 回答
0

以下课程可以帮助您迁移领域数据库表(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++;
        }
    }
}
于 2016-09-16T07:35:05.500 回答