fix upgrade, need to apply at the transaction at the end of the loop