From 31f8287c4fc8e8f974a4259d65437b42a57b9c1a Mon Sep 17 00:00:00 2001 From: Tanmoy Sarkar <57363826+tanmoysrt@users.noreply.github.com> Date: Mon, 21 Oct 2024 08:36:37 +0000 Subject: [PATCH] feat: send failed query to request as well --- agent/database.py | 3 ++- agent/site.py | 12 +++++++++--- agent/web.py | 3 +-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/agent/database.py b/agent/database.py index 5c93ce70..85f670e3 100644 --- a/agent/database.py +++ b/agent/database.py @@ -96,8 +96,9 @@ def _sql(self, query: str, params=(), commit: bool = False, as_dict: bool = Fals with self.db.atomic() as transaction: try: for q in queries: + self.last_executed_query = q if not commit and self._is_restricted_query_for_no_commit_mode(q): - raise ProgrammingError("Provided query is not allowed in read only mode") + raise ProgrammingError(f"Provided query is not allowed in read only mode") output = None row_count = None cursor = self.db.execute_sql(q, params) diff --git a/agent/site.py b/agent/site.py index 5fe20870..c3d1034d 100644 --- a/agent/site.py +++ b/agent/site.py @@ -832,9 +832,15 @@ def get_database_table_schemas(self): return tables def run_sql_query(self, query: str, commit: bool = False, as_dict: bool = False): - return Database(self.host, 3306, self.user, self.password, self.database).execute_query( - query, commit=commit, as_dict=as_dict - ) + database = Database(self.host, 3306, self.user, self.password, self.database) + success, output = database.execute_query(query, commit=commit, as_dict=as_dict) + response = { + "success": success, + "data": output + } + if not success and hasattr(database, "last_executed_query"): + response["failed_query"] = database.last_executed_query + return response @property def job_record(self): diff --git a/agent/web.py b/agent/web.py index 45f588a2..3c0ecfd5 100644 --- a/agent/web.py +++ b/agent/web.py @@ -561,8 +561,7 @@ def run_sql(bench, site): query = request.json.get("query") commit = request.json.get("commit") or False as_dict = request.json.get("as_dict") or False - success, data = Server().benches[bench].sites[site].run_sql_query(query, commit, as_dict) - return {"success": success, "data": data} + return Server().benches[bench].sites[site].run_sql_query(query, commit, as_dict) @application.route(