Skip to content

Commit

Permalink
Better error message when sqlite URL uses relative path (#36774)
Browse files Browse the repository at this point in the history
When sqlite URL uses relative path, the error printed is quite
cryptic:

```
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
```

This might easily happen for example when you are in a hurry and put
relative value in your AIRFLOW_HOME.

This PR checks if sql is relative and throws more appropriate and
explicit message what is wrong.

(cherry picked from commit 082055e)
  • Loading branch information
potiuk authored and ephraimbuddy committed Jan 15, 2024
1 parent 42263f0 commit 3857385
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions airflow/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ def configure_vars():
global PLUGINS_FOLDER
global DONOT_MODIFY_HANDLERS
SQL_ALCHEMY_CONN = conf.get("database", "SQL_ALCHEMY_CONN")
if SQL_ALCHEMY_CONN.startswith("sqlite") and not SQL_ALCHEMY_CONN.startswith("sqlite:////"):
from airflow.exceptions import AirflowConfigException

raise AirflowConfigException(
f"Cannot use relative path: `{SQL_ALCHEMY_CONN}` to connect to sqlite. "
"Please use absolute path such as `sqlite:////tmp/airflow.db`."
)

DAGS_FOLDER = os.path.expanduser(conf.get("core", "DAGS_FOLDER"))

PLUGINS_FOLDER = conf.get("core", "plugins_folder", fallback=os.path.join(AIRFLOW_HOME, "plugins"))
Expand Down
14 changes: 12 additions & 2 deletions tests/core/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import sys
import tempfile
from unittest import mock
from unittest.mock import MagicMock, call
from unittest.mock import MagicMock, call, patch

import pytest

from airflow.exceptions import AirflowClusterPolicyViolation
from airflow.exceptions import AirflowClusterPolicyViolation, AirflowConfigException
from tests.test_utils.config import conf_vars

SETTINGS_FILE_POLICY = """
Expand Down Expand Up @@ -214,3 +214,13 @@ def test_uses_updated_session_timeout_config_by_default(self):
session_lifetime_config = settings.get_session_lifetime_config()
default_timeout_minutes = 30 * 24 * 60
assert session_lifetime_config == default_timeout_minutes


def test_sqlite_relative_path():
from airflow import settings

with patch("airflow.settings.conf.get") as conf_get_mock:
conf_get_mock.return_value = "sqlite:///./relative_path.db"
with pytest.raises(AirflowConfigException) as exc:
settings.configure_vars()
assert "Cannot use relative path:" in str(exc.value)

0 comments on commit 3857385

Please sign in to comment.