예제 #1
0
 def test_mysql_operator_test(self):
     sql = """
     CREATE TABLE IF NOT EXISTS test_airflow (
         dummy VARCHAR(50)
     );
     """
     op = MySqlOperator(task_id='basic_mysql', sql=sql, dag=self.dag)
     op.run(start_date=DEFAULT_DATE,
            end_date=DEFAULT_DATE,
            ignore_ti_state=True)
예제 #2
0
 def test_mysql_operator_test_multi(self):
     sql = [
         "CREATE TABLE IF NOT EXISTS test_airflow (dummy VARCHAR(50))",
         "TRUNCATE TABLE test_airflow",
         "INSERT INTO test_airflow VALUES ('X')",
     ]
     from airflow.providers.mysql.operators.mysql import MySqlOperator
     op = MySqlOperator(
         task_id='mysql_operator_test_multi',
         sql=sql,
         dag=self.dag,
     )
     op.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)
예제 #3
0
 def test_mysql_operator_test_multi(self, client):
     with MySqlContext(client):
         sql = [
             "CREATE TABLE IF NOT EXISTS test_airflow (dummy VARCHAR(50))",
             "TRUNCATE TABLE test_airflow",
             "INSERT INTO test_airflow VALUES ('X')",
         ]
         op = MySqlOperator(
             task_id='mysql_operator_test_multi',
             sql=sql,
             dag=self.dag,
         )
         op.run(start_date=DEFAULT_DATE,
                end_date=DEFAULT_DATE,
                ignore_ti_state=True)
예제 #4
0
    def test_overwrite_schema(self):
        """
        Verifies option to overwrite connection schema
        """
        sql = "SELECT 1;"
        op = MySqlOperator(
            task_id='test_mysql_operator_test_schema_overwrite',
            sql=sql,
            dag=self.dag,
            database="foobar",
        )

        from _mysql_exceptions import OperationalError
        try:
            op.run(start_date=DEFAULT_DATE,
                   end_date=DEFAULT_DATE,
                   ignore_ti_state=True)
        except OperationalError as e:
            assert "Unknown database 'foobar'" in str(e)
예제 #5
0
    def test_overwrite_schema(self, client):
        """
        Verifies option to overwrite connection schema
        """
        with MySqlContext(client):
            sql = "SELECT 1;"
            op = MySqlOperator(
                task_id='test_mysql_operator_test_schema_overwrite',
                sql=sql,
                dag=self.dag,
                database="foobar",
            )

            from MySQLdb import OperationalError  # pylint: disable=no-name-in-module

            try:
                op.run(start_date=DEFAULT_DATE,
                       end_date=DEFAULT_DATE,
                       ignore_ti_state=True)
            except OperationalError as e:
                assert "Unknown database 'foobar'" in str(e)
예제 #6
0
from airflow import DAG
from airflow.providers.mysql.operators.mysql import MySqlOperator

dag = DAG(
    'example_mysql',
    start_date=datetime(2021, 1, 1),
    default_args={'mysql_conn_id': 'mysql_conn_id'},
    tags=['example'],
    catchup=False,
)

# [START howto_operator_mysql]

drop_table_mysql_task = MySqlOperator(task_id='create_table_mysql',
                                      sql=r"""DROP TABLE table_name;""",
                                      dag=dag)

# [END howto_operator_mysql]

# [START howto_operator_mysql_external_file]

mysql_task = MySqlOperator(
    task_id='create_table_mysql_external_file',
    sql='/scripts/drop_table.sql',
    dag=dag,
)

# [END howto_operator_mysql_external_file]

drop_table_mysql_task >> mysql_task
예제 #7
0
    dest_conn.close()


with DAG(**dag_params, template_searchpath=[cfg.dir_dag_template]) as dag:

    create_extension_task = PostgresOperator(task_id='create_extension',
                                             sql="create_extension.sql",
                                             postgres_conn_id="source")

    create_source_table_and_insert_source_table = PostgresOperator(
        task_id='create_table',
        sql="set_up_python.sql",
        postgres_conn_id="source")

    create_target_table_mysql = MySqlOperator(
        task_id='create_table_mysql',
        mysql_conn_id='target',
        sql='create_table_mysql.sql',
    )

    insert_source_table_no_duplicate = PostgresOperator(
        task_id='insert_target_table_no_duplicate',
        sql="insert_target_table_no_duplicate.sql",
        postgres_conn_id="source")

    insert_target_table = PythonOperator(task_id='insert_target_table',
                                         python_callable=copy,
                                         provide_context=True)

    create_extension_task >> create_source_table_and_insert_source_table >> insert_source_table_no_duplicate >> create_target_table_mysql >> insert_target_table