import re import subprocess cmd = "mysql -h sh-cdb-6fzlwnms.sql.tencentcdb.com -P 63912 -u assistant_prod -p'U%$4Tu_C3+4' -e 'SET NAMES utf8mb4; SELECT id, session_id FROM ai_assistant.archive_messages WHERE session_id LIKE \"%[%\";' 2>&1 | grep -v Warning" result = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) lines = result.stdout.decode('utf-8').strip().split('\n') print('Total lines:', len(lines)) fixed = 0 for line in lines: if not line.strip() or line.startswith('id') or '\t' not in line: continue parts = line.split('\t') if len(parts) < 2: continue id_str, session_id = parts[0], parts[1] # Replace [\"xxx\"] -> xxx or [xxx] -> xxx new_id = re.sub(r'\[\s*"([^"]+)"\s*\]', r'\1', session_id) new_id = re.sub(r'\[\s*([^\]]+)\s*\]', r'\1', new_id) if new_id != session_id: sql = "UPDATE ai_assistant.archive_messages SET session_id='%s' WHERE id=%s;" % (new_id, id_str) subprocess.run(['mysql', '-h', 'sh-cdb-6fzlwnms.sql.tencentcdb.com', '-P', '63912', '-u', 'assistant_prod', '-pU%$4Tu_C3+4', '-e', sql], stdout=subprocess.PIPE, stderr=subprocess.PIPE) fixed += 1 if fixed % 100 == 0: print('Fixed %d...' % fixed) print('Done. Fixed=%d' % fixed)