db: build a new db first then overwrite the old one