0

What i want to achieve is following things. At some point i want do some updates on my data and database. What i want to do is to drop a table and repopulate it again, in order to keep the id-s same for some of the items

Today i populate a table like this

suspend fun insertItemsIfTableIsEmpty() = withContext(Dispatchers.IO) {
        if (itemsBoxStore.count() == 0L) {
            insertItems(items = Items.all)
        }
    }

What i want to achieve at some point is to drop the old table and repopulate with new items. Some of those are identical, few are different and some are new.

After that i want to keep that table untouched and initiate as in the code above. I know how to do it for sql and room, but not for objectBox. The items are quite complex and have one-to-many and one-to-one relations with other classes not mentioned here. Those other classes are not touched

Drop an old table and create a new. But it failed

  • When you say "drop the old table and repopulate with new items" you probably refer to deleting all objects and inserting new ones, right? (Dropping a table is something else in SQL.) – Markus Junginger Feb 06 '23 at 11:26
  • "in order to keep the id-s same for some of the items" this seems under-defined. It sound like you do not want to delete all objects, but keep some to keep the IDs. – Markus Junginger Feb 06 '23 at 11:29
  • @MarkusJunginger Yes, i want to delete all objects and inserting new ones. Since other items refer to the old ones, how do i ensure that they get the same id as before (item1 same, item2 same, item3 same but maybe some property has another value, item4 same, item5 new – user17035623 Feb 06 '23 at 14:56
  • Do not delete objects that you want to preserve, but put them to update (or at least use the ID of the existing object to overwrite). Otherwise the relation information is lost and no magic can recreate them. – Markus Junginger Feb 07 '23 at 20:30
  • @MarkusJunginger I understand. My question is if there is any recommended way to perform some kind of migration similar to what you do in sql – user17035623 Feb 08 '23 at 17:18
  • What exactly would you do in SQL? – Markus Junginger Feb 08 '23 at 20:21
  • @MarkusJunginger In android for example you have public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // If you need to add a column if (newVersion > oldVersion) { db.execSQL("remove_some_table"); dbexSQ("add_run_some_Scripts" } } – user17035623 Feb 09 '23 at 14:43
  • @MarkusJunginger In android you have for example .addMigration() and can execute different migratioins, like delele some old table, create some new and also prepopulate it with some default values. For example MIGRATION_13_14 can drop a table, replace it with a new and also prepulate with some default objects – user17035623 Feb 09 '23 at 14:50
  • OK, you are talking about schema migration. Maybe clarify in a new question what exactly you want to do with some examples. – Markus Junginger Feb 09 '23 at 16:27
  • 1
    @MarkusJunginger I will. Off topic but i love objectbox. Compare to room this is really really much better – user17035623 Feb 09 '23 at 18:43

0 Answers0