I don't think adding a check to only run it once brings an advantage, the query always has the same result and there is no downside to running it every upgrade except the small cost of running the query.

On the other side, not having it executed because of some random error will lead to an unusable system.