@@ -0,0 +1,1190 @@
+#!/bin/python%0Aimport MySQLdb%0Aimport argparse%0Aimport os%0A%0Aparser = argparse.ArgumentParser(description='Test documents of SugarCRM')%0Aparser.add_argument('--remove', type=bool, default=False,%0A    help='delete documents')%0Aargs = parser.parse_args()%0A%0AHOST = %22localhost%22%0AUSER = %22database_user%22%0APASSWD = %22database_password%22%0ADB = %22database%22%0ASUGARDIR = %22/var/www/sugarcrm/upload%22%0A%0Adb = MySQLdb.connect(host=HOST, user=USER, passwd=PASSWD, db=DB)%0Acur = db.cursor()%0A%0Acur.execute(%22SELECT id,filename FROM document_revisions%22)%0Arows = cur.fetchall()%0Ato_delete = %5B%5D%0Afor row in rows:%0A    if os.path.isfile(os.path.join(SUGARDIR, row%5B0%5D)):%0A        print %22File %25s (%25s) exists%22 %25 (row%5B0%5D, row%5B1%5D)%0A    else:%0A        print %22File %25s (%25s) doesn't exist!!%22 %25 (row%5B0%5D, row%5B1%5D)%0A        to_delete.append(row%5B0%5D)%0Acur.close()%0Aif args.remove:%0A    print %22Removing unexistent files...%22%0A    ids = %22%22%0A    for revision_id in to_delete:%0A        ids += %22'%25s',%22 %25 revision_id%0A    ids = ids%5B0:-1%5D%0A    sql = %22 delete from document_revisions where id in (%25s)%22 %25 ids%0A    cur.execute(sql)%0A    sql = %22 delete from documents d where not exists (select id from%22%0A    %22 document_revisions where document_id = d.id) %22%0A    cur.execute(sql)%0A

Add script to test if documents exist