function updateTablesAndIndexes()

in public/dexie.js [1833:1909]


    function updateTablesAndIndexes(oldVersion, trans, idbtrans) {
      // Upgrade version to version, step-by-step from oldest to newest version.
      // Each transaction object will contain the table set that was current in that version (but also not-yet-deleted tables from its previous version)
      var queue = [];
      var oldVersionStruct = versions.filter(function (version) {
        return version._cfg.version === oldVersion;
      })[0];
      if (!oldVersionStruct)
        throw new exceptions.Upgrade(
          "Dexie specification of currently installed DB version is missing"
        );
      globalSchema = db._dbSchema = oldVersionStruct._cfg.dbschema;
      var anyContentUpgraderHasRun = false;
      var versToRun = versions.filter(function (v) {
        return v._cfg.version > oldVersion;
      });
      versToRun.forEach(function (version) {
        /// <param name="version" type="Version"></param>
        queue.push(function () {
          var oldSchema = globalSchema;
          var newSchema = version._cfg.dbschema;
          adjustToExistingIndexNames(oldSchema, idbtrans);
          adjustToExistingIndexNames(newSchema, idbtrans);
          globalSchema = db._dbSchema = newSchema;
          var diff = getSchemaDiff(oldSchema, newSchema);
          // Add tables
          diff.add.forEach(function (tuple) {
            createTable(idbtrans, tuple[0], tuple[1].primKey, tuple[1].indexes);
          });
          // Change tables
          diff.change.forEach(function (change) {
            if (change.recreate) {
              throw new exceptions.Upgrade(
                "Not yet support for changing primary key"
              );
            } else {
              var store = idbtrans.objectStore(change.name);
              // Add indexes
              change.add.forEach(function (idx) {
                addIndex(store, idx);
              });
              // Update indexes
              change.change.forEach(function (idx) {
                store.deleteIndex(idx.name);
                addIndex(store, idx);
              });
              // Delete indexes
              change.del.forEach(function (idxName) {
                store.deleteIndex(idxName);
              });
            }
          });
          if (version._cfg.contentUpgrade) {
            anyContentUpgraderHasRun = true;
            return Promise.follow(function () {
              version._cfg.contentUpgrade(trans);
            });
          }
        });
        queue.push(function (idbtrans) {
          if (!anyContentUpgraderHasRun || !hasIEDeleteObjectStoreBug) {
            var newSchema = version._cfg.dbschema;
            // Delete old tables
            deleteRemovedTables(newSchema, idbtrans);
          }
        });
      });
      // Now, create a queue execution engine
      function runQueue() {
        return queue.length
          ? Promise.resolve(queue.shift()(trans.idbtrans)).then(runQueue)
          : Promise.resolve();
      }
      return runQueue().then(function () {
        createMissingTables(globalSchema, idbtrans); // At last, make sure to create any missing tables. (Needed by addons that add stores to DB without specifying version)
      });
    }