Database upgrade fix

upgrade-database is having strange issues with operations not being atomic, breaking upgrades that span multiple versions.
Fixed by closing and reopening the connection to the database after each upgrade operation.