Los siguientes ejemplos muestran cómo agregar, eliminar y modificar propiedades en un esquema. Primero, realice el cambio de esquema requerido. Luego, incremente la versión del esquema. Finalmente, si el cambio es perjudicial (destructivo), cree la versión correspondiente. función de migración para mover datos del esquema original al esquema actualizado.
Nota
Versions Update on Realm Open
Assume that each schema change shown in the following example occurs after the application has used the existing schema. The new schema version numbers apply only after you open the realm and explicitly specify the new version number. In other words, you can't specify version 3 without previously specifying and using versions 0, 1, and 2.
Un reino que utiliza la versión del esquema 0 tiene un tipo de objeto Person:
public class Person extends RealmObject { // Realm schema version 0 public String firstName; public int age; }
class Person: RealmObject { // Realm schema version 0 var firstName: String = "" var age: int = 0 }
A. Agregar una propiedad
El siguiente ejemplo añade una propiedad lastName al esquema Persona original:
public class Person extends RealmObject { // Realm schema version 1 public String firstName; public String lastName; public int age; }
class Person: RealmObject { // Realm schema version 1 var firstName: String = "" var lastName: String = "" var age: int = 0 }
B. Borrar una Propiedad
The following example uses a combined fullName property instead of the separate firstName and lastName property in the original Person schema:
public class Person extends RealmObject { // Realm schema version 2 public String fullName; public int age; }
class Person: RealmObject { // Realm schema version 2 var fullName: String = "" var age: int = 0 }
C. Modify a Property Type or Rename a Property
El siguiente ejemplo modifica la propiedad age en el esquema original de Persona renombrándola a birthday y cambiando el tipo a Date:
public class Person extends RealmObject { // Realm schema version 3 public String fullName; public Date birthday = new Date(); }
class Person: RealmObject { // Realm schema version 3 var fullName: String = "" var birthday: Date = Date() }
D. Funciones de migración
Para migrar el realm y cumplir con el esquema actualizado Person, establece la versión del esquema del realm en 3 y define una función de migración para fijar el valor de fullName según las propiedades existentes firstName y lastName, así como el valor de birthday según age:
public class Migration implements RealmMigration { public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { Long version = oldVersion; // DynamicRealm exposes an editable schema RealmSchema schema = realm.getSchema(); // Changes from version 0 to 1: Adding lastName. // All properties will be initialized with the default value "". if (version == 0L) { schema.get("Person") .addField("lastName", String.class, FieldAttribute.REQUIRED); version++; } // Changes from version 1 to 2: combine firstName/lastName into fullName if (version == 1L) { schema.get("Person") .addField("fullName", String.class, FieldAttribute.REQUIRED) .transform( DynamicRealmObject obj -> { String name = "${obj.getString("firstName")} ${obj.getString("lastName")}"; obj.setString("fullName", name); }) .removeField("firstName") .removeField("lastName"); version++; } // Changes from version 2 to 3: replace age with birthday if (version == 2L) { schema.get("Person") .addField("birthday", Date::class.java, FieldAttribute.REQUIRED) .transform(DynamicRealmObject obj -> { Int birthYear = Date().year - obj.getInt("age"); obj.setDate("birthday", Date(birthYear, 1, 1)); }) .removeField("age"); version++; } } }; public class Module {} RealmConfiguration config = new RealmConfiguration.Builder() .modules(new Module()) .schemaVersion(3) // Must be bumped when the schema changes .migration(new Migration()) // Migration to run instead of throwing an exception .build();
val migration = object: RealmMigration { override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { var version: Long = oldVersion // DynamicRealm exposes an editable schema val schema: RealmSchema = realm.schema // Changes from version 0 to 1: Adding lastName. // All properties will be initialized with the default value "". if (version == 0L) { schema.get("Person")!! .addField("lastName", String::class.java, FieldAttribute.REQUIRED) version++ } // Changes from version 1 to 2: Combining firstName/lastName into fullName if (version == 1L) { schema.get("Person")!! .addField("fullName", String::class.java, FieldAttribute.REQUIRED) .transform { obj: DynamicRealmObject -> val name = "${obj.getString("firstName")} ${obj.getString("lastName")}" obj.setString("fullName", name) } .removeField("firstName") .removeField("lastName") version++ } // Changes from version 2 to 3: Replace age with birthday if (version == 2L) { schema.get("Person")!! .addField("birthday", Date::class.java, FieldAttribute.REQUIRED) .transform { obj: DynamicRealmObject -> var birthYear = Date().year - obj.getInt("age") obj.setDate("birthday", Date(birthYear, 1, 1)) } .removeField("age") version++ } } } class Module val config = RealmConfiguration.Builder() .schemaVersion(3) // Must be bumped when the schema changes .migration(migration) // Migration to run instead of throwing an exception .build()
You can make changes to the schema of a synchronized realm by modifying the JSON Schema defined in your application's backend. Synchronized realms do not require migration functions, since breaking changes to synchronized schemas cause synchronization errors. Sync handles non-breaking changes to synchronized realm schemas automatically. As a result, both old versions of your schema and new versions can synchronize as users upgrade their applications.
Para aprender más sobre los cambios en el esquema de los reinos sincronizados, consulta cambios de esquema sincronizados.
Nota
Modo de desarrollo: solo para definir tu esquema inicial
You can define the first version of your application's synced schema using Development Mode, which automatically translates client models into JSON Schema on the backend. However, you should make subsequent changes to your schema through edits to the JSON Schema in your App.
Tip
Puedes usar el método de construcción RealmConfiguration.shouldDeleteRealmIfMigrationNeeded() al construir un reino para eliminarlo en lugar de realizar una migración cuando sea necesario. Esto puede ser útil durante el desarrollo, cuando los esquemas cambian con frecuencia.