# -*- coding: utf-8 -*- """备份订单链路相关表(写接口测试前置)。""" import json import os import re import pymysql from _root import ROOT YML = str(ROOT / "yoshop-java/src/main/resources/application.yml") pwd = re.findall(r"password:\s*(.+)", open(YML, encoding="utf-8").read())[0].strip().strip('"') conn = pymysql.connect(host="192.168.1.177", port=3306, user="root", password=pwd, database="shop", charset="utf8mb4") cur = conn.cursor() os.makedirs("backup", exist_ok=True) TABLES = [ "yoshop_order", "yoshop_order_goods", "yoshop_order_address", "yoshop_order_delivery", "yoshop_order_delivery_goods", "yoshop_order_refund", "yoshop_order_refund_address", "yoshop_order_refund_image", "yoshop_user", "yoshop_user_balance_log", "yoshop_user_points_log", "yoshop_user_coupon", "yoshop_goods", "yoshop_goods_sku", "yoshop_store_address", ] for t in TABLES: cur.execute("select * from " + t) cols = [d[0] for d in cur.description] rows = [dict(zip(cols, r)) for r in cur.fetchall()] json.dump(rows, open("backup/%s.json" % t, "w", encoding="utf-8"), ensure_ascii=False, default=str) print(t, len(rows)) conn.close()