Python 3.* port of rebuild_db