This is an automated email from the ASF dual-hosted git repository.

potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 3598a52a2d Sanitize filenames in MySQLHook (#33328)
3598a52a2d is described below

commit 3598a52a2d1a7f9aef5e3777f1dd2a5823722aa2
Author: PApostol <50751110+papos...@users.noreply.github.com>
AuthorDate: Wed Jan 3 17:46:17 2024 +0200

    Sanitize filenames in MySQLHook (#33328)
---
 airflow/providers/mysql/hooks/mysql.py    | 20 ++++++-------------
 tests/providers/mysql/hooks/test_mysql.py | 33 ++++++++++---------------------
 2 files changed, 16 insertions(+), 37 deletions(-)

diff --git a/airflow/providers/mysql/hooks/mysql.py 
b/airflow/providers/mysql/hooks/mysql.py
index d7a1bfdd55..00ff92b62c 100644
--- a/airflow/providers/mysql/hooks/mysql.py
+++ b/airflow/providers/mysql/hooks/mysql.py
@@ -214,10 +214,8 @@ class MySqlHook(DbApiHook):
         conn = self.get_conn()
         cur = conn.cursor()
         cur.execute(
-            f"""
-            LOAD DATA LOCAL INFILE '{tmp_file}'
-            INTO TABLE {table}
-            """
+            f"LOAD DATA LOCAL INFILE %s INTO TABLE {table}",
+            (tmp_file,),
         )
         conn.commit()
         conn.close()  # type: ignore[misc]
@@ -227,10 +225,8 @@ class MySqlHook(DbApiHook):
         conn = self.get_conn()
         cur = conn.cursor()
         cur.execute(
-            f"""
-            SELECT * INTO OUTFILE '{tmp_file}'
-            FROM {table}
-            """
+            f"SELECT * INTO OUTFILE %s FROM {table}",
+            (tmp_file,),
         )
         conn.commit()
         conn.close()  # type: ignore[misc]
@@ -294,12 +290,8 @@ class MySqlHook(DbApiHook):
         cursor = conn.cursor()
 
         cursor.execute(
-            f"""
-            LOAD DATA LOCAL INFILE '{tmp_file}'
-            {duplicate_key_handling}
-            INTO TABLE {table}
-            {extra_options}
-            """
+            f"LOAD DATA LOCAL INFILE %s %s INTO TABLE {table} %s",
+            (tmp_file, duplicate_key_handling, extra_options),
         )
 
         cursor.close()
diff --git a/tests/providers/mysql/hooks/test_mysql.py 
b/tests/providers/mysql/hooks/test_mysql.py
index b4de3ce20f..271e249193 100644
--- a/tests/providers/mysql/hooks/test_mysql.py
+++ b/tests/providers/mysql/hooks/test_mysql.py
@@ -281,21 +281,11 @@ class TestMySqlHook:
 
     def test_bulk_load(self):
         self.db_hook.bulk_load("table", "/tmp/file")
-        self.cur.execute.assert_called_once_with(
-            """
-            LOAD DATA LOCAL INFILE '/tmp/file'
-            INTO TABLE table
-            """
-        )
+        self.cur.execute.assert_called_once_with("LOAD DATA LOCAL INFILE %s 
INTO TABLE table", ("/tmp/file",))
 
     def test_bulk_dump(self):
         self.db_hook.bulk_dump("table", "/tmp/file")
-        self.cur.execute.assert_called_once_with(
-            """
-            SELECT * INTO OUTFILE '/tmp/file'
-            FROM table
-            """
-        )
+        self.cur.execute.assert_called_once_with("SELECT * INTO OUTFILE %s 
FROM table", ("/tmp/file",))
 
     def test_serialize_cell(self):
         assert "foo" == self.db_hook._serialize_cell("foo", None)
@@ -310,14 +300,14 @@ class TestMySqlHook:
             IGNORE 1 LINES""",
         )
         self.cur.execute.assert_called_once_with(
-            """
-            LOAD DATA LOCAL INFILE '/tmp/file'
-            IGNORE
-            INTO TABLE table
-            FIELDS TERMINATED BY ';'
+            "LOAD DATA LOCAL INFILE %s %s INTO TABLE table %s",
+            (
+                "/tmp/file",
+                "IGNORE",
+                """FIELDS TERMINATED BY ';'
             OPTIONALLY ENCLOSED BY '"'
-            IGNORE 1 LINES
-            """
+            IGNORE 1 LINES""",
+            ),
         )
 
 
@@ -412,8 +402,5 @@ class TestMySql:
             hook.bulk_dump(table, tmp_file)
 
             assert mock_execute.call_count == 1
-            query = f"""
-                SELECT * INTO OUTFILE '{tmp_file}'
-                FROM {table}
-            """
+            query = f"SELECT * INTO OUTFILE %s FROM {table}"
             
assert_equal_ignore_multiple_spaces(mock_execute.call_args.args[0], query)

Reply via email to