Georgi Koemdzhiev
Georgi Koemdzhiev

Reputation: 11931

Performing a simple migration in Realm

I am testing Realm db for my next Android app and I want to do a simple migration. I got a bit confused looking at the documentation.

I have this simple class User:

public class User extends RealmObject {
@PrimaryKey
private String name;
private int age;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
}

and I want to add (via migration) an additional field to the table User called "phoneNumber". I don't understand how/where I can initiate the migration? I am creating the schema in the application onCreate method like that:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                .schemaVersion(0)
                .migration(new MyMigration())
                .build();
        Realm.setDefaultConfiguration(realmConfiguration);
}

}

and my migration class:

public class MyMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
    // DynamicRealm exposes an editable schema
    RealmSchema schema = realm.getSchema();

    // Migrate to version 1: Add a new class.
    // Example:
    // public Person extends RealmObject {
    //     private String name;
    //     private int age;
    //     // getters and setters left out for brevity
    // }
    if (oldVersion == 0) {
        schema.create("User")
                .addField("name", String.class)
                .addField("age", int.class);
        oldVersion++;
    }

    // Migrate to version 2: Add a primary key + object references
    // Example:
    // public Person extends RealmObject {
    //     private String name;
    //     @PrimaryKey
    //     private int age;
    //     private Dog favoriteDog;
    //     private RealmList<Dog> dogs;
    //     // getters and setters left out for brevity
    // }
    if (oldVersion == 1) {
        schema.get("User")
                .addField("phoneNumber", long.class, FieldAttribute.REQUIRED);
        oldVersion++;
    }

    Log.d("MainActivity","Migration happend!");
}

}

Upvotes: 2

Views: 1063

Answers (1)

beeender
beeender

Reputation: 3565

Try this:

public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
    // DynamicRealm exposes an editable schema
    RealmSchema schema = realm.getSchema();

    // This is the current schema version before migration.
    if (oldVersion == 0) {
        schema.get("User")
            .addField("phoneNumber", long.class, FieldAttribute.REQUIRED);
        oldVersion++;
    }

    // More migrations (for version 1->2, 2->3, etc..) in the future.
}

And create RealmConfiguration like below:

RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
            .schemaVersion(1) // This the expected schema version after migration.
            .migration(new MyMigration())
            .build();
Realm.setDefaultConfiguration(realmConfiguration);

Upvotes: 1

Related Questions