diff --git a/airflow-core/src/airflow/cli/commands/db_command.py b/airflow-core/src/airflow/cli/commands/db_command.py index 4c15ccc2d488d..54de07299c645 100644 --- a/airflow-core/src/airflow/cli/commands/db_command.py +++ b/airflow-core/src/airflow/cli/commands/db_command.py @@ -306,6 +306,12 @@ def shell(args): url = settings.get_engine().url print(f"DB: {url!r}") + if not url.database: + raise ValueError( + "The metadata database name is missing from the connection URI. " + "Please check your AIRFLOW__DATABASE__SQL_ALCHEMY_CONN configuration." + ) + if url.get_backend_name() == "mysql": with NamedTemporaryFile(suffix="my.cnf") as f: f.write(_build_mysql_cnf(url)) diff --git a/airflow-core/tests/unit/cli/commands/test_db_command.py b/airflow-core/tests/unit/cli/commands/test_db_command.py index 76e391e1144ed..45f4a45aafac1 100644 --- a/airflow-core/tests/unit/cli/commands/test_db_command.py +++ b/airflow-core/tests/unit/cli/commands/test_db_command.py @@ -534,6 +534,15 @@ def test_cli_shell_invalid_ppg3(self): with pytest.raises(AirflowException, match=r"Unknown driver: invalid\+psycopg"): db_command.shell(self.parser.parse_args(["db", "shell"])) + @mock.patch( + "airflow.cli.commands.db_command.settings.engine.url", + make_url("postgresql+psycopg2://postgres:airflow@postgres/"), + ) + def test_db_shell_missing_database_name(self): + """Assert that an explicit ValueError is raised when the database name is missing.""" + with pytest.raises(ValueError, match="The metadata database name is missing"): + db_command.shell(self.parser.parse_args(["db", "shell"])) + def test_run_db_downgrade_command_success_and_messages(self, capsys): class Args: to_revision = "abc"