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

52 lines
1.8 KiB
Python
Raw 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 -*-
"""
Перезаписывает HTML-блоки услуг, чек-апов, публикаций и страниц из дампа
после починки sqlread: раньше парсер отбрасывал слэш у escape-последовательностей,
и \r\n попадал в текст буквами "rn" — на странице это видно как мусор.
Обновляет только поле blocks по legacy_id. Ничего не создаёт и не удаляет.
"""
import sqlread, io
P = 'D:/medkor-local/tmp/dana/db.sql'
OUT = io.open('seed_fix_blocks.sql', 'w', encoding='utf-8')
def esc(s):
return "'" + str(s).replace("\\", "\\\\").replace("'", "''") + "'"
def blocks_sql(detail):
if not detail or not detail.strip() or detail == 'NULL':
return "'[]'::jsonb"
return "jsonb_build_array(jsonb_build_object('type','html','html'," + esc(detail) + "))"
# инфоблок -> (таблица, нужен ли фильтр по активности)
TARGETS = {
'30': 'services',
'50': 'checkups',
'27': 'posts',
'26': 'posts',
'28': 'posts',
}
PAGES = {'21': 'about', '18': 'history'}
OUT.write('BEGIN;\n')
counts = {}
for r in sqlread.rows(P, 'b_iblock_element'):
ib = r['IBLOCK_ID']
detail = r.get('DETAIL_TEXT')
if ib in TARGETS and r.get('ACTIVE') == 'Y':
table = TARGETS[ib]
OUT.write("UPDATE %s SET blocks=%s WHERE legacy_id=%s;\n" % (
table, blocks_sql(detail), r['ID']))
counts[table] = counts.get(table, 0) + 1
if ib in PAGES:
OUT.write("UPDATE pages SET blocks=%s WHERE slug=%s;\n" % (
blocks_sql(detail), esc(PAGES[ib])))
counts['pages'] = counts.get('pages', 0) + 1
OUT.write('COMMIT;\n')
OUT.close()
print('операций обновления:', counts)