move db operation out and fix test