Fix database initialisation and upgrade