Move migration code to after a successfull sync (so migrations are not attempted before the database has been created on fresh installs), #156