package de.xikolo.models.migrate;

import io.realm.DynamicRealm;
import io.realm.FieldAttribute;
import io.realm.RealmMigration;
import io.realm.RealmSchema;

public class RealmSchemaMigration implements RealmMigration {

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

        // Data schema migrations
        // See https://realm.io/docs/java/latest/#migrations
        if (oldVersion == 1) {
            schema.create("Channel")
                .addField("id", String.class, FieldAttribute.PRIMARY_KEY)
                .addField("title", String.class)
                .addField("slug", String.class)
                .addField("color", String.class)
                .addField("position", int.class)
                .addField("description", String.class)
                .addField("imageUrl", String.class);

            schema.get("Course")
                .addField("channelId", String.class);
        }

        if (oldVersion == 2) {
            schema.create("Document")
                .addField("id", String.class, FieldAttribute.PRIMARY_KEY)
                .addField("title", String.class)
                .addField("description", String.class)
                .addRealmListField("tags", String.class)
                .addField("isPublic", boolean.class)
                .addRealmListField("courseIds", String.class);

            schema.create("DocumentLocalization")
                .addField("id", String.class, FieldAttribute.PRIMARY_KEY)
                .addField("title", String.class)
                .addField("description", String.class)
                .addField("language", String.class)
                .addField("revision", int.class)
                .addField("fileUrl", String.class)
                .addField("documentId", String.class);
        }
    }

}

@@ -1028,32 +1028,59 @@
 , String.class);
+%0A%0A            oldVersion++;
 %0A        %7D%0A%0A    
@@ -1930,16 +1930,43 @@
 .class);
+%0A%0A            oldVersion++;
 %0A       

Fix realm migrations