Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-1360

Migration crashes when sorting the old migration by a field deleted in the new migration.

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None

      I changed the Realm schema and want to create new entities based on the previous ones. If for a new entity I use the same name that was in the previous scheme, then the migration crashes with this error:

       exception in notifier thread: N5realm10LogicErrorE: Column does not exist 
      

      If I rename the entity in the new migration to something other than "Routine", then everything goes correctly.

      I use Realm 10.8.0.

      Old schema:

      const OLD_SCHEMA = [
        {
          name: 'Routine',
          primaryKey: 'id',
          properties: {
            id: 'string',
            title: 'string',
            triggerDays: 'int[]',
            triggeredOn: 'string[]',
            tiles: 'RoutineTile[]',
            categories: 'string[]',
            builtInBehavior: { type: 'string', optional: true },
            createdAt: 'date',
          },
        },
        {
          name: 'RoutineTile',
          embedded: true,
          properties: {
            id: 'string',
            categories: 'string[]',
            title: 'string',
            duration: 'int',
            planningType: 'string',
            plannedTime: 'int[]',
            plannedEndTime: 'int[]',
          },
        },
      ];
      

      New schema:

      const NEW_SCHEMA = [
        {
          name: 'Routine',
          primaryKey: 'id',
          properties: {
            id: 'string',
            active: { type: 'bool', default: true },
            title: 'string',
            triggerDays: 'int[]',
            triggeredOn: 'string[]',
            categories: 'string[]',
            activityId: { type: 'string', optional: true },
            duration: 'int',
            builtInBehavior: { type: 'string', optional: true },
            position: 'int',
            plannedTime: 'int[]',
            plannedEndTime: 'int[]',
            planningType: 'string',
          },
        },
      ];
      

      Migration:

      function migration(oldRealm, newRealm) {
        let oldRoutines = oldRealm.objects('Routine').sorted('createdAt');
        let oldRoutineTiles = [];
        let newRoutines = [];
      
        // 1. Generate hashmap to get routine box from routine tile
        let routinesMapping = {};
        oldRoutines.forEach((oldRoutine) => {
          oldRoutineTiles = [...oldRoutineTiles, ...oldRoutine.tiles];
          oldRoutine.tiles.forEach((oldRoutineTile) => {
            routinesMapping[oldRoutineTile.id] = oldRoutine;
          });
        });
      
        // 2. Generate new routines
        let positionIndex = 0;
        for (let oldRoutineTileId in oldRoutineTiles) {
          let oldRoutineTile = oldRoutineTiles[oldRoutineTileId];
          let oldRoutine = routinesMapping[oldRoutineTile.id];
      
          if (!oldRoutine) {
            return;
          }
      
          newRoutines.push({
            activityId: null,
            id: oldRoutineTile.id,
            active: true,
            title: oldRoutineTile.title,
            position: positionIndex,
            triggerDays: oldRoutine.triggerDays,
            triggeredOn: oldRoutine.triggeredOn,
            categories: oldRoutineTile.categories,
            plannedTime: oldRoutineTile.plannedTime,
            plannedEndTime: oldRoutineTile.plannedEndTime,
            duration: oldRoutineTile.duration,
            builtInBehavior: oldRoutine.builtInBehavior,
            planningType: oldRoutineTile.planningType,
          });
      
          positionIndex++;
        }
      
        // Delete all old entities
        newRealm.delete(copyRoutines);
      
        // Generate new entities
        newRoutines.forEach((newRoutine) => {
          newRealm.create('Routine', newRoutine); // ⛔️ Crashes here
        });
      }
      

      I know that it would be hard to debug that just from that data, so could send the whole realm file if necessary.

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: