From 6eddf547d6097450231b8b89da2af77f4b7932a2 Mon Sep 17 00:00:00 2001 From: Chethan UK Date: Fri, 3 Jun 2022 00:41:45 +0100 Subject: [PATCH] Migrate Microsoft example DAGs to new design #22452 - mssql --- .../microsoft/mssql/example_dags/__init__.py | 16 -- .../mssql/example_dags/example_mssql.py | 137 ---------------- .../index.rst | 2 +- .../operators.rst | 12 +- .../microsoft/mssql}/create_table.sql | 0 .../microsoft/mssql/example_mssql.py | 149 ++++++++++++++++++ 6 files changed, 156 insertions(+), 160 deletions(-) delete mode 100644 airflow/providers/microsoft/mssql/example_dags/__init__.py delete mode 100644 airflow/providers/microsoft/mssql/example_dags/example_mssql.py rename {airflow/providers/microsoft/mssql/example_dags => tests/system/providers/microsoft/mssql}/create_table.sql (100%) create mode 100644 tests/system/providers/microsoft/mssql/example_mssql.py diff --git a/airflow/providers/microsoft/mssql/example_dags/__init__.py b/airflow/providers/microsoft/mssql/example_dags/__init__.py deleted file mode 100644 index 13a83393a9124..0000000000000 --- a/airflow/providers/microsoft/mssql/example_dags/__init__.py +++ /dev/null @@ -1,16 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. diff --git a/airflow/providers/microsoft/mssql/example_dags/example_mssql.py b/airflow/providers/microsoft/mssql/example_dags/example_mssql.py deleted file mode 100644 index 84a3b68a199f6..0000000000000 --- a/airflow/providers/microsoft/mssql/example_dags/example_mssql.py +++ /dev/null @@ -1,137 +0,0 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -""" -Example use of MsSql related operators. -""" -# [START mssql_operator_howto_guide] - -from datetime import datetime - -from airflow import DAG -from airflow.providers.microsoft.mssql.hooks.mssql import MsSqlHook -from airflow.providers.microsoft.mssql.operators.mssql import MsSqlOperator - -dag = DAG( - 'example_mssql', - schedule_interval='@daily', - start_date=datetime(2021, 10, 1), - tags=['example'], - catchup=False, -) - -# [START howto_operator_mssql] - -# Example of creating a task to create a table in MsSql - -create_table_mssql_task = MsSqlOperator( - task_id='create_country_table', - mssql_conn_id='airflow_mssql', - sql=r""" - CREATE TABLE Country ( - country_id INT NOT NULL IDENTITY(1,1) PRIMARY KEY, - name TEXT, - continent TEXT - ); - """, - dag=dag, -) - -# [END howto_operator_mssql] - -# [START mssql_hook_howto_guide_insert_mssql_hook] - - -@dag.task(task_id="insert_mssql_task") -def insert_mssql_hook(): - mssql_hook = MsSqlHook(mssql_conn_id='airflow_mssql', schema='airflow') - - rows = [ - ('India', 'Asia'), - ('Germany', 'Europe'), - ('Argentina', 'South America'), - ('Ghana', 'Africa'), - ('Japan', 'Asia'), - ('Namibia', 'Africa'), - ] - target_fields = ['name', 'continent'] - mssql_hook.insert_rows(table='Country', rows=rows, target_fields=target_fields) - - -# [END mssql_hook_howto_guide_insert_mssql_hook] - -# [START mssql_operator_howto_guide_create_table_mssql_from_external_file] -# Example of creating a task that calls an sql command from an external file. -create_table_mssql_from_external_file = MsSqlOperator( - task_id='create_table_from_external_file', - mssql_conn_id='airflow_mssql', - sql='create_table.sql', - dag=dag, -) -# [END mssql_operator_howto_guide_create_table_mssql_from_external_file] - -# [START mssql_operator_howto_guide_populate_user_table] -populate_user_table = MsSqlOperator( - task_id='populate_user_table', - mssql_conn_id='airflow_mssql', - sql=r""" - INSERT INTO Users (username, description) - VALUES ( 'Danny', 'Musician'); - INSERT INTO Users (username, description) - VALUES ( 'Simone', 'Chef'); - INSERT INTO Users (username, description) - VALUES ( 'Lily', 'Florist'); - INSERT INTO Users (username, description) - VALUES ( 'Tim', 'Pet shop owner'); - """, -) -# [END mssql_operator_howto_guide_populate_user_table] - -# [START mssql_operator_howto_guide_get_all_countries] -get_all_countries = MsSqlOperator( - task_id="get_all_countries", - mssql_conn_id='airflow_mssql', - sql=r"""SELECT * FROM Country;""", -) -# [END mssql_operator_howto_guide_get_all_countries] - -# [START mssql_operator_howto_guide_get_all_description] -get_all_description = MsSqlOperator( - task_id="get_all_description", - mssql_conn_id='airflow_mssql', - sql=r"""SELECT description FROM Users;""", -) -# [END mssql_operator_howto_guide_get_all_description] - -# [START mssql_operator_howto_guide_params_passing_get_query] -get_countries_from_continent = MsSqlOperator( - task_id="get_countries_from_continent", - mssql_conn_id='airflow_mssql', - sql=r"""SELECT * FROM Country where {{ params.column }}='{{ params.value }}';""", - params={"column": "CONVERT(VARCHAR, continent)", "value": "Asia"}, -) -# [END mssql_operator_howto_guide_params_passing_get_query] -( - create_table_mssql_task - >> insert_mssql_hook() - >> create_table_mssql_from_external_file - >> populate_user_table - >> get_all_countries - >> get_all_description - >> get_countries_from_continent -) -# [END mssql_operator_howto_guide] diff --git a/docs/apache-airflow-providers-microsoft-mssql/index.rst b/docs/apache-airflow-providers-microsoft-mssql/index.rst index ac42801752ede..df73fd3d9df83 100644 --- a/docs/apache-airflow-providers-microsoft-mssql/index.rst +++ b/docs/apache-airflow-providers-microsoft-mssql/index.rst @@ -39,7 +39,7 @@ Content :maxdepth: 1 :caption: Resources - Example DAGs + Example DAGs PyPI Repository Installing from sources diff --git a/docs/apache-airflow-providers-microsoft-mssql/operators.rst b/docs/apache-airflow-providers-microsoft-mssql/operators.rst index 2dc5f785b4b40..0dd55172150c2 100644 --- a/docs/apache-airflow-providers-microsoft-mssql/operators.rst +++ b/docs/apache-airflow-providers-microsoft-mssql/operators.rst @@ -38,7 +38,7 @@ The code snippets below are based on Airflow-2.2 An example usage of the MsSqlOperator is as follows: -.. exampleinclude:: /../../airflow/providers/microsoft/mssql/example_dags/example_mssql.py +.. exampleinclude:: /../../tests/system/providers/microsoft/mssql/example_mssql.py :language: python :start-after: [START howto_operator_mssql] :end-before: [END howto_operator_mssql] @@ -46,7 +46,7 @@ An example usage of the MsSqlOperator is as follows: You can also use an external file to execute the SQL commands. Script folder must be at the same level as DAG.py file. This way you can easily maintain the SQL queries separated from the code. -.. exampleinclude:: /../../airflow/providers/microsoft/mssql/example_dags/example_mssql.py +.. exampleinclude:: /../../tests/system/providers/microsoft/mssql/example_mssql.py :language: python :start-after: [START mssql_operator_howto_guide_create_table_mssql_from_external_file] :end-before: [END mssql_operator_howto_guide_create_table_mssql_from_external_file] @@ -68,7 +68,7 @@ Inserting data into a MSSQL database table --------------------------------------------- We can then create a MsSqlOperator task that populate the ``Users`` table. -.. exampleinclude:: /../../airflow/providers/microsoft/mssql/example_dags/example_mssql.py +.. exampleinclude:: /../../tests/system/providers/microsoft/mssql/example_mssql.py :language: python :start-after: [START mssql_operator_howto_guide_populate_user_table] :end-before: [END mssql_operator_howto_guide_populate_user_table] @@ -79,7 +79,7 @@ Fetching records from your MSSQL database table Fetching records from your MSSQL database table can be as simple as: -.. exampleinclude:: /../../airflow/providers/microsoft/mssql/example_dags/example_mssql.py +.. exampleinclude:: /../../tests/system/providers/microsoft/mssql/example_mssql.py :language: python :start-after: [START mssql_operator_howto_guide_get_all_countries] :end-before: [END mssql_operator_howto_guide_get_all_countries] @@ -93,7 +93,7 @@ SQL requests during runtime. To find the countries in Asian continent: -.. exampleinclude:: /../../airflow/providers/microsoft/mssql/example_dags/example_mssql.py +.. exampleinclude:: /../../tests/system/providers/microsoft/mssql/example_mssql.py :language: python :start-after: [START mssql_operator_howto_guide_params_passing_get_query] :end-before: [END mssql_operator_howto_guide_params_passing_get_query] @@ -104,7 +104,7 @@ The complete MSSQL Operator DAG When we put everything together, our DAG should look like this: -.. exampleinclude:: /../../airflow/providers/microsoft/mssql/example_dags/example_mssql.py +.. exampleinclude:: /../../tests/system/providers/microsoft/mssql/example_mssql.py :language: python :start-after: [START mssql_operator_howto_guide] :end-before: [END mssql_operator_howto_guide] diff --git a/airflow/providers/microsoft/mssql/example_dags/create_table.sql b/tests/system/providers/microsoft/mssql/create_table.sql similarity index 100% rename from airflow/providers/microsoft/mssql/example_dags/create_table.sql rename to tests/system/providers/microsoft/mssql/create_table.sql diff --git a/tests/system/providers/microsoft/mssql/example_mssql.py b/tests/system/providers/microsoft/mssql/example_mssql.py new file mode 100644 index 0000000000000..04b9ea0186337 --- /dev/null +++ b/tests/system/providers/microsoft/mssql/example_mssql.py @@ -0,0 +1,149 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +""" +Example use of MsSql related operators. +""" +# [START mssql_operator_howto_guide] +import os +from datetime import datetime + +from airflow import DAG +from airflow.providers.microsoft.mssql.hooks.mssql import MsSqlHook +from airflow.providers.microsoft.mssql.operators.mssql import MsSqlOperator + +ENV_ID = os.environ.get("SYSTEM_TESTS_ENV_ID") +DAG_ID = "example_mssql" + + +with DAG( + DAG_ID, + schedule_interval='@daily', + start_date=datetime(2021, 10, 1), + tags=['example'], + catchup=False, +) as dag: + + # [START howto_operator_mssql] + + # Example of creating a task to create a table in MsSql + + create_table_mssql_task = MsSqlOperator( + task_id='create_country_table', + mssql_conn_id='airflow_mssql', + sql=r""" + CREATE TABLE Country ( + country_id INT NOT NULL IDENTITY(1,1) PRIMARY KEY, + name TEXT, + continent TEXT + ); + """, + dag=dag, + ) + + # [END howto_operator_mssql] + + # [START mssql_hook_howto_guide_insert_mssql_hook] + + @dag.task(task_id="insert_mssql_task") + def insert_mssql_hook(): + mssql_hook = MsSqlHook(mssql_conn_id='airflow_mssql', schema='airflow') + + rows = [ + ('India', 'Asia'), + ('Germany', 'Europe'), + ('Argentina', 'South America'), + ('Ghana', 'Africa'), + ('Japan', 'Asia'), + ('Namibia', 'Africa'), + ] + target_fields = ['name', 'continent'] + mssql_hook.insert_rows(table='Country', rows=rows, target_fields=target_fields) + + # [END mssql_hook_howto_guide_insert_mssql_hook] + + # [START mssql_operator_howto_guide_create_table_mssql_from_external_file] + # Example of creating a task that calls an sql command from an external file. + create_table_mssql_from_external_file = MsSqlOperator( + task_id='create_table_from_external_file', + mssql_conn_id='airflow_mssql', + sql='create_table.sql', + dag=dag, + ) + # [END mssql_operator_howto_guide_create_table_mssql_from_external_file] + + # [START mssql_operator_howto_guide_populate_user_table] + populate_user_table = MsSqlOperator( + task_id='populate_user_table', + mssql_conn_id='airflow_mssql', + sql=r""" + INSERT INTO Users (username, description) + VALUES ( 'Danny', 'Musician'); + INSERT INTO Users (username, description) + VALUES ( 'Simone', 'Chef'); + INSERT INTO Users (username, description) + VALUES ( 'Lily', 'Florist'); + INSERT INTO Users (username, description) + VALUES ( 'Tim', 'Pet shop owner'); + """, + ) + # [END mssql_operator_howto_guide_populate_user_table] + + # [START mssql_operator_howto_guide_get_all_countries] + get_all_countries = MsSqlOperator( + task_id="get_all_countries", + mssql_conn_id='airflow_mssql', + sql=r"""SELECT * FROM Country;""", + ) + # [END mssql_operator_howto_guide_get_all_countries] + + # [START mssql_operator_howto_guide_get_all_description] + get_all_description = MsSqlOperator( + task_id="get_all_description", + mssql_conn_id='airflow_mssql', + sql=r"""SELECT description FROM Users;""", + ) + # [END mssql_operator_howto_guide_get_all_description] + + # [START mssql_operator_howto_guide_params_passing_get_query] + get_countries_from_continent = MsSqlOperator( + task_id="get_countries_from_continent", + mssql_conn_id='airflow_mssql', + sql=r"""SELECT * FROM Country where {{ params.column }}='{{ params.value }}';""", + params={"column": "CONVERT(VARCHAR, continent)", "value": "Asia"}, + ) + # [END mssql_operator_howto_guide_params_passing_get_query] + ( + create_table_mssql_task + >> insert_mssql_hook() + >> create_table_mssql_from_external_file + >> populate_user_table + >> get_all_countries + >> get_all_description + >> get_countries_from_continent + ) + # [END mssql_operator_howto_guide] + + from tests.system.utils.watcher import watcher + + # This test needs watcher in order to properly mark success/failure + # when "tearDown" task with trigger rule is part of the DAG + list(dag.tasks) >> watcher() +from tests.system.utils import get_test_run # noqa: E402 + +# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest) +test_run = get_test_run(dag)