fix statusflag db query bugs and unittests