Skip to content

Commit

Permalink
Fix SqliteHook compatibility with SQLAlchemy engine (#23790)
Browse files Browse the repository at this point in the history
Same as #19508 but for Sqlite as described in https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#connect-strings to be able to create a Sqlalchemy engine from the URI itself.

Without this, it currently fails with the following error due to how we create URI in Connections. An absolute path is denoted by starting with a slash, means you need four slashes:

```
url = sqlite://%2Ftmp%2Fsqlite.db

    def create_connect_args(self, url):
        if url.username or url.password or url.host or url.port:
>           raise exc.ArgumentError(
                "Invalid SQLite URL: %s\n"
                "Valid SQLite URL forms are:\n"
                " sqlite:///:memory: (or, sqlite://)\n"
                " sqlite:///relative/path/to/file.db\n"
                " sqlite:////absolute/path/to/file.db" % (url,)
            )
E           sqlalchemy.exc.ArgumentError: Invalid SQLite URL: sqlite://%2Ftmp%2Fsqlite.db
E           Valid SQLite URL forms are:
E            sqlite:///:memory: (or, sqlite://)
E            sqlite:///relative/path/to/file.db
E            sqlite:////absolute/path/to/file.db
```
  • Loading branch information
kaxil authored May 19, 2022
1 parent 9db2271 commit 479ad19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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

0 comments on commit 479ad19

Please sign in to comment.