use of io.realm.RealmSchema in project open-event-android by fossasia.
the class RealmDatabaseMigration method migrate.
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
// DynamicRealm exposes an editable schema
RealmSchema schema = realm.getSchema();
if (oldVersion == 0) {
schema.get("Session").addField("created-at", String.class).addField("deleted-at", String.class).addField("submitted-at", String.class).addField("is-mail-sent", Boolean.class);
oldVersion++;
}
if (oldVersion == 1) {
schema.get("Event").addField("created-at", String.class).addField("deleted-at", String.class).addField("icon-image-url", String.class);
oldVersion++;
}
}
use of io.realm.RealmSchema in project toshi-android-client by toshiapp.
the class DbMigration method migrate.
@Override
public void migrate(final DynamicRealm realm, long oldVersion, final long newVersion) {
final RealmSchema schema = realm.getSchema();
// Add a new field on User.
if (oldVersion == 0) {
schema.get("User").addField("cacheTimestamp", long.class);
oldVersion++;
}
// Add PendingMessage table
if (oldVersion == 1) {
schema.create("PendingMessage").addField("privateKey", String.class, FieldAttribute.PRIMARY_KEY).addRealmObjectField("receiver", schema.get("User")).addRealmObjectField("sofaMessage", schema.get("SofaMessage"));
oldVersion++;
}
// Add attachmentFilename to SofaMessage table
if (oldVersion == 2) {
schema.get("SofaMessage").addField("attachmentFilename", String.class);
oldVersion++;
}
// Add support for reputation to User.
if (oldVersion == 3) {
schema.get("User").addField("reputation_score", Double.class).addField("review_count", int.class);
oldVersion++;
}
// Move the custom data to top level on a User
if (oldVersion == 4) {
final RealmObjectSchema userSchema = schema.get("User");
userSchema.addField("about", String.class).addField("avatar", String.class).addField("location", String.class).addField("name", String.class).transform(obj -> {
final DynamicRealmObject customUserInfo = obj.getObject("customUserInfo");
obj.set("about", customUserInfo.getString("about"));
obj.set("avatar", customUserInfo.getString("avatar"));
obj.set("location", customUserInfo.getString("location"));
obj.set("name", customUserInfo.getString("name"));
}).removeField("customUserInfo");
oldVersion++;
}
// Rename attachmentFilename to attachmentFilePath
if (oldVersion == 5) {
final RealmObjectSchema sofaMessageSchema = schema.get("SofaMessage");
sofaMessageSchema.renameField("attachmentFilename", "attachmentFilePath");
oldVersion++;
}
// Migration is too much effort considering we're yet to go live.
if (oldVersion == 6) {
final RealmObjectSchema sofaMessageSchema = schema.get("SofaMessage");
sofaMessageSchema.removeField("sentByLocal").addRealmObjectField("sender", schema.get("User"));
oldVersion++;
}
// Encrypt and shard
if (oldVersion == 7) {
realm.writeEncryptedCopyTo(new File(realm.getPath(), this.wallet.getOwnerAddress()), this.wallet.generateDatabaseEncryptionKey());
oldVersion++;
}
if (oldVersion == 8) {
if (schema.contains("BlockedUser")) {
schema.remove("BlockedUser");
}
final RealmObjectSchema blockedUserSchema = schema.create("BlockedUser");
blockedUserSchema.addField("owner_address", String.class, FieldAttribute.PRIMARY_KEY);
oldVersion++;
}
if (oldVersion == 9) {
final RealmObjectSchema userSchema = schema.get("User");
if (userSchema.hasField("customAppInfo")) {
userSchema.removeField("customAppInfo");
}
if (!userSchema.hasField("is_app")) {
userSchema.addField("is_app", boolean.class);
}
if (schema.contains("CustomAppInformation")) {
schema.remove("CustomAppInformation");
}
oldVersion++;
}
// Rename "Conversation" to "ContactThread"
if (oldVersion == 10) {
final RealmObjectSchema conversationSchema = schema.get("Conversation");
if (conversationSchema != null) {
conversationSchema.renameField("conversationId", "threadId");
schema.rename("Conversation", "ContactThread");
}
oldVersion++;
}
// Split out recipient into seperate table
if (oldVersion == 11) {
if (schema.get("Group") == null) {
schema.create("Group").addField("id", String.class, FieldAttribute.PRIMARY_KEY).addField("title", String.class).addRealmListField("members", schema.get("User"));
}
if (schema.get("Recipient") == null) {
schema.create("Recipient").addField("id", String.class, FieldAttribute.PRIMARY_KEY).addRealmObjectField("user", schema.get("User")).addRealmObjectField("group", schema.get("Group"));
}
schema.rename("ContactThread", "Conversation").addRealmObjectField("recipient", schema.get("Recipient")).transform(obj -> {
final String primaryKey = UUID.randomUUID().toString();
final DynamicRealmObject newRecipient = realm.createObject("Recipient", primaryKey);
newRecipient.setObject("user", obj.getObject("member"));
obj.set("recipient", newRecipient);
}).removeField("member");
oldVersion++;
}
if (oldVersion == 12) {
final RealmObjectSchema userSchema = schema.get("User");
if (!userSchema.hasField("is_public")) {
userSchema.addField("is_public", boolean.class);
}
oldVersion++;
}
// Port Recipient ids to be threadIds
if (oldVersion == 13) {
schema.get("Conversation").transform(obj -> {
// Get existing recipient object
final DynamicRealmObject recipient = obj.getObject("recipient");
final String threadId = obj.getString("threadId");
final DynamicRealmObject user = recipient.getObject("user");
final DynamicRealmObject group = recipient.getObject("group");
// Create new object with correct PK
final DynamicRealmObject newRecipient = realm.createObject("Recipient", threadId);
newRecipient.setObject("user", user);
newRecipient.setObject("group", group);
// Delete old recipient
recipient.deleteFromRealm();
// Set new recipient
obj.set("recipient", newRecipient);
});
oldVersion++;
}
// Port PendingMessageStore to using Recipient
if (oldVersion == 14) {
schema.get("PendingMessage").addRealmObjectField("tempReceiver", schema.get("Recipient")).transform(obj -> {
final DynamicRealmObject oldUser = obj.getObject("receiver");
final String primaryKey = oldUser.getString("owner_address");
final DynamicRealmObject newRecipient = realm.createObject("Recipient", primaryKey);
newRecipient.setObject("user", oldUser);
obj.set("tempReceiver", newRecipient);
}).removeField("receiver").renameField("tempReceiver", "receiver");
oldVersion++;
}
if (oldVersion == 15) {
schema.get("User").addField("average_rating", Double.class);
oldVersion++;
}
if (oldVersion == 16) {
if (schema.get("SofaError") == null) {
schema.create("SofaError").addField("id", String.class, FieldAttribute.PRIMARY_KEY).addField("message", String.class);
}
if (!schema.get("SofaMessage").hasField("errorMessage")) {
schema.get("SofaMessage").addRealmObjectField("errorMessage", schema.get("SofaError"));
}
oldVersion++;
}
if (oldVersion == 17) {
final RealmObjectSchema mutedConversationSchema = schema.create("MutedConversation");
mutedConversationSchema.addField("threadId", String.class, FieldAttribute.PRIMARY_KEY);
oldVersion++;
}
if (oldVersion == 18) {
if (schema.contains("MutedConversation")) {
schema.rename("MutedConversation", "ConversationStatus").addField("isMuted", boolean.class).addField("isAccepted", boolean.class).transform(obj -> obj.set("isMuted", true));
}
schema.get("Conversation").addRealmObjectField("conversationStatus", schema.get("ConversationStatus"));
final List<DynamicRealmObject> conversations = realm.where("Conversation").findAll();
for (final DynamicRealmObject conversation : conversations) {
final String conversationThreadId = conversation.getString("threadId");
DynamicRealmObject conversationStatus = realm.where("ConversationStatus").equalTo("threadId", conversationThreadId).findFirst();
conversationStatus = conversationStatus != null ? conversationStatus : realm.createObject("ConversationStatus", conversationThreadId);
conversationStatus.set("isAccepted", true);
conversation.set("conversationStatus", conversationStatus);
}
oldVersion++;
}
if (oldVersion == 19) {
if (schema.get("Avatar") == null) {
schema.create("Avatar").addField("bytes", byte[].class);
}
if (!schema.get("Group").hasField("avatar")) {
schema.get("Group").addRealmObjectField("avatar", schema.get("Avatar"));
}
oldVersion++;
}
if (oldVersion == 20) {
final RealmResults<DynamicRealmObject> contacts = realm.where("Contact").findAll();
contacts.deleteAllFromRealm();
if (schema.contains("Contact")) {
schema.remove("Contact");
}
oldVersion++;
}
}
use of io.realm.RealmSchema in project ForPDA by RadiationX.
the class DbMigration method migrate.
@Override
public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
/*
for oldest versions
*/
RealmObjectSchema oldFavSchema = schema.get("FavItemBd");
if (oldFavSchema != null && !oldFavSchema.hasField("isForum")) {
oldFavSchema.addField("isForum", boolean.class);
}
RealmObjectSchema oldHistorySchema = schema.get("HistoryItemBd");
if (oldHistorySchema != null && !oldHistorySchema.hasField("url")) {
oldHistorySchema.addField("url", String.class);
}
if (oldVersion == 1) {
RealmObjectSchema favSchema = schema.get("FavItemBd");
if (favSchema != null) {
favSchema.removeField("isNewMessages").removeField("info").addField("isNew", boolean.class).addField("isPoll", boolean.class).addField("isClosed", boolean.class);
}
oldVersion++;
}
if (oldVersion == 2) {
RealmObjectSchema historySchema = schema.get("HistoryItemBd");
if (historySchema != null) {
SimpleDateFormat oldDateFormat = new SimpleDateFormat("MM.dd.yy, HH:mm", Locale.getDefault());
SimpleDateFormat newDateFormat = new SimpleDateFormat("dd.MM.yy, HH:mm", Locale.getDefault());
historySchema.transform(dynamicRealmObject -> {
String dateString = dynamicRealmObject.getString("date");
Date date = new Date();
try {
date = oldDateFormat.parse(dateString);
} catch (ParseException e) {
e.printStackTrace();
}
Log.d("SUKA", "DATES " + dateString + " : " + newDateFormat.format(date));
dynamicRealmObject.setString("date", newDateFormat.format(date));
});
}
oldVersion++;
}
if (oldVersion == 3) {
RealmObjectSchema favSchema = schema.get("FavItemBd");
if (favSchema != null) {
favSchema.addField("curatorId", int.class).addField("curatorNick", String.class).addField("subType", String.class);
}
oldVersion++;
}
}
use of io.realm.RealmSchema 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.RealmSchema in project realm-java by realm.
the class Migration method migrate.
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
// During a migration, a DynamicRealm is exposed. A DynamicRealm is an untyped variant of a normal Realm, but
// with the same object creation and query capabilities.
// A DynamicRealm uses Strings instead of Class references because the Classes might not even exist or have been
// renamed.
// Access the Realm schema in order to create, modify or delete classes and their fields.
RealmSchema schema = realm.getSchema();
// Migrate from version 0 to version 1
if (oldVersion == 0) {
RealmObjectSchema personSchema = schema.get("Person");
// Combine 'firstName' and 'lastName' in a new field called 'fullName'
personSchema.addField("fullName", String.class, FieldAttribute.REQUIRED).transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
obj.set("fullName", obj.getString("firstName") + " " + obj.getString("lastName"));
}
}).removeField("firstName").removeField("lastName");
oldVersion++;
}
// Migrate from version 1 to version 2
if (oldVersion == 1) {
// Create a new class
RealmObjectSchema petSchema = schema.create("Pet").addField("name", String.class, FieldAttribute.REQUIRED).addField("type", String.class, FieldAttribute.REQUIRED);
// Add a new field to an old class and populate it with initial data
schema.get("Person").addRealmListField("pets", petSchema).transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
if (obj.getString("fullName").equals("JP McDonald")) {
DynamicRealmObject pet = realm.createObject("Pet");
pet.setString("name", "Jimbo");
pet.setString("type", "dog");
obj.getList("pets").add(pet);
}
}
});
oldVersion++;
}
// Migrate from version 2 to version 3
if (oldVersion == 2) {
RealmObjectSchema personSchema = schema.get("Person");
// fullName is nullable now.
personSchema.setNullable("fullName", true);
// Change type from String to int
schema.get("Pet").addField("type_tmp", int.class).transform(new RealmObjectSchema.Function() {
@Override
public void apply(DynamicRealmObject obj) {
String oldType = obj.getString("type");
if (oldType.equals("dog")) {
obj.setLong("type_tmp", 1);
} else if (oldType.equals("cat")) {
obj.setInt("type_tmp", 2);
} else if (oldType.equals("hamster")) {
obj.setInt("type_tmp", 3);
}
}
}).removeField("type").renameField("type_tmp", "type");
oldVersion++;
}
}
Aggregations