Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix get_instance_users_summary IndexError #2736

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sql/engines/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,10 @@ def get_instance_users_summary(self):
self.server_fork_type == MysqlForkType.MARIADB
and self.server_version >= (10, 4, 2)
):
support_account_lock = True
sql_get_user = sql_get_user_with_account_locked
else:
support_account_lock = False
sql_get_user = sql_get_user_without_account_locked
query_result = self.query("mysql", sql_get_user)
if query_result.error and sql_get_user == sql_get_user_with_account_locked:
Expand All @@ -398,7 +400,11 @@ def get_instance_users_summary(self):
"host": db_user[2],
"privileges": user_priv,
"saved": False,
"is_locked": db_user[3] if server_version >= (5, 7, 6) else None,
"is_locked": (
db_user[3]
if support_account_lock and len(db_user) == 4
else None
),
}
rows.append(row)
query_result.rows = rows
Expand Down
44 changes: 44 additions & 0 deletions sql/engines/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,50 @@ def test_get_instance_users_summary(self, _query, _connect):
user_summary = new_engine.get_instance_users_summary()
self.assertEqual(user_summary.error, "query error")

result_with_lock = ResultSet()
result_with_lock.rows = [("'root'@'localhost'", "root", "localhost", "N")]
_query.return_value = result_with_lock
_connect.return_value.get_server_info.return_value = "5.7.20-16log"
self.assertTupleEqual(new_engine.server_version, (5, 7, 20))
user_summary_with_lock = new_engine.get_instance_users_summary()
self.assertEqual(
user_summary_with_lock.rows,
[
{
"host": "localhost",
"is_locked": "N",
"privileges": [("'root'@'localhost'", "root", "localhost", "N")],
"saved": False,
"user": "root",
"user_host": "'root'@'localhost'",
}
],
)

@patch("MySQLdb.connect")
@patch.object(MysqlEngine, "query")
def test_get_instance_users_summary_without_lock(self, _query, _connect):
result_without_lock = ResultSet()
result_without_lock.rows = [("'root'@'localhost'", "root", "localhost")]
_query.return_value = result_without_lock
mysql_engine = MysqlEngine(instance=self.ins1)
_connect.return_value.get_server_info.return_value = "5.7.5-16log"
self.assertTupleEqual(mysql_engine.server_version, (5, 7, 5))
user_summary_without_lock = mysql_engine.get_instance_users_summary()
self.assertEqual(
user_summary_without_lock.rows,
[
{
"host": "localhost",
"is_locked": None,
"privileges": [("'root'@'localhost'", "root", "localhost")],
"saved": False,
"user": "root",
"user_host": "'root'@'localhost'",
}
],
)

@patch("MySQLdb.connect")
@patch.object(MysqlEngine, "execute")
def test_create_instance_user(self, _execute, _connect):
Expand Down
Loading