Closes #5065. Pulled the two helper functions from core into upgrade script.