MDL-41062 WIP: fixes for freds comments
MDL-41062 upgradelib: improved efficiency by using a single query

Rather than doing two updates per duplicate grade item, do one.

Also added more assertions with out of order id/sortorder relationship