/** * 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(); } }
@Override public void onCreate() { super.onCreate(); Realm.init(this); Realm.setDefaultConfiguration(new RealmConfiguration.Builder().schemaVersion(7) // .migration(new RealmMigration() { @Override public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) { RealmAutoMigration.migrate(realm); } }) // .initialData(realm -> { Cat cat = new Cat(); for(CatNames catName : CatNames.values()) { cat.setName(catName.getName()); realm.insert(cat); } }) // .build()); SingletonComponent singletonComponent = DaggerSingletonComponent.create(); Injector.setComponent(singletonComponent); }
@Override public void migrate(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) { oldVersion++; } // Migrate from version 1 to version 2 if (oldVersion == 1) { RealmObjectSchema bloodGlucoseDataSchema = schema.get("BloodGlucoseData"); bloodGlucoseDataSchema .addField("timezoneOffsetInMinutes", int.class) .transform(timezoneTransformFunction); //oldVersion++; } }
@Override public void onCreate() { super.onCreate(); sInstance = this; RealmConfiguration configuration = new RealmConfiguration.Builder(this).deleteRealmIfMigrationNeeded() .migration(new RealmMigration() { @Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { } }) .name("chao.realm") .build(); Realm.setDefaultConfiguration(configuration); Stetho.initialize(Stetho.newInitializerBuilder(this) .enableDumpapp(Stetho.defaultDumperPluginsProvider(this)) .enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build()) .build()); FacebookSdk.sdkInitialize(getApplicationContext()); AppEventsLogger.activateApp(this); }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { RealmSchema schema = realm.getSchema(); /* * Migrates to version 1 of the schema. * - Make uniqueId the primary key for the RBook class instead of the relative path. */ if (oldVersion == 0) { schema.get("RBook") .removePrimaryKey() // Remove @PrimaryKey from relPath. .addIndex("relPath") // Add @Index to relPath. .addPrimaryKey("uniqueId"); // Add @PrimaryKey to uniqueId. oldVersion++; } }
@Override public void onCreate() { super.onCreate(); mAppContext = this; // 配置Realm数据库 RealmConfiguration configuration = new RealmConfiguration .Builder(this) .deleteRealmIfMigrationNeeded() .schemaVersion(6) .migration(new RealmMigration() { @Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { } }).build(); Realm.setDefaultConfiguration(configuration); //配置腾讯bugly CrashReport.initCrashReport(getApplicationContext(), ConstantUtil.BUGLY_ID, false); }
@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("UserScript") .addField("title", String.class) .addField("url", String.class); oldVersion++; } if (oldVersion == 1) { schema.get("UserScript") .addField("javascript", String.class); oldVersion++; } }
@Override public void onCreate() { super.onCreate(); RealmConfiguration config = new RealmConfiguration.Builder(getApplicationContext()) .name(getResources().getString(R.string.database_conf_name)) .schemaVersion(getResources().getInteger(R.integer.database_conf_version)) .modules(new DbModule()) .deleteRealmIfMigrationNeeded() .migration(new RealmMigration() { @Override public void migrate(DynamicRealm dynamicRealm, long l, long l1) { Log.d(TAG, "migrate l [" + l + "] l1 [" + l1 + "]"); } }) .build(); Realm.setDefaultConfiguration(config); }
@Override public <E> Flowable<RealmList<E>> from(DynamicRealm realm, final RealmList<E> list) { final RealmConfiguration realmConfig = realm.getConfiguration(); return Flowable.create(new FlowableOnSubscribe<RealmList<E>>() { @Override public void subscribe(final FlowableEmitter<RealmList<E>> emitter) throws Exception { // Gets instance to make sure that the Realm is open for as long as the // Observable is subscribed to it. final DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig); listRefs.get().acquireReference(list); final RealmChangeListener<RealmList<E>> listener = new RealmChangeListener<RealmList<E>>() { @Override public void onChange(RealmList<E> results) { if (!emitter.isCancelled()) { emitter.onNext(list); } } }; list.addChangeListener(listener); // Cleanup when stream is disposed emitter.setDisposable(Disposables.fromRunnable(new Runnable() { @Override public void run() { list.removeChangeListener(listener); observableRealm.close(); listRefs.get().releaseReference(list); } })); // Emit current value immediately emitter.onNext(list); } }, BACK_PRESSURE_STRATEGY); }
@Override public <E> Observable<CollectionChange<RealmList<E>>> changesetsFrom(DynamicRealm realm, final RealmList<E> list) { final RealmConfiguration realmConfig = realm.getConfiguration(); return Observable.create(new ObservableOnSubscribe<CollectionChange<RealmList<E>>>() { @Override public void subscribe(final ObservableEmitter<CollectionChange<RealmList<E>>> emitter) throws Exception { // Gets instance to make sure that the Realm is open for as long as the // Observable is subscribed to it. final DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig); listRefs.get().acquireReference(list); final OrderedRealmCollectionChangeListener<RealmList<E>> listener = new OrderedRealmCollectionChangeListener<RealmList<E>>() { @Override public void onChange(RealmList<E> results, OrderedCollectionChangeSet changeSet) { if (!emitter.isDisposed()) { emitter.onNext(new CollectionChange<>(results, changeSet)); } } }; list.addChangeListener(listener); // Cleanup when stream is disposed emitter.setDisposable(Disposables.fromRunnable(new Runnable() { @Override public void run() { list.removeChangeListener(listener); observableRealm.close(); listRefs.get().releaseReference(list); } })); // Emit current value immediately emitter.onNext(new CollectionChange<>(list, null)); } }); }
@Override public Flowable<DynamicRealmObject> from(DynamicRealm realm, final DynamicRealmObject object) { final RealmConfiguration realmConfig = realm.getConfiguration(); return Flowable.create(new FlowableOnSubscribe<DynamicRealmObject>() { @Override public void subscribe(final FlowableEmitter<DynamicRealmObject> emitter) throws Exception { // Gets instance to make sure that the Realm is open for as long as the // Observable is subscribed to it. final DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig); objectRefs.get().acquireReference(object); final RealmChangeListener<DynamicRealmObject> listener = new RealmChangeListener<DynamicRealmObject>() { @Override public void onChange(DynamicRealmObject obj) { if (!emitter.isCancelled()) { emitter.onNext(obj); } } }; RealmObject.addChangeListener(object, listener); // Cleanup when stream is disposed emitter.setDisposable(Disposables.fromRunnable(new Runnable() { @Override public void run() { RealmObject.removeChangeListener(object, listener); observableRealm.close(); objectRefs.get().releaseReference(object); } })); // Emit current value immediately emitter.onNext(object); } }, BACK_PRESSURE_STRATEGY); }
@Override public Observable<ObjectChange<DynamicRealmObject>> changesetsFrom(DynamicRealm realm, final DynamicRealmObject object) { final RealmConfiguration realmConfig = realm.getConfiguration(); return Observable.create(new ObservableOnSubscribe<ObjectChange<DynamicRealmObject>>() { @Override public void subscribe(final ObservableEmitter<ObjectChange<DynamicRealmObject>> emitter) throws Exception { // Gets instance to make sure that the Realm is open for as long as the // Observable is subscribed to it. final DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig); objectRefs.get().acquireReference(object); final RealmObjectChangeListener<DynamicRealmObject> listener = new RealmObjectChangeListener<DynamicRealmObject>() { @Override public void onChange(DynamicRealmObject obj, ObjectChangeSet changeSet) { if (!emitter.isDisposed()) { emitter.onNext(new ObjectChange<>(obj, changeSet)); } } }; object.addChangeListener(listener); // Cleanup when stream is disposed emitter.setDisposable(Disposables.fromRunnable(new Runnable() { @Override public void run() { object.removeChangeListener(listener); observableRealm.close(); objectRefs.get().releaseReference(object); } })); // Emit current value immediately emitter.onNext(new ObjectChange<>(object, null)); } }); }
@SuppressLint("DefaultLocale") @Override public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) { if (oldVersion < newVersion) { // Unknown migration throw new RealmMigrationNeededException(realm.getPath(), String.format("Migration missing from v%d to v%d", oldVersion, newVersion)); } }
@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.get("RealmContact") .addPrimaryKey("idx"); // .addField("idx", String.class, FieldAttribute.PRIMARY_KEY); 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("RealmContact") .addField("idx", String.class, FieldAttribute.PRIMARY_KEY) .addField("name", String.class) .addField("phone", String.class); oldVersion++;*/ } }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { RealmSchema schema = realm.getSchema(); Log.i(TAG, "MIGRATING DATABASE from v%d to v%d", oldVersion, newVersion); // why < 2? because I forgot to assign a schema version until I wrote this migration, so the // oldVersion here will be whatever default is assigned by Realm if (oldVersion < 2) { if (!schema.get("Post").hasField("customExcerpt")) { Log.d(TAG, "ADDING CUSTOM EXCERPT FIELD TO POST TABLE"); schema.get("Post").addField("customExcerpt", String.class); } oldVersion = 2; } }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { if (newVersion > oldVersion) { RealmSchema realmSchema = realm.getSchema(); if (oldVersion == 1) { realmSchema.create("UpgradeModel") .addField("localId", String.class, FieldAttribute.PRIMARY_KEY) .addField("version", String.class) .addField("url", String.class) .addField("compulsive_upgrade", String.class) .addField("description", String.class); } updateLearningModel(realmSchema); } }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { RealmSchema schema = realm.getSchema(); if (oldVersion < 1) { schema.get("Devotional").addField("unread", boolean.class); } }
RealmManager() { final RealmConfiguration configuration = Realm.getDefaultInstance().getConfiguration(); realmFileName = configuration.getRealmFileName(); dynamicRealm = DynamicRealm.getInstance(configuration); dynamicRealm.addChangeListener(new RealmChangeListener<DynamicRealm>() { @Override public void onChange(final DynamicRealm element) { dataChangedListener.onDataChanged(); } }); handler = new Handler(); }
@Override public void migrate(DynamicRealm dynamicRealm, long oldVersion, long newVersion) { if (oldVersion == 0) { dynamicRealm.getSchema().get("Invitation").addField("email", String.class); oldVersion++; } if (oldVersion == 1) { dynamicRealm.getSchema().get("Message").addField("receiptorsStr", String.class); dynamicRealm.getSchema().get("Member").addField("service", String.class); dynamicRealm.getSchema().create("IdeaDraft") .addField("title", String.class) .addField("description", String.class); dynamicRealm.getSchema().rename("TeamInfo", "Team"); dynamicRealm.getSchema().get("Team").addField("source", String.class); dynamicRealm.getSchema().get("Team").addField("sourceId", String.class); dynamicRealm.getSchema().get("Team").addField("signCode", String.class); dynamicRealm.getSchema().get("Team").addField("inviteCode", String.class); dynamicRealm.getSchema().get("Team").addField("inviteUrl", String.class); dynamicRealm.getSchema().get("Team").addField("isQuit", boolean.class); dynamicRealm.getSchema().get("Team").addField("nonJoinable", boolean.class); dynamicRealm.getSchema().get("Team").addField("hasUnread", boolean.class); dynamicRealm.getSchema().get("Team").addField("unread", int.class); dynamicRealm.getSchema().get("Team").addPrimaryKey("_id"); RealmObjectSchema roomSchema = dynamicRealm.getSchema().get("Room"); dynamicRealm.getSchema().get("Notification").addRealmObjectField("room", roomSchema); RealmObjectSchema memberSchema = dynamicRealm.getSchema().get("Member"); dynamicRealm.getSchema().get("Notification").addRealmObjectField("member", memberSchema); RealmObjectSchema storySchema = dynamicRealm.getSchema().get("Story"); dynamicRealm.getSchema().get("Notification").addRealmObjectField("story", storySchema); oldVersion++; } }
public DBMetadataCollector() { mTables = new ArrayList<>(); if (mConfiguration == null) throw new NullPointerException("RealmConfiguration is null"); this.mRealm = DynamicRealm.getInstance(mConfiguration); execute(); }
public void alterValue(final Position position, final String s) { mRealm.executeTransaction(new DynamicRealm.Transaction() { @Override public void execute(DynamicRealm realm) { DynamicRealmObject dynamicRealmObject = realm.where(mActiveTable.getName()) .findAll().get(position.y); dynamicRealmObject.set(mActiveTable.getColumns().get(position.x).getName(), s); } }); }
public void deleteRow(final Position position) { mRealm.executeTransaction(new DynamicRealm.Transaction() { @Override public void execute(DynamicRealm realm) { DynamicRealmObject object = realm.where(mActiveTable.getName()).findAll().get(position.y); object.deleteFromRealm(); } }); }
public ViewAdapter(TableStructure table, ScrollTableView view) { this.mTable = table; mView = view; realm = DynamicRealm.getInstance(DBMetadataCollector.mConfiguration); mItens = realm.where(mTable.getName()).findAll(); generateData(); }
/** * 初始化 */ public static void init(Context context) { if (sInstance == null) { if (context != null && context instanceof Application) { sInstance = (Application) context; /** * 初始化Realm数据库 */ Realm.init(context); Realm.setDefaultConfiguration(new RealmConfiguration.Builder() // .directory(context.getFilesDir()) // .name(Realm.DEFAULT_REALM_NAME) // .encryptionKey(null) .schemaVersion(4) .migration(new RealmMigration() { @Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { Log.e("Realm: migrate", "数据库迁移@oldVersion: " + oldVersion + "@newVersion: " + newVersion); } }) .deleteRealmIfMigrationNeeded() .build()); Stetho.initialize(Stetho.newInitializerBuilder(context) .enableDumpapp(Stetho.defaultDumperPluginsProvider(context)) .enableWebKitInspector(RealmInspectorModulesProvider.builder(context) // .withFolder(context.getCacheDir()) // .withEncryptionKey("encrypted.realm", "MagicCube".getBytes()) // .withMetaTables() // .withDescendingOrder() // .withLimit(1000) // .databaseNamePattern(Pattern.compile(".+\\.realm")) .build()) .build()); } else { throw new IllegalArgumentException("context is null or doesn't Application Instance!"); } } else { Log.e("DBHelper: init", "sInstance != null ==> 重复初始化!"); } }
@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(); if(oldVersion==1){ RealmObjectSchema settingSchema = schema.get("Setting"); settingSchema.addField("locale",String.class) .transform(new RealmObjectSchema.Function() { @Override public void apply(DynamicRealmObject obj) { obj.set("locale","Auto"); } }); oldVersion++; } if(oldVersion==2){ RealmObjectSchema cacheSchema = schema.get("NetWorkCache"); cacheSchema.removeField("record").addField("record",String.class); oldVersion++; } }
@Override public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) { // DynamicRealm exposes an editable schema RealmSchema schema = realm.getSchema(); if (schema == null) return; // TODO: Check in future versions if schema.get() returns null at some point try { if (oldVersion == 1) { schema.get("Sample") .addField("version", int.class); oldVersion++; } if (oldVersion == 2) { schema.get("Device") .removeField("serialNumber"); schema.get("Sample") .addField("database", int.class); oldVersion++; } } catch (NullPointerException e) { LOGE(TAG, "Schema is null!"); e.printStackTrace(); } }
@Singleton @Provides RealmConfiguration provideRealmConfiguration(Context context) { return new RealmConfiguration.Builder(context) .name("deglancer.realm") .schemaVersion(3) .deleteRealmIfMigrationNeeded() .migration(new RealmMigration() { @Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { } }) .build(); }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { RealmSchema schema = realm.getSchema(); Crashlytics.log(Log.INFO, TAG, "MIGRATING DATABASE from v" + oldVersion + " to v" + newVersion); // why < 2? because I forgot to assign a schema version until I wrote this migration, so the // oldVersion here will be whatever default is assigned by Realm if (oldVersion < 2) { if (!schema.get("Post").hasField("customExcerpt")) { Crashlytics.log(Log.DEBUG, TAG, "ADDING CUSTOM EXCERPT FIELD TO POST TABLE"); schema.get("Post").addField("customExcerpt", String.class); } oldVersion = 2; } }
public void showTableStructure(DynamicRealm dynamicRealm) { RealmObjectSchema realmObjectSchema = dynamicRealm.getSchema().get(simpleTableName); Set<String> fieldNames = realmObjectSchema.getFieldNames(); stringBuilder.append("<div class=\"content\">").append("<table class=\"dataTable\">") .append("<th>Column Name</th><th>Type</th>"); for (String fieldName : fieldNames) { RealmFieldType realmFieldType = realmObjectSchema.getFieldType(fieldName); stringBuilder.append("<tr>") .append("<td>").append(fieldName).append("</td>") .append("<td>").append(realmFieldType.name()).append("</td>") .append("</tr>"); } stringBuilder.append("</table></div>"); }
@Override public <E> Single<RealmQuery<E>> from(DynamicRealm realm, RealmQuery<E> query) { throw new RuntimeException("RealmQuery not supported yet."); }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { //Mengambil schema RealmSchema schema = realm.getSchema(); //membuat schema baru jika versi 0 if (oldVersion == 0) { //Profil schema.create("Profil") .addField("nama", String.class) .addField("email", String.class) .addField("instansi", String.class); //Tugas schema.create("Akademik") .addField("judul", String.class) .addField("jenis", String.class) .addField("option", String.class) .addField("deadline", String.class) .addField("deskripsi", String.class) .addField("done", String.class); //Organisasi schema.create("Organisasi") .addField("judul", String.class) .addField("jenis", String.class) .addField("deadline", String.class) .addField("deskripsi", String.class) .addField("option", String.class) .addField("sebagai", String.class) .addField("tugas", String.class) .addField("presensi", String.class) .addField("notulensi", String.class) .addField("done", String.class); //Lainnya schema.create("Lainnya") .addField("judul", String.class) .addField("deadline", String.class) .addField("deskripsi", String.class) .addField("done", String.class); oldVersion++; } }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { }
@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) { // Make updates to the schema here. For example: // schema.create("Person") // .addField("name", String.class) // .addField("age", int.class); // Increment so that old versions multiple steps behind get all migrations 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) { // Make updates to the schema here. For example: //schema.get("Person") // .addField("id", long.class, FieldAttribute.PRIMARY_KEY) // .addRealmObjectField("favoriteDog", schema.get("Dog")) // .addRealmListField("dogs", schema.get("Dog")); // Increment so that old versions multiple steps behind get all migrations oldVersion++; } }
@Override public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) { }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { // DynamicRealm exposes an editable schema RealmSchema schema = realm.getSchema(); if (oldVersion == 0) { schema.get("Trip") .addField("synced", boolean.class); oldVersion++; } if (oldVersion == 1) { schema.get("StationUse") .addField("manualEntry", boolean.class); oldVersion++; } if (oldVersion == 2) { schema.get("Trip") .addField("userConfirmed", boolean.class); oldVersion++; } if (oldVersion == 3) { schema.get("Trip") .addField("submitted", boolean.class); oldVersion++; } if (oldVersion == 4) { schema.create("Feedback") .addField("id", String.class) .addPrimaryKey("id") .addField("synced", boolean.class) .addField("timestamp", Date.class) .setRequired("timestamp", true) .addField("type", String.class) .setRequired("type", true) .addField("contents", String.class) .setRequired("contents", true); oldVersion++; } if (oldVersion == 5) { schema.get("Trip") .addField("syncFailures", int.class); oldVersion++; } if (oldVersion == 6) { schema.create("NotificationRule") .addField("name", String.class) .setRequired("name", true) .addField("enabled", boolean.class) .addField("startTime", long.class) .addField("endTime", long.class) .addRealmListField("weekDays", Integer.class) .setRequired("weekDays", true); oldVersion++; } if (oldVersion == 7) { schema.get("NotificationRule") .addField("id", String.class) .addPrimaryKey("id"); oldVersion++; } }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { RealmSchema schema = realm.getSchema(); }
@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("Person") .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("Person") .addField("id", long.class, FieldAttribute.PRIMARY_KEY) .addRealmObjectField("favoriteDog", schema.get("Dog")) .addRealmListField("dogs", schema.get("Dog")); */ oldVersion++; } }
@Override public void migrate(@NonNull final DynamicRealm realm, long oldVersion, long newVersion) { Log.d(TAG, "Starting migration from " + oldVersion + " to " + newVersion); // Migration from versions < 9 not supported, versions prior 9 were missing the // 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++; } }
@Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { L.i("Realm -> Migration: Version: " + oldVersion + " to " + newVersion); }
@Override public void migrate(DynamicRealm dynamicRealm, long oldVersion, long newVersion) { Log.d("TEST", String.format("From %d to %d", oldVersion, newVersion)); }