Hhm – may be I am blind, but I can't figure out, what should be wrong. The intention is to set next_upgrade only if neither next_upgrade is already set nor "not-upgradable" has been set. Just to be sure, I refactored my code to make it testable and the tests show that the code works as intended.
So could you please explain, why do think the code is wrong?
P.S.: Due to the changes in the last version of my mod, none of of yours should be affected longer as they aren't part of the whitelist for prototypes to be adapted