Sounds like instead of coding it so it changes the maximum health in a city, it should be coded as an increase to maximum health. For example, if X = Normal Max HP:
Instead of changing X from 200 to 500, it should be coded as X + Y, with Y being 300. Then the unit's actual maximum HP would increase to 500 inside the city, but the game would also be able to continue to reference X (200) as the normal maximum, and thus the unit would still have 12 troops.
As for leveling up as per the OP, I agree it should be percentage based. If the max HP increases from 200 to 500 from leveling up, and not from a city bonus, and the unit has 199 HP before the level up, then it should be 199 * (500/200) = HP after leveling up. I'd truncate the decimal, and thus you'd have 497/500 HP.