public static Table initTable(ImplicitTransaction transaction) {
   if (!transaction.hasTable("class_SatsClass")) {
     Table table = transaction.getTable("class_SatsClass");
     table.addColumn(ColumnType.STRING, "id");
     table.addColumn(ColumnType.STRING, "centerFilterId");
     table.addColumn(ColumnType.STRING, "classTypeId");
     table.addColumn(ColumnType.INTEGER, "durationInMinutes");
     table.addColumn(ColumnType.STRING, "instructorId");
     table.addColumn(ColumnType.STRING, "name");
     table.addColumn(ColumnType.STRING, "startTime");
     table.addColumn(ColumnType.INTEGER, "bookedPersonsCount");
     table.addColumn(ColumnType.INTEGER, "maxPersonsCount");
     table.addColumn(ColumnType.STRING, "regionId");
     table.addColumn(ColumnType.INTEGER, "waitingListCount");
     if (!transaction.hasTable("class_ClassCategoryIds")) {
       ClassCategoryIdsRealmProxy.initTable(transaction);
     }
     table.addColumnLink(
         ColumnType.LINK_LIST, "classCategoryIds", transaction.getTable("class_ClassCategoryIds"));
     table.setIndex(table.getColumnIndex("id"));
     table.setPrimaryKey("id");
     return table;
   }
   return transaction.getTable("class_SatsClass");
 }
Пример #2
0
  @Override
  public long execute(Realm realm, long version) {

    /*
        // Version 0
        class Person
            String firstName;
            String lastName;
            int    age;

        // Version 1
        class Person
            String fullName;        // combine firstName and lastName into single field
            int age;
    */

    // Migrate from version 0 to version 1
    if (version == 0) {
      Table personTable = realm.getTable(Person.class);

      long fistNameIndex = getIndexForProperty(personTable, "firstName");
      long lastNameIndex = getIndexForProperty(personTable, "lastName");
      long fullNameIndex = personTable.addColumn(ColumnType.STRING, "fullName");
      for (int i = 0; i < personTable.size(); i++) {
        personTable.setString(
            fullNameIndex,
            i,
            personTable.getString(fistNameIndex, i)
                + " "
                + personTable.getString(lastNameIndex, i));
      }
      personTable.removeColumn(getIndexForProperty(personTable, "firstName"));
      personTable.removeColumn(getIndexForProperty(personTable, "lastName"));
      version++;
    }

    /*
        // Version 2
            class Pet                   // add a new model class
                String name;
                String type;

            class Person
                String fullName;
                int age;
                RealmList<Pet> pets;    // add an array property

    */
    // Migrate from version 1 to version 2
    if (version == 1) {
      Table personTable = realm.getTable(Person.class);
      Table petTable = realm.getTable(Pet.class);
      long nameColumnIndex = petTable.addColumn(ColumnType.STRING, "name");
      long typeColumnIndex = petTable.addColumn(ColumnType.STRING, "type");
      long petsIndex = personTable.addColumnLink(ColumnType.LINK_LIST, "pets", petTable);
      long fullNameIndex = getIndexForProperty(personTable, "fullName");

      for (int i = 0; i < personTable.size(); i++) {
        if (personTable.getString(fullNameIndex, i).equals("JP McDonald")) {
          long rowIndex = petTable.addEmptyRow();
          petTable.setString(nameColumnIndex, rowIndex, "Jimbo");
          petTable.setString(typeColumnIndex, rowIndex, "dog");
          personTable.getUncheckedRow(i).getLinkList(petsIndex).add(rowIndex);
        }
      }
      version++;
    }

    /*
        // Version 3
            class Pet
                String name;
                int type;               // type becomes int

            class Person
                String fullName;
                RealmList<Pet> pets;    // age and pets re-ordered
                int age;
    */
    // Migrate from version 2 to version 3
    if (version == 2) {
      Table petTable = realm.getTable(Pet.class);
      long oldTypeIndex = getIndexForProperty(petTable, "type");
      long typeIndex = petTable.addColumn(ColumnType.INTEGER, "type");
      for (int i = 0; i < petTable.size(); i++) {
        String type = petTable.getString(oldTypeIndex, i);
        if (type.equals("dog")) {
          petTable.setLong(typeIndex, i, 1);
        } else if (type.equals("cat")) {
          petTable.setLong(typeIndex, i, 2);
        } else if (type.equals("hamster")) {
          petTable.setLong(typeIndex, i, 3);
        }
      }
      petTable.removeColumn(oldTypeIndex);
      version++;
    }
    return version;
  }