dana-medical-site/migrations/import/fix_review_texts.py
2026-10-14 13:33:13 +00:00

40 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
Чинит тексты отзывов, потерянные при первом переносе: clean() возвращал строку
"NULL" как непустую, поэтому подстановка из PREVIEW_TEXT не срабатывала и в
базу уходило литеральное "NULL" вместо текста.
Обновляет только поле text и только у отзывов, где сейчас лежит "NULL".
Статусы модерации, ответы клиники и оценки не трогает.
"""
import sqlread, io, re, html
P = 'D:/medkor-local/tmp/dana/db.sql'
OUT = io.open('seed_fix_reviews.sql', 'w', encoding='utf-8')
def esc(s):
return "'" + str(s).replace("\\", "\\\\").replace("'", "''") + "'"
def clean(s):
if not s or s == 'NULL':
return ''
s = re.sub(r'(?is)<[^>]+>', ' ', s)
s = html.unescape(s)
return re.sub(r'\s+', ' ', s).strip()
OUT.write('BEGIN;\n')
fixed = empty = 0
for r in sqlread.rows(P, 'b_iblock_element'):
if r['IBLOCK_ID'] != '23':
continue
text = clean(r.get('DETAIL_TEXT')) or clean(r.get('PREVIEW_TEXT'))
if not text:
empty += 1
continue
OUT.write("UPDATE reviews SET text=%s, updated_at=now() "
"WHERE legacy_id=%s AND text='NULL';\n" % (esc(text), r['ID']))
fixed += 1
OUT.write('COMMIT;\n')
OUT.close()
print('операций обновления:', fixed, '| без текста в источнике:', empty)