This is an automated email from the ASF dual-hosted git repository.
potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 082055e23a Better error message when sqlite URL uses relative path
(#36774)
082055e23a is described below
commit 082055e23a38169a613f639296e144234f15d28c
Author: Jarek Potiuk <[email protected]>
AuthorDate: Sun Jan 14 22:43:55 2024 +0100
Better error message when sqlite URL uses relative path (#36774)
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.
---
airflow/settings.py | 8 ++++++++
tests/core/test_settings.py | 14 ++++++++++++--
2 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/airflow/settings.py b/airflow/settings.py
index a1b9c32a99..19874d1d21 100644
--- a/airflow/settings.py
+++ b/airflow/settings.py
@@ -191,6 +191,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"))
diff --git a/tests/core/test_settings.py b/tests/core/test_settings.py
index 949448075d..79f49b4ef2 100644
--- a/tests/core/test_settings.py
+++ b/tests/core/test_settings.py
@@ -21,11 +21,11 @@ import os
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 = """
@@ -214,3 +214,13 @@ class TestUpdatedConfigNames:
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)