function State.load()

in kong/kong/db/migrations/state.lua [183:304]


function State.load(db)

  log.debug("loading subsystems migrations...")

  local subsystems, err = load_subsystems(db, db.kong_config.loaded_plugins)
  if not subsystems then
    return nil, prefix_err(db, err)
  end

  log.verbose("retrieving %s schema state...", db.infos.db_desc)

  local ok, err = db.connector:connect_migrations({ no_keyspace = true })
  if not ok then
    return nil, prefix_err(db, err)
  end

  local rows, err = db.connector:schema_migrations(subsystems)
  if err then
    db.connector:close()
    return nil, prefix_err(db, "failed to check schema state: " .. err)
  end

  db.connector:close()

  log.verbose("schema state retrieved")

  local schema_state = {
    needs_bootstrap = false,
    executed_migrations = nil,
    pending_migrations = nil,
    missing_migrations = nil,
    new_migrations = nil,
  }

  local rows_as_hash = {}

  if not rows then
    schema_state.needs_bootstrap = true

  else
    for _, row in ipairs(rows) do
      rows_as_hash[row.subsystem] = {
        last_executed = row.last_executed,
        executed = value_or_empty_table(row.executed),
        pending = value_or_empty_table(row.pending),
      }
    end
  end

  for _, subsystem in ipairs(subsystems) do
    local subsystem_state = {
      executed_migrations = {},
      pending_migrations = {},
      missing_migrations = {},
      new_migrations = {},
    }

    if not rows_as_hash[subsystem.name] then
      
      
      for i, mig in ipairs(subsystem.migrations) do
        subsystem_state.new_migrations[i] = mig
      end

    else
      

      local n

      for i, mig in ipairs(subsystem.migrations) do
        if mig.name == rows_as_hash[subsystem.name].last_executed then
          n = i + 1
        end

        local found

        for _, db_mig in ipairs(rows_as_hash[subsystem.name].executed) do
          if mig.name == db_mig then
            found = true
            table.insert(subsystem_state.executed_migrations, mig)
            break
          end
        end

        if not found then
          for _, db_mig in ipairs(rows_as_hash[subsystem.name].pending) do
            if mig.name == db_mig then
              found = true
              table.insert(subsystem_state.pending_migrations, mig)
              break
            end
          end
        end

        if not found then
          if not n or i >= n then
            table.insert(subsystem_state.new_migrations, mig)

          else
            table.insert(subsystem_state.missing_migrations, mig)
          end
        end
      end
    end

    for k, v in pairs(subsystem_state) do
      if #v > 0 then
        if not schema_state[k] then
          schema_state[k] = setmetatable({}, Migrations_mt)
        end

        table.insert(schema_state[k], {
            subsystem = subsystem.name,
            namespace = subsystem.namespace,
            migrations = v,
          })
      end
    end
  end

  return setmetatable(schema_state, State)
end