fix: Redis target_database隔离修复 - _parse_redis_commands使用ticket.target_database作为默认db编号,修复简单文本格式写死db=0的bug,确保工单提交到db1时数据正确写入db1
This commit is contained in:
1 parent
b92c3399e1
commit
8f962a988f
2 files changed
+25
-8
No files matched your search
@@ -1094,12 +1094,17 @@ async def _send_ticket_notification(
|
|||||||
instance_name = await _get_instance_name(db, ticket.instance_id)
|
instance_name = await _get_instance_name(db, ticket.instance_id)
|
||||||
sql_preview = ticket.sql_content[:100] + "..." if len(ticket.sql_content) > 100 else ticket.sql_content
|
sql_preview = ticket.sql_content[:100] + "..." if len(ticket.sql_content) > 100 else ticket.sql_content
|
||||||
|
|
||||||
|
# Redis database 格式化: "0" → "db0"
|
||||||
|
target_db = ticket.target_database or ""
|
||||||
|
if ticket.db_type == "redis" and target_db.isdigit():
|
||||||
|
target_db = f"db{target_db}"
|
||||||
|
|
||||||
notification_data = {
|
notification_data = {
|
||||||
"title": ticket.title,
|
"title": ticket.title,
|
||||||
"ticket_id": ticket.id,
|
"ticket_id": ticket.id,
|
||||||
"ticket_no": ticket.ticket_no or str(ticket.id),
|
"ticket_no": ticket.ticket_no or str(ticket.id),
|
||||||
"db_type": ticket.db_type,
|
"db_type": ticket.db_type,
|
||||||
"target_database": ticket.target_database or "",
|
"target_database": target_db,
|
||||||
"instance_name": instance_name,
|
"instance_name": instance_name,
|
||||||
"sql_preview": sql_preview,
|
"sql_preview": sql_preview,
|
||||||
"applicant": applicant_name,
|
"applicant": applicant_name,
|
||||||
@@ -1141,12 +1146,17 @@ async def _send_execution_notification(
|
|||||||
applicant_name = await _get_user_name(db, ticket.applicant_id)
|
applicant_name = await _get_user_name(db, ticket.applicant_id)
|
||||||
sql_preview = ticket.sql_content[:100] + "..." if len(ticket.sql_content) > 100 else ticket.sql_content
|
sql_preview = ticket.sql_content[:100] + "..." if len(ticket.sql_content) > 100 else ticket.sql_content
|
||||||
|
|
||||||
|
# Redis database 格式化: "0" → "db0"
|
||||||
|
target_db = ticket.target_database or ""
|
||||||
|
if ticket.db_type == "redis" and target_db.isdigit():
|
||||||
|
target_db = f"db{target_db}"
|
||||||
|
|
||||||
notification_data = {
|
notification_data = {
|
||||||
"title": ticket.title,
|
"title": ticket.title,
|
||||||
"ticket_id": ticket.id,
|
"ticket_id": ticket.id,
|
||||||
"ticket_no": ticket.ticket_no or str(ticket.id),
|
"ticket_no": ticket.ticket_no or str(ticket.id),
|
||||||
"db_type": ticket.db_type,
|
"db_type": ticket.db_type,
|
||||||
"target_database": ticket.target_database or "",
|
"target_database": target_db,
|
||||||
"instance_name": instance_name,
|
"instance_name": instance_name,
|
||||||
"sql_preview": sql_preview,
|
"sql_preview": sql_preview,
|
||||||
"applicant": applicant_name,
|
"applicant": applicant_name,
|
||||||
|
|||||||
@@ -520,7 +520,7 @@ class TicketExecutor:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# 解析命令
|
# 解析命令
|
||||||
commands = self._parse_redis_commands(ticket.sql_content)
|
commands = self._parse_redis_commands(ticket.sql_content, ticket.target_database)
|
||||||
if not commands:
|
if not commands:
|
||||||
report.status = "failed"
|
report.status = "failed"
|
||||||
report.errors.append("未解析到有效的 Redis 命令")
|
report.errors.append("未解析到有效的 Redis 命令")
|
||||||
@@ -1552,13 +1552,14 @@ class TicketExecutor:
|
|||||||
return row or 0
|
return row or 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse_redis_commands(content: str) -> List[Dict[str, Any]]:
|
def _parse_redis_commands(content: str, target_database: str = "0") -> List[Dict[str, Any]]:
|
||||||
"""解析 Redis 工单内容为命令列表。
|
"""解析 Redis 工单内容为命令列表。
|
||||||
|
|
||||||
支持 JSON 格式和简单文本格式。
|
支持 JSON 格式和简单文本格式。
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
content: 工单内容
|
content: 工单内容
|
||||||
|
target_database: 目标 DB 编号(来自 ticket.target_database)
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
命令列表,每项为 {"command": str, "db": int}
|
命令列表,每项为 {"command": str, "db": int}
|
||||||
@@ -1567,6 +1568,12 @@ class TicketExecutor:
|
|||||||
if not content:
|
if not content:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
# 解析默认 db 编号
|
||||||
|
try:
|
||||||
|
default_db = int(target_database)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
default_db = 0
|
||||||
|
|
||||||
# 尝试 JSON 解析
|
# 尝试 JSON 解析
|
||||||
if content.startswith("{"):
|
if content.startswith("{"):
|
||||||
try:
|
try:
|
||||||
@@ -1577,14 +1584,14 @@ class TicketExecutor:
|
|||||||
if isinstance(cmd, dict) and "command" in cmd:
|
if isinstance(cmd, dict) and "command" in cmd:
|
||||||
commands.append({
|
commands.append({
|
||||||
"command": cmd["command"],
|
"command": cmd["command"],
|
||||||
"db": cmd.get("db", 0),
|
"db": cmd.get("db", default_db),
|
||||||
})
|
})
|
||||||
elif isinstance(cmd, str):
|
elif isinstance(cmd, str):
|
||||||
commands.append({"command": cmd, "db": 0})
|
commands.append({"command": cmd, "db": default_db})
|
||||||
return commands
|
return commands
|
||||||
# 可能是单条命令的 JSON
|
# 可能是单条命令的 JSON
|
||||||
if "command" in data:
|
if "command" in data:
|
||||||
return [{"command": data["command"], "db": data.get("db", 0)}]
|
return [{"command": data["command"], "db": data.get("db", default_db)}]
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
pass # 不是 JSON,尝试文本格式
|
pass # 不是 JSON,尝试文本格式
|
||||||
|
|
||||||
@@ -1593,7 +1600,7 @@ class TicketExecutor:
|
|||||||
for line in content.split("\n"):
|
for line in content.split("\n"):
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
if line and not line.startswith("#"):
|
if line and not line.startswith("#"):
|
||||||
commands.append({"command": line, "db": 0})
|
commands.append({"command": line, "db": default_db})
|
||||||
|
|
||||||
return commands
|
return commands
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user