Fix in tools:upgrade-sql, refs #5492

Bump the db version even if an upgrade is omitted.
Also, make sure that the upgrader doesn't get stuck.
Some cosmetic changes.