Create infra scripts
This commit is contained in:
111
uv_app/user/debug_client_101460.py
Normal file
111
uv_app/user/debug_client_101460.py
Normal file
@@ -0,0 +1,111 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Dict, Optional
|
||||
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
|
||||
from uv_app.core.mssql import connect_to_mssql
|
||||
from uv_app.core.pgsql import connect_to_pgsql
|
||||
|
||||
QUERY_PATH = Path(__file__).with_name("routeriai_query.sql")
|
||||
CLIENT_CODE = "101460"
|
||||
|
||||
|
||||
def _read_query() -> str:
|
||||
return QUERY_PATH.read_text(encoding="utf-8")
|
||||
|
||||
|
||||
def _clean_text(value: object) -> Optional[str]:
|
||||
if value is None:
|
||||
return None
|
||||
text = value.decode(errors="ignore") if isinstance(value, bytes) else str(value)
|
||||
text = text.replace("\u00a0", " ")
|
||||
text = re.sub(r"[\x00-\x1f\x7f\u2028\u2029\u0085]", " ", text)
|
||||
return re.sub(r"\s+", " ", text).strip()
|
||||
|
||||
|
||||
def _format_diff_value(value: str) -> str:
|
||||
return (
|
||||
value.replace("\r", "\\r")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\t", "\\t")
|
||||
.replace("\u2028", "\\u2028")
|
||||
.replace("\u2029", "\\u2029")
|
||||
.replace("\u0085", "\\u0085")
|
||||
)
|
||||
|
||||
|
||||
def _dump_string(label: str, value: object) -> None:
|
||||
if value is None:
|
||||
print(f"{label}: None")
|
||||
return
|
||||
text = value.decode(errors="ignore") if isinstance(value, bytes) else str(value)
|
||||
print(f"{label} raw repr: {text!r}")
|
||||
print(f"{label} raw escaped: {_format_diff_value(text)}")
|
||||
cleaned = _clean_text(text)
|
||||
print(f"{label} cleaned repr: {cleaned!r}")
|
||||
print(f"{label} cleaned escaped: {_format_diff_value(cleaned or '')}")
|
||||
codepoints = [f"U+{ord(ch):04X}" for ch in text]
|
||||
print(f"{label} codepoints: {' '.join(codepoints)}")
|
||||
|
||||
|
||||
def _fetch_pg_client() -> Optional[Dict[str, object]]:
|
||||
conn = connect_to_pgsql()
|
||||
if conn is None:
|
||||
raise RuntimeError("Failed to connect to PostgreSQL.")
|
||||
try:
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cursor:
|
||||
cursor.execute(_read_query())
|
||||
rows = cursor.fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
for row in rows:
|
||||
if str(row.get("client_code") or "").strip() == CLIENT_CODE:
|
||||
return row
|
||||
return None
|
||||
|
||||
|
||||
def _fetch_mssql_client() -> Optional[Dict[str, object]]:
|
||||
conn = connect_to_mssql()
|
||||
if conn is None:
|
||||
raise RuntimeError("Failed to connect to MSSQL.")
|
||||
try:
|
||||
query = """
|
||||
SELECT
|
||||
N08_KODAS_KS,
|
||||
N08_PAV
|
||||
FROM dbo.N08_KLIJ
|
||||
WHERE N08_KODAS_KS = ?
|
||||
"""
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(query, (CLIENT_CODE,))
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
return None
|
||||
columns = [c[0] for c in cursor.description]
|
||||
return dict(zip(columns, row))
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
pg_row = _fetch_pg_client()
|
||||
if not pg_row:
|
||||
print("Client not found in PostgreSQL.")
|
||||
else:
|
||||
print("PostgreSQL data:")
|
||||
_dump_string("PG name", pg_row.get("name"))
|
||||
|
||||
mssql_row = _fetch_mssql_client()
|
||||
if not mssql_row:
|
||||
print("Client not found in MSSQL.")
|
||||
else:
|
||||
print("MSSQL data:")
|
||||
_dump_string("MSSQL N08_PAV", mssql_row.get("N08_PAV"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,6 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import re
|
||||
from datetime import date, datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, Iterable, Optional, Tuple
|
||||
@@ -108,6 +109,19 @@ def _normalize_person_type(value: Optional[object]) -> str:
|
||||
return "1"
|
||||
|
||||
|
||||
def _clean_text(value: object) -> Optional[str]:
|
||||
if value is None:
|
||||
return None
|
||||
if isinstance(value, bytes):
|
||||
text = value.decode(errors="ignore")
|
||||
else:
|
||||
text = str(value)
|
||||
text = text.replace("\u00a0", " ")
|
||||
# Drop control chars (including C1) and unicode line/paragraph separators.
|
||||
text = re.sub(r"[\x00-\x1f\x7f-\x9f\u2028\u2029]", " ", text)
|
||||
return re.sub(r"\s+", " ", text).strip()
|
||||
|
||||
|
||||
def _build_n08_payload(row: Dict[str, object]) -> Dict[str, object]:
|
||||
mobile = (row.get("mobile_phone") or "").strip()
|
||||
phone = (row.get("phone") or "").strip()
|
||||
@@ -129,9 +143,11 @@ def _build_n08_payload(row: Dict[str, object]) -> Dict[str, object]:
|
||||
if isinstance(creation_date, (date, datetime)):
|
||||
creation_date = creation_date.isoformat()
|
||||
|
||||
name = _clean_text(row.get("name"))
|
||||
|
||||
return {
|
||||
"N08_KODAS_KS": row.get("client_code"),
|
||||
"N08_PAV": row.get("name"),
|
||||
"N08_PAV": name,
|
||||
"N08_ADR": row.get("address"),
|
||||
"N08_E_MAIL": row.get("email"),
|
||||
"N08_ADD_DATE": creation_date,
|
||||
@@ -163,6 +179,17 @@ def _normalize_value(value: object) -> str:
|
||||
return str(value).strip()
|
||||
|
||||
|
||||
def _format_diff_value(value: str) -> str:
|
||||
return (
|
||||
value.replace("\r", "\\r")
|
||||
.replace("\n", "\\n")
|
||||
.replace("\t", "\\t")
|
||||
.replace("\u2028", "\\u2028")
|
||||
.replace("\u2029", "\\u2029")
|
||||
.replace("\u0085", "\\u0085")
|
||||
)
|
||||
|
||||
|
||||
def _diff_fields(
|
||||
existing: Optional[Dict[str, object]],
|
||||
desired: Dict[str, object],
|
||||
@@ -207,7 +234,9 @@ def _upsert_client(
|
||||
return
|
||||
print(f"Updating client: {client_code} ({index}/{total})")
|
||||
for field, (old, new) in changes.items():
|
||||
print(f" {field}: '{old}' -> '{new}'")
|
||||
old_fmt = _format_diff_value(old)
|
||||
new_fmt = _format_diff_value(new)
|
||||
print(f" {field}: '{old_fmt}' -> '{new_fmt}'")
|
||||
payload = {
|
||||
"method": "EDIT_N08",
|
||||
"params": {
|
||||
@@ -221,7 +250,8 @@ def _upsert_client(
|
||||
else:
|
||||
print(f"Creating client: {client_code} ({index}/{total})")
|
||||
for field, (_, new) in changes.items():
|
||||
print(f" {field}: '' -> '{new}'")
|
||||
new_fmt = _format_diff_value(new)
|
||||
print(f" {field}: '' -> '{new_fmt}'")
|
||||
payload = {
|
||||
"method": "EDIT_N08_FULL",
|
||||
"params": {"oper": "I", "user": user},
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
SELECT
|
||||
k.kodas AS client_code,
|
||||
trim(concat_ws(' ', k.vardas, k.pavarde)) AS name,
|
||||
regexp_replace(
|
||||
trim(concat_ws(' ', k.vardas, k.pavarde)),
|
||||
'\s+',
|
||||
' ',
|
||||
'g'
|
||||
) AS name,
|
||||
trim(concat_ws(', ',
|
||||
concat_ws(' ', g.pav, k.namas, NULLIF(NULLIF(k.butas::text, '0'), '')),
|
||||
m.pav,
|
||||
|
||||
Reference in New Issue
Block a user