Fix memory leak in database migration