Fixed database upgrade bugs

upgrade-database is having strange issues with changes not occurring in
order, breaking the upgrade process if going across multiple versions.
Fixed by reopening database connection after each upgrade.