Skip to content

Commit

Permalink
slave获取报错bug修复 (#2839)
Browse files Browse the repository at this point in the history
* mongodb comment 检测问题修复

* 修复获取slave的bug

* lint

* edit mongo test
  • Loading branch information
blight19 authored Oct 15, 2024
1 parent b71b2c5 commit eb99790
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sql/engines/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ def _build_cmd(
else:
cmd_template = (
"{mongo} --quiet {auth_options} {host}:{port}/{auth_db} <<\\EOF\n"
"db=db.getSiblingDB('{db_name}');{slave_ok}printjson({sql})\nEOF"
"db=db.getSiblingDB('{db_name}');{slave_ok}{sql}\nEOF"
)
# 长度不超限直接mongo shell,无需临时文件
common_params["sql"] = sql
Expand Down Expand Up @@ -397,7 +397,7 @@ def get_slave(self):

sql = """var host=""; rs.status().members.forEach(function(item) {i=1; if (item.stateStr =="SECONDARY") \
{host=item.name } }); print(host);"""
slave_msg = self.exec_cmd(sql)
slave_msg = self.exec_cmd(sql, db_name=self.db_name)
# 如果是阿里云的云mongodb,会获取不到备节点真实的ip和端口,那就干脆不获取,直接用主节点来执行sql
# 如果是自建mongodb,获取到备节点的ip是192.168.1.33:27019这样的值;但如果是阿里云mongodb,获取到的备节点ip是SECONDARY、hiddenNode这样的值
# 所以,为了使代码更加通用,通过有无冒号来判断自建Mongod还是阿里云mongdb;没有冒号就判定为阿里云mongodb,直接返回false;
Expand Down
4 changes: 2 additions & 2 deletions sql/engines/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_build_cmd_without_load(mongo_engine):
# Expected command template
expected_cmd = (
"mongo --quiet -u test_user -p 'test_password' localhost:27017/admin <<\\EOF\n"
"db=db.getSiblingDB('test_db');rs.slaveOk();printjson(db.test_collection.find())\nEOF"
"db=db.getSiblingDB('test_db');rs.slaveOk();db.test_collection.find()\nEOF"
)

# Assertions
Expand All @@ -72,7 +72,7 @@ def test_build_cmd_without_auth(mongo_engine):
# Expected command template
expected_cmd = (
"mongo --quiet localhost:27017/admin <<\\EOF\n"
"db=db.getSiblingDB('test_db');rs.slaveOk();printjson(db.test_collection.find())\nEOF"
"db=db.getSiblingDB('test_db');rs.slaveOk();db.test_collection.find()\nEOF"
)

# Assertions
Expand Down

0 comments on commit eb99790

Please sign in to comment.