Search in sources :

Example 1 with RealmObjectSchema

use of io.realm.RealmObjectSchema 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++;
    }
}
Also used : DynamicRealmObject(io.realm.DynamicRealmObject) RealmSchema(io.realm.RealmSchema) RealmObjectSchema(io.realm.RealmObjectSchema)

Example 2 with RealmObjectSchema

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

the class AllTypesRealmProxy method createRealmObjectSchema.

public static RealmObjectSchema createRealmObjectSchema(RealmSchema realmSchema) {
    if (!realmSchema.contains("AllTypes")) {
        RealmObjectSchema realmObjectSchema = realmSchema.create("AllTypes");
        realmObjectSchema.add(new Property("columnString", RealmFieldType.STRING, Property.PRIMARY_KEY, Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("columnLong", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("columnFloat", RealmFieldType.FLOAT, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("columnDouble", RealmFieldType.DOUBLE, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("columnBoolean", RealmFieldType.BOOLEAN, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("columnDate", RealmFieldType.DATE, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("columnBinary", RealmFieldType.BINARY, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        if (!realmSchema.contains("AllTypes")) {
            AllTypesRealmProxy.createRealmObjectSchema(realmSchema);
        }
        realmObjectSchema.add(new Property("columnObject", RealmFieldType.OBJECT, realmSchema.get("AllTypes")));
        if (!realmSchema.contains("AllTypes")) {
            AllTypesRealmProxy.createRealmObjectSchema(realmSchema);
        }
        realmObjectSchema.add(new Property("columnRealmList", RealmFieldType.LIST, realmSchema.get("AllTypes")));
        return realmObjectSchema;
    }
    return realmSchema.get("AllTypes");
}
Also used : RealmObjectSchema(io.realm.RealmObjectSchema)

Example 3 with RealmObjectSchema

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

the class BooleansRealmProxy method createRealmObjectSchema.

public static RealmObjectSchema createRealmObjectSchema(RealmSchema realmSchema) {
    if (!realmSchema.contains("Booleans")) {
        RealmObjectSchema realmObjectSchema = realmSchema.create("Booleans");
        realmObjectSchema.add(new Property("done", RealmFieldType.BOOLEAN, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("isReady", RealmFieldType.BOOLEAN, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("mCompleted", RealmFieldType.BOOLEAN, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("anotherBoolean", RealmFieldType.BOOLEAN, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        return realmObjectSchema;
    }
    return realmSchema.get("Booleans");
}
Also used : RealmObjectSchema(io.realm.RealmObjectSchema)

Example 4 with RealmObjectSchema

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

the class NullTypesRealmProxy method createRealmObjectSchema.

public static RealmObjectSchema createRealmObjectSchema(RealmSchema realmSchema) {
    if (!realmSchema.contains("NullTypes")) {
        RealmObjectSchema realmObjectSchema = realmSchema.create("NullTypes");
        realmObjectSchema.add(new Property("fieldStringNotNull", RealmFieldType.STRING, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldStringNull", RealmFieldType.STRING, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldBooleanNotNull", RealmFieldType.BOOLEAN, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldBooleanNull", RealmFieldType.BOOLEAN, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldBytesNotNull", RealmFieldType.BINARY, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldBytesNull", RealmFieldType.BINARY, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldByteNotNull", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldByteNull", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldShortNotNull", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldShortNull", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldIntegerNotNull", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldIntegerNull", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldLongNotNull", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldLongNull", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldFloatNotNull", RealmFieldType.FLOAT, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldFloatNull", RealmFieldType.FLOAT, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldDoubleNotNull", RealmFieldType.DOUBLE, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldDoubleNull", RealmFieldType.DOUBLE, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldDateNotNull", RealmFieldType.DATE, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        realmObjectSchema.add(new Property("fieldDateNull", RealmFieldType.DATE, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        if (!realmSchema.contains("NullTypes")) {
            NullTypesRealmProxy.createRealmObjectSchema(realmSchema);
        }
        realmObjectSchema.add(new Property("fieldObjectNull", RealmFieldType.OBJECT, realmSchema.get("NullTypes")));
        return realmObjectSchema;
    }
    return realmSchema.get("NullTypes");
}
Also used : RealmObjectSchema(io.realm.RealmObjectSchema)

Example 5 with RealmObjectSchema

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

the class SimpleRealmProxy method createRealmObjectSchema.

public static RealmObjectSchema createRealmObjectSchema(RealmSchema realmSchema) {
    if (!realmSchema.contains("Simple")) {
        RealmObjectSchema realmObjectSchema = realmSchema.create("Simple");
        realmObjectSchema.add(new Property("name", RealmFieldType.STRING, !Property.PRIMARY_KEY, !Property.INDEXED, !Property.REQUIRED));
        realmObjectSchema.add(new Property("age", RealmFieldType.INTEGER, !Property.PRIMARY_KEY, !Property.INDEXED, Property.REQUIRED));
        return realmObjectSchema;
    }
    return realmSchema.get("Simple");
}
Also used : RealmObjectSchema(io.realm.RealmObjectSchema)

Aggregations

RealmObjectSchema (io.realm.RealmObjectSchema)6 RealmSchema (io.realm.RealmSchema)2 DynamicRealmObject (io.realm.DynamicRealmObject)1