Rewrite the database build function.