Search in sources :

Example 6 with RealmSchema

use of io.realm.RealmSchema in project realm-java by realm.

the class PrimaryKeyTests method removingPrimaryKeyRemovesConstraint_typeSetters.

/**
 * This test surfaces a bunch of problems, most of them seem to be around caching of the schema
 * during a transaction
 *
 * 1) Removing the primary key do not invalidate the cache in RealmSchema and those cached
 *    are ImmutableRealmObjectSchema so do not change when the primary key is removed.
 *
 * 2) Addding `schema.refresh()` to RealmObjectSchema.removePrimaryKey()` causes
 *    RealmPrimaryKeyConstraintException anyway. Unclear why.
 */
@Test
public void removingPrimaryKeyRemovesConstraint_typeSetters() {
    RealmConfiguration config = configFactory.createConfigurationBuilder().name("removeConstraints").build();
    DynamicRealm realm = DynamicRealm.getInstance(config);
    RealmSchema realmSchema = realm.getSchema();
    realm.beginTransaction();
    RealmObjectSchema tableSchema = realmSchema.create("Employee").addField("name", String.class, FieldAttribute.PRIMARY_KEY);
    realm.createObject("Employee", "Foo");
    DynamicRealmObject obj = realm.createObject("Employee", "Foo2");
    try {
        // Tries to create 2nd entry with name Foo.
        obj.setString("name", "Foo");
    } catch (IllegalArgumentException e) {
        tableSchema.removePrimaryKey();
        obj.setString("name", "Foo");
    } finally {
        realm.close();
    }
}
Also used : RealmConfiguration(io.realm.RealmConfiguration) DynamicRealmObject(io.realm.DynamicRealmObject) RealmSchema(io.realm.RealmSchema) DynamicRealm(io.realm.DynamicRealm) RealmObjectSchema(io.realm.RealmObjectSchema) Test(org.junit.Test)

Example 7 with RealmSchema

use of io.realm.RealmSchema in project Rocket.Chat.Android by RocketChat.

the class Migration method migrate.

@Override
public void migrate(DynamicRealm dynamicRealm, long oldVersion, long newVersion) {
    RealmSchema schema = dynamicRealm.getSchema();
    if (oldVersion == 0) {
        // NOOP
        oldVersion++;
    }
    if (oldVersion == 1) {
        schema.create("RealmSpotlightUser").addField(RealmSpotlightUser.Columns.ID, String.class, FieldAttribute.PRIMARY_KEY).addField(RealmSpotlightUser.Columns.USERNAME, String.class).addField(RealmSpotlightUser.Columns.STATUS, String.class);
        schema.create("RealmSpotlightRoom").addField(RealmSpotlightRoom.Columns.ID, String.class, FieldAttribute.PRIMARY_KEY).addField(RealmSpotlightRoom.Columns.NAME, String.class).addField(RealmSpotlightRoom.Columns.TYPE, String.class);
        oldVersion++;
    }
    if (oldVersion == 2) {
        RealmObjectSchema roomSchema = schema.get("RealmSpotlightUser");
        roomSchema.addField(RealmSpotlightUser.Columns.NAME, String.class);
        oldVersion++;
    }
    if (oldVersion == 3) {
        schema.create("RealmRole").addField(RealmRole.Columns.ID, String.class, FieldAttribute.PRIMARY_KEY).addField(RealmRole.Columns.NAME, String.class);
        schema.create("RealmPermission").addField(RealmPermission.Columns.ID, String.class, FieldAttribute.PRIMARY_KEY).addField(RealmPermission.Columns.NAME, String.class).addRealmListField(RealmPermission.Columns.ROLES, schema.get("RealmRole"));
        schema.create("RealmRoomRole").addField(RealmRoomRole.Columns.ID, String.class, FieldAttribute.PRIMARY_KEY).addField(RealmRoomRole.Columns.ROOM_ID, String.class).addRealmObjectField(RealmRoomRole.Columns.USER, schema.get("RealmUser")).addRealmListField(RealmRoomRole.Columns.ROLES, schema.get("RealmRole"));
        oldVersion++;
    }
    if (oldVersion == 4) {
        RealmObjectSchema messageSchema = schema.get("RealmMessage");
        messageSchema.addField(RealmMessage.EDITED_AT, long.class);
    }
}
Also used : RealmSchema(io.realm.RealmSchema) RealmObjectSchema(io.realm.RealmObjectSchema)

Example 8 with RealmSchema

use of io.realm.RealmSchema in project ocreader by schaal.

the class DatabaseMigration method migrate.

@Override
public void migrate(@NonNull final DynamicRealm realm, long oldVersion, long newVersion) {
    Log.d(TAG, "Starting migration from " + oldVersion + " to " + newVersion);
    // contentHash for items
    if (oldVersion < 9) {
        throw new IllegalStateException("Migration from Schema < 9 not supported");
    }
    RealmSchema schema = realm.getSchema();
    /*
          9 -> 10

          - Add primary key ID to TemporaryFeed
          - Rename TemporaryFeed id to treeItemId
          - add TemporaryFeed object for list and pager activities
         */
    if (oldVersion == 9) {
        realm.delete("TemporaryFeed");
        final RealmObjectSchema temporaryFeedSchema = schema.get("TemporaryFeed");
        if (temporaryFeedSchema == null)
            throw new IllegalStateException("TemporaryFeed schema not found");
        temporaryFeedSchema.renameField(TemporaryFeed.ID, TemporaryFeed.TREE_ITEM_ID).addField(TemporaryFeed.ID, long.class, FieldAttribute.PRIMARY_KEY);
        realm.createObject("TemporaryFeed", TemporaryFeed.LIST_ID);
        realm.createObject("TemporaryFeed", TemporaryFeed.PAGER_ID);
        oldVersion++;
    }
    /*
          10 -> 11

           - Make sure every item has updatedAt != null, set updatedAt = pubDate if not
         */
    if (oldVersion == 10) {
        for (DynamicRealmObject object : realm.where("Item").isNull(Item.UPDATED_AT).findAll()) {
            object.setDate(Item.UPDATED_AT, object.getDate(Item.PUB_DATE));
        }
        oldVersion++;
    }
    /*
          11 -> 12

          - Add active property to Item
         */
    if (oldVersion == 11) {
        final RealmObjectSchema itemSchema = schema.get("Item");
        if (itemSchema == null)
            throw new IllegalStateException("Item schema not found");
        itemSchema.addField(Item.ACTIVE, boolean.class, FieldAttribute.INDEXED);
        // noinspection UnusedAssignment
        oldVersion++;
    }
}
Also used : DynamicRealmObject(io.realm.DynamicRealmObject) RealmSchema(io.realm.RealmSchema) RealmObjectSchema(io.realm.RealmObjectSchema)

Aggregations

RealmSchema (io.realm.RealmSchema)8 RealmObjectSchema (io.realm.RealmObjectSchema)7 DynamicRealmObject (io.realm.DynamicRealmObject)5 DynamicRealm (io.realm.DynamicRealm)3 RealmConfiguration (io.realm.RealmConfiguration)2 RealmMigration (io.realm.RealmMigration)2 HDWallet (com.toshi.crypto.HDWallet)1 Attachment (com.xabber.android.data.database.messagerealm.Attachment)1 ForwardId (com.xabber.android.data.database.messagerealm.ForwardId)1 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)1 SyncInfo (com.xabber.android.data.database.messagerealm.SyncInfo)1 FieldAttribute (io.realm.FieldAttribute)1 RealmResults (io.realm.RealmResults)1 File (java.io.File)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 UUID (java.util.UUID)1 Test (org.junit.Test)1