Rename Realm Table

I have a Gift table in my schema. Now I need to rename Gift.class to UserBonus.class and add some new parameters (and not a problem). What is the right way to do this?

I know that realm.getTable() can return a table to me, the problem is that the old Gift exists in the scheme, but in fact I do not have Gift.class (now it is UserBonus and getTable() will return me the new created table), therefore, I cannot get the old values โ€‹โ€‹of the gift table and move them to the new bonus table.

The only way I see this is to leave empty Gift.class and use it only for transferring.

Thanks for any advice
Yakiv

+6
source share
2 answers

Currently, I have implemented it as follows:

  • added new UserBonus.class
  • Added by @Deprecated on Gift.class
  • copy all data from the Gift table to the UserBonus table

     // added bonus types Table userBonus = realm.getTable(UserBonus.class); userBonus.addColumn(ColumnType.STRING, "localId"); userBonus.addColumn(ColumnType.INTEGER, "type"); userBonus.addColumn(ColumnType.INTEGER, "date"); userBonus.addColumn(ColumnType.STRING, "userName"); userBonus.addColumn(ColumnType.STRING, "userNumber"); userBonus.addColumn(ColumnType.STRING, "credits"); // move Gift to UserBonus RealmResults<Gift> gifts = realm.where(Gift.class).findAll(); for (Gift gift :gifts) { userBonus.add( gift.getLocalId(), UserBonus.TYPE_FRIEND, gift.getDate(), gift.getUserName(), gift.getUserNumber(), gift.getCredits() ); } realm.where(Gift.class).findAll().clear(); 
+2
source

Maybe my answer will be obsolete, but now in Realm on Android there is a 'rename' method that can rename a table (model) to a schema. The code will look like this:

 RealmSchema schema = Realm.getInstance().getSchema(); schema.rename("OldModelName", "NewModelName"); 

And it should work!

You can also see all the functions in Migration (from June 25, 2015) - https://github.com/realm/realm-java/pull/1239

+4
source

All Articles