This script is always ran by an administrator so it doesn't have to ignore access. But is it possible that there are comments under disabled entities? Should the upgrade use access_show_hidden_entities(true)?