如何在 Android 中迁移 Realm
How to migrate Realm in Android
所以最近我陷入了一个巨大的问题,即如何在 Realm 中迁移内容。我终于解决了这个问题,所以我决定在 Whosebug 上 post 它。请注意,Realm 很快就会升级他们的系统,所以答案可能很快就会过时。
代码适用于 Realm 80.3。
这是我使用的代码:
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 希望您提供数据库文件的完整路径。
希望这篇 post 能对某人有所帮助!
以下class可能会帮助您迁移领域数据库表(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++;
}
}
}
所以最近我陷入了一个巨大的问题,即如何在 Realm 中迁移内容。我终于解决了这个问题,所以我决定在 Whosebug 上 post 它。请注意,Realm 很快就会升级他们的系统,所以答案可能很快就会过时。
代码适用于 Realm 80.3。
这是我使用的代码:
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 希望您提供数据库文件的完整路径。
希望这篇 post 能对某人有所帮助!
以下class可能会帮助您迁移领域数据库表(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++;
}
}
}