use of io.realm.RealmMigration in project xabber-android by redsolution.
the class MessageDatabaseManager method createRealmConfiguration.
private RealmConfiguration createRealmConfiguration() {
return new RealmConfiguration.Builder().name(REALM_MESSAGE_DATABASE_NAME).schemaVersion(REALM_MESSAGE_DATABASE_VERSION).modules(new MessageRealmDatabaseModule()).migration(new RealmMigration() {
@Override
public void migrate(DynamicRealm realm1, long oldVersion, long newVersion) {
RealmSchema schema = realm1.getSchema();
if (oldVersion == 1) {
schema.create(SyncInfo.class.getSimpleName()).addField(SyncInfo.FIELD_ACCOUNT, String.class, FieldAttribute.INDEXED).addField(SyncInfo.FIELD_USER, String.class, FieldAttribute.INDEXED).addField(SyncInfo.FIELD_FIRST_MAM_MESSAGE_MAM_ID, String.class).addField(SyncInfo.FIELD_FIRST_MAM_MESSAGE_STANZA_ID, String.class).addField(SyncInfo.FIELD_LAST_MESSAGE_MAM_ID, String.class).addField(SyncInfo.FIELD_REMOTE_HISTORY_COMPLETELY_LOADED, boolean.class);
oldVersion++;
}
if (oldVersion == 2) {
schema.create(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.UNIQUE_ID, String.class, FieldAttribute.PRIMARY_KEY).addField(MessageItem.Fields.ACCOUNT, String.class, FieldAttribute.INDEXED).addField(MessageItem.Fields.USER, String.class, FieldAttribute.INDEXED).addField(MessageItem.Fields.RESOURCE, String.class).addField(MessageItem.Fields.ACTION, String.class).addField(MessageItem.Fields.TEXT, String.class).addField(MessageItem.Fields.TIMESTAMP, Long.class, FieldAttribute.INDEXED).addField(MessageItem.Fields.DELAY_TIMESTAMP, Long.class).addField(MessageItem.Fields.STANZA_ID, String.class).addField(MessageItem.Fields.INCOMING, boolean.class).addField(MessageItem.Fields.UNENCRYPTED, boolean.class).addField(MessageItem.Fields.SENT, boolean.class).addField(MessageItem.Fields.READ, boolean.class).addField(MessageItem.Fields.DELIVERED, boolean.class).addField(MessageItem.Fields.OFFLINE, boolean.class).addField(MessageItem.Fields.ERROR, boolean.class).addField(MessageItem.Fields.IS_RECEIVED_FROM_MAM, boolean.class).addField(MessageItem.Fields.FILE_PATH, String.class).addField(MessageItem.Fields.FILE_SIZE, Long.class);
oldVersion++;
}
if (oldVersion == 3) {
schema.get(MessageItem.class.getSimpleName()).addIndex(MessageItem.Fields.SENT);
oldVersion++;
}
if (oldVersion == 4) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.FORWARDED, boolean.class);
oldVersion++;
}
if (oldVersion == 5) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.ACKNOWLEDGED, boolean.class);
oldVersion++;
}
if (oldVersion == 6) {
schema.create("LogMessage").addField("level", int.class).addField("tag", Date.class).addField("message", String.class).addField("datetime", String.class);
oldVersion++;
}
if (oldVersion == 7) {
schema.remove("LogMessage");
oldVersion++;
}
if (oldVersion == 8) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.FILE_URL, String.class);
oldVersion++;
}
if (oldVersion == 9) {
schema.remove("BlockedContactsForAccount");
schema.remove("BlockedContact");
oldVersion++;
}
if (oldVersion == 10) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.IS_IN_PROGRESS, boolean.class);
oldVersion++;
}
if (oldVersion == 11) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.IS_IMAGE, boolean.class);
oldVersion++;
}
if (oldVersion == 12) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.IMAGE_WIDTH, Integer.class).addField(MessageItem.Fields.IMAGE_HEIGHT, Integer.class);
oldVersion++;
}
if (oldVersion == 13) {
schema.get(MessageItem.class.getSimpleName()).removeField(MessageItem.Fields.UNENCRYPTED).addField(MessageItem.Fields.ENCRYPTED, boolean.class);
oldVersion++;
}
if (oldVersion == 14) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.ERROR_DESCR, String.class);
oldVersion++;
}
if (oldVersion == 15) {
schema.create(Attachment.class.getSimpleName()).addField("uniqueId", String.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED).addField("title", String.class).addField("filePath", String.class).addField("fileUrl", String.class).addField("fileSize", Long.class).addField("isImage", boolean.class).addField("imageWidth", Integer.class).addField("imageHeight", Integer.class).addField("duration", Long.class).addField("mimeType", String.class);
schema.get(MessageItem.class.getSimpleName()).addRealmListField(MessageItem.Fields.ATTACHMENTS, schema.get(Attachment.class.getSimpleName()));
oldVersion++;
}
if (oldVersion == 16) {
schema.create(ForwardId.class.getSimpleName()).addField("id", String.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED).addField("forwardMessageId", String.class);
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.ORIGINAL_STANZA, String.class).addField(MessageItem.Fields.ORIGINAL_FROM, String.class).addField(MessageItem.Fields.PARENT_MESSAGE_ID, String.class).addField(MessageItem.Fields.FROM_MUC, boolean.class).addRealmListField(MessageItem.Fields.FORWARDED_IDS, schema.get(ForwardId.class.getSimpleName()));
oldVersion++;
}
if (oldVersion == 17) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.DISPLAYED, boolean.class);
oldVersion++;
}
if (oldVersion == 18) {
schema.get(MessageItem.class.getSimpleName()).transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setBoolean(MessageItem.Fields.READ, true);
}
});
oldVersion++;
}
if (oldVersion == 19) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.PREVIOUS_ID, String.class).addField(MessageItem.Fields.ARCHIVED_ID, String.class).transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.setString(MessageItem.Fields.PREVIOUS_ID, "legacy");
}
});
oldVersion++;
}
if (oldVersion == 20) {
schema.create(ContactGroup.class.getSimpleName()).addField(ContactGroup.Fields.GROUP_NAME, String.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED);
schema.create(ContactRealm.class.getSimpleName()).addField(ContactRealm.Fields.ID, String.class, FieldAttribute.PRIMARY_KEY, FieldAttribute.REQUIRED).addField(ContactRealm.Fields.ACCOUNT, String.class).addField(ContactRealm.Fields.USER, String.class).addField(ContactRealm.Fields.NAME, String.class).addField(ContactRealm.Fields.ACCOUNT_RESOURCE, String.class).addRealmObjectField(ContactRealm.Fields.LAST_MESSAGE, schema.get(MessageItem.class.getSimpleName())).addRealmListField(ContactRealm.Fields.GROUPS, schema.get(ContactGroup.class.getSimpleName()));
oldVersion++;
}
if (oldVersion == 21) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.MARKUP_TEXT, String.class);
oldVersion++;
}
if (oldVersion == 22) {
schema.get(MessageItem.class.getSimpleName()).addField(MessageItem.Fields.GROUPCHAT_USER_ID, String.class);
oldVersion++;
}
}
}).build();
}
use of io.realm.RealmMigration in project CustomViews by AndroidStudy233.
the class RealmManager method init.
/**
* 初始化realm,比如说如果你的app在不同的账号登录同一台设备就要需要建立不同的数据库(根据名称来区分)
*
* @param realmName 数据库名称
* @param version 版本号
*/
private static void init(String realmName, int version) {
RealmConfiguration configuration = new RealmConfiguration.Builder().name(// 配置名字
realmName).encryptionKey(// 加密用字段,不是64位会报错
new byte[64]).schemaVersion(// 版本号
version).migration(new RealmMigration() {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
MyLogUtil.debug("oldVersion: " + oldVersion);
MyLogUtil.debug("newVersion: " + newVersion);
RealmObjectSchema schema = realm.getSchema().get("Doctor");
Set<String> names = schema.getFieldNames();
if (names.contains("id"))
schema.removeField("id");
if (names.contains("name") && !schema.getPrimaryKey().equals("name")) {
schema.removePrimaryKey();
schema.addPrimaryKey("name");
}
}
}).build();
Realm.setDefaultConfiguration(configuration);
// Context.getFilesDir() 目录下的realmName.realm数据库
}
Aggregations