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

Fix SqliteHook compatibility with SQLAlchemy engine #23790

Merged
merged 1 commit into from
May 19, 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
6 changes: 6 additions & 0 deletions airflow/providers/sqlite/hooks/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ def get_conn(self) -> sqlite3.dbapi2.Connection:
conn = sqlite3.connect(airflow_conn.host)
return conn

def get_uri(self) -> str:
"""Override DbApiHook get_uri method for get_sqlalchemy_engine()"""
conn_id = getattr(self, self.conn_name_attr)
airflow_conn = self.get_connection(conn_id)
return f"sqlite:///{airflow_conn.host}"

@staticmethod
def _generate_insert_sql(table, values, target_fields, replace, **kwargs):
"""
Expand Down
12 changes: 12 additions & 0 deletions tests/providers/sqlite/hooks/test_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from unittest import mock
from unittest.mock import patch

import sqlalchemy

from airflow.models import Connection
from airflow.providers.sqlite.hooks.sqlite import SqliteHook

Expand Down Expand Up @@ -126,3 +128,13 @@ def test_generate_insert_sql_replace_true(self):
)

assert sql == expected_sql

def test_sqlalchemy_engine(self):
"""Test that the sqlalchemy engine is initialized"""
conn_id = 'sqlite_default'
hook = SqliteHook(sqlite_conn_id=conn_id)
engine = hook.get_sqlalchemy_engine()
assert isinstance(engine, sqlalchemy.engine.Engine)
assert engine.name == 'sqlite'
# Assert filepath of the sqliate DB is correct
assert engine.url.database == hook.get_connection(conn_id).host