ai-business-write/check_database_tenant_data.py

141 lines
5.2 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.

"""
检查数据库中的实际数据,查看有哪些 tenant_id 以及对应的数据量
"""
import pymysql
import os
from dotenv import load_dotenv
load_dotenv()
# 数据库连接配置
DB_CONFIG = {
'host': os.getenv('DB_HOST', '152.136.177.240'),
'port': int(os.getenv('DB_PORT', 5012)),
'user': os.getenv('DB_USER', 'finyx'),
'password': os.getenv('DB_PASSWORD', '6QsGK6MpePZDE57Z'),
'database': os.getenv('DB_NAME', 'finyx'),
'charset': 'utf8mb4'
}
def check_tenant_data():
"""检查各个表中的 tenant_id 数据"""
conn = pymysql.connect(**DB_CONFIG)
cursor = conn.cursor(pymysql.cursors.DictCursor)
try:
print("=" * 80)
print("检查数据库中的 tenant_id 数据")
print("=" * 80)
# 1. 检查 f_polic_field 表中的 tenant_id
print("\n1. f_polic_field 表中的 tenant_id 分布:")
cursor.execute("""
SELECT tenant_id,
COUNT(*) as total_count,
SUM(CASE WHEN field_type = 1 THEN 1 ELSE 0 END) as input_count,
SUM(CASE WHEN field_type = 2 THEN 1 ELSE 0 END) as output_count,
SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END) as enabled_count
FROM f_polic_field
GROUP BY tenant_id
ORDER BY tenant_id
""")
field_tenants = cursor.fetchall()
for row in field_tenants:
print(f" tenant_id: {row['tenant_id']}")
print(f" 总字段数: {row['total_count']}, 输入字段: {row['input_count']}, 输出字段: {row['output_count']}, 启用: {row['enabled_count']}")
# 2. 检查 f_polic_file_config 表中的 tenant_id
print("\n2. f_polic_file_config 表中的 tenant_id 分布:")
cursor.execute("""
SELECT tenant_id,
COUNT(*) as total_count,
SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END) as enabled_count
FROM f_polic_file_config
GROUP BY tenant_id
ORDER BY tenant_id
""")
config_tenants = cursor.fetchall()
for row in config_tenants:
print(f" tenant_id: {row['tenant_id']}")
print(f" 总模板数: {row['total_count']}, 启用: {row['enabled_count']}")
# 3. 检查 f_polic_file_field 表中的 tenant_id
print("\n3. f_polic_file_field 表中的 tenant_id 分布:")
cursor.execute("""
SELECT tenant_id,
COUNT(*) as total_count,
SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END) as enabled_count
FROM f_polic_file_field
GROUP BY tenant_id
ORDER BY tenant_id
""")
relation_tenants = cursor.fetchall()
for row in relation_tenants:
print(f" tenant_id: {row['tenant_id']}")
print(f" 总关联数: {row['total_count']}, 启用: {row['enabled_count']}")
# 4. 检查特定 tenant_id 的详细数据
test_tenant_id = 615873064429507600
print(f"\n4. 检查 tenant_id = {test_tenant_id} 的详细数据:")
# 字段数据
cursor.execute("""
SELECT COUNT(*) as count
FROM f_polic_field
WHERE tenant_id = %s
""", (test_tenant_id,))
field_count = cursor.fetchone()['count']
print(f" f_polic_field 表中的字段数: {field_count}")
if field_count > 0:
cursor.execute("""
SELECT id, name, filed_code, field_type, state
FROM f_polic_field
WHERE tenant_id = %s
LIMIT 10
""", (test_tenant_id,))
sample_fields = cursor.fetchall()
print(f" 示例字段前10条")
for field in sample_fields:
print(f" ID: {field['id']}, 名称: {field['name']}, 编码: {field['filed_code']}, 类型: {field['field_type']}, 状态: {field['state']}")
# 模板数据
cursor.execute("""
SELECT COUNT(*) as count
FROM f_polic_file_config
WHERE tenant_id = %s
""", (test_tenant_id,))
template_count = cursor.fetchone()['count']
print(f" f_polic_file_config 表中的模板数: {template_count}")
# 关联数据
cursor.execute("""
SELECT COUNT(*) as count
FROM f_polic_file_field
WHERE tenant_id = %s
""", (test_tenant_id,))
relation_count = cursor.fetchone()['count']
print(f" f_polic_file_field 表中的关联数: {relation_count}")
# 5. 检查所有不同的 tenant_id
print("\n5. 所有表中出现的 tenant_id 汇总:")
cursor.execute("""
SELECT DISTINCT tenant_id FROM f_polic_field
UNION
SELECT DISTINCT tenant_id FROM f_polic_file_config
UNION
SELECT DISTINCT tenant_id FROM f_polic_file_field
ORDER BY tenant_id
""")
all_tenants = cursor.fetchall()
print(" 所有 tenant_id 列表:")
for row in all_tenants:
print(f" {row['tenant_id']}")
finally:
cursor.close()
conn.close()
if __name__ == '__main__':
check_tenant_data()