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

Use unused SQLCheckOperator.parameters in SQLCheckOperator.execute. #27599

Merged
merged 7 commits into from
Nov 14, 2022
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
12 changes: 10 additions & 2 deletions airflow/providers/common/sql/operators/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ class SQLCheckOperator(BaseSQLOperator):
:param sql: the sql to be executed. (templated)
:param conn_id: the connection ID used to connect to the database.
:param database: name of database which overwrite the defined one in connection
:param parameters: (optional) the parameters to render the SQL query with.
"""

template_fields: Sequence[str] = ("sql",)
Expand All @@ -619,14 +620,21 @@ class SQLCheckOperator(BaseSQLOperator):
ui_color = "#fff7e6"

def __init__(
self, *, sql: str, conn_id: str | None = None, database: str | None = None, **kwargs
self,
*,
sql: str,
conn_id: str | None = None,
database: str | None = None,
parameters: Iterable | Mapping | None = None,
wjmolina marked this conversation as resolved.
Show resolved Hide resolved
**kwargs,
) -> None:
super().__init__(conn_id=conn_id, database=database, **kwargs)
self.sql = sql
self.parameters = parameters

def execute(self, context: Context):
self.log.info("Executing SQL check: %s", self.sql)
records = self.get_db_hook().get_first(self.sql)
records = self.get_db_hook().get_first(self.sql, self.parameters)

self.log.info("Record: %s", records)
if not records:
Expand Down
2 changes: 1 addition & 1 deletion airflow/providers/snowflake/operators/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def __init__(
session_parameters: dict | None = None,
**kwargs,
) -> None:
super().__init__(sql=sql, **kwargs)
super().__init__(sql=sql, parameters=parameters, **kwargs)
self.snowflake_conn_id = snowflake_conn_id
self.sql = sql
self.autocommit = autocommit
Expand Down
7 changes: 6 additions & 1 deletion tests/providers/common/sql/operators/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def test_sql_operator_hook_params_biguery(self, mock_get_conn):

class TestCheckOperator(unittest.TestCase):
def setUp(self):
self._operator = SQLCheckOperator(task_id="test_task", sql="sql")
self._operator = SQLCheckOperator(task_id="test_task", sql="sql", parameters="parameters")

@mock.patch.object(SQLCheckOperator, "get_db_hook")
def test_execute_no_records(self, mock_get_db_hook):
Expand All @@ -499,6 +499,11 @@ def test_execute_not_all_records_are_true(self, mock_get_db_hook):
with pytest.raises(AirflowException, match=r"Test failed."):
self._operator.execute({})

@mock.patch.object(SQLCheckOperator, "get_db_hook")
def test_sqlcheckoperator_parameters(self, mock_get_db_hook):
self._operator.execute({})
mock_get_db_hook.return_value.get_first.assert_called_once_with("sql", "parameters")


class TestValueCheckOperator(unittest.TestCase):
def setUp(self):
Expand Down