Skip to content
Closed
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
29 changes: 29 additions & 0 deletions airflow/providers/microsoft/mssql/hooks/mssql.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

from typing import Any

from typing import Iterable, Sized

import pymssql

from airflow.providers.common.sql.hooks.sql import DbApiHook
Expand Down Expand Up @@ -114,3 +116,30 @@ def set_autocommit(

def get_autocommit(self, conn: pymssql.connect):
return conn.autocommit_state

@staticmethod
def _generate_insert_sql(
table: str, values: Sized, target_fields: Iterable[str], replace: bool, **kwargs
) -> str:
"""
Static helper method that generates the INSERT SQL statement.

:param table: Name of the target table
:param values: The row to insert into the table
:param target_fields: The names of the columns to fill in the table
:param replace: Whether to replace instead of insert (replace currently not supported)
:return: The generated INSERT SQL statement
"""
if replace:
raise NotImplementedError("replace is not supported currently")

placeholders = ["?"] * len(values)

if target_fields:
target_fields = ", ".join(target_fields)
target_fields = f"({target_fields})"
else:
target_fields = ""

sql = f"INSERT INTO {table} {target_fields} VALUES ({','.join(placeholders)})"
return sql
32 changes: 32 additions & 0 deletions tests/operators/test_generic_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from airflow.models.dag import DAG
from airflow.operators.generic_transfer import GenericTransfer
from airflow.providers.microsoft.mssql.hooks.mssql import MsSqlHook
from airflow.providers.mysql.hooks.mysql import MySqlHook
from airflow.providers.postgres.hooks.postgres import PostgresHook
from airflow.utils import timezone
Expand Down Expand Up @@ -97,6 +98,37 @@ def test_mysql_to_mysql_replace(self, mock_insert):
assert 'replace' in kwargs


@pytest.mark.backend("mssql")
class TestMsSql(unittest.TestCase):
def setUp(self):
args = {'owner': 'airflow', 'start_date': DEFAULT_DATE}
dag = DAG(TEST_DAG_ID, default_args=args)
self.dag = dag

def tearDown(self):
drop_tables = {'test_mssql_to_mssql', 'test_airflow'}
with closing(MsSqlHook().get_conn()) as conn:
for table in drop_tables:
with closing(conn.cursor()) as cur:
cur.execute(f"DROP TABLE IF EXISTS {table}")

def test_mssql_to_mssql(self):
sql = "SELECT * FROM connection;"
op = GenericTransfer(
task_id='test_ms2ms',
preoperator=[
"DROP TABLE IF EXISTS test_mssql_to_mssql",
"CREATE TABLE IF NOT EXISTS test_mssql_to_mssql LIKE connection",
],
source_conn_id='airflow_db',
destination_conn_id='airflow_db',
destination_table="test_mssql_to_mssql",
sql=sql,
dag=self.dag,
)
op.run(start_date=DEFAULT_DATE, end_date=DEFAULT_DATE, ignore_ti_state=True)


@pytest.mark.backend("postgres")
class TestPostgres(unittest.TestCase):
def setUp(self):
Expand Down