Fix upgrade behavior to maintain env vars properly