Skip to content
Open
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-core/src/airflow/cli/commands/db_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 9 additions & 0 deletions airflow-core/tests/unit/cli/commands/test_db_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]))

Comment on lines +537 to +545

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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"]))
@pytest.mark.parametrize(
"conn_uri",
[
pytest.param("postgresql://postgres:postgres@postgres:5432", id="db-name-none"),
pytest.param("postgresql://postgres:postgres@postgres:5432/", id="db-name-empty"),
],
)
def test_db_shell_missing_database_name(self, conn_uri):
"""Assert that an explicit ValueError is raised when the database name is missing."""
with mock.patch("airflow.cli.commands.db_command.settings.engine.url", make_url(conn_uri)):
with pytest.raises(ValueError, match="The metadata database name is missing"):
db_command.shell(self.parser.parse_args(["db", "shell"]))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The URL the bug was actually reported with is postgresql://postgres:postgres@postgres:5432 — no trailing slash, which parses to url.database = None. That None is what blew up the original code
(env["PGDATABASE"] = None).

Your test uses the trailing-slash form postgresql+psycopg2://postgres:airflow@postgres/, which gives you '' instead of None. The test isn't feeding in the exact input from the report.
Easiest fix is to just cover both.

def test_run_db_downgrade_command_success_and_messages(self, capsys):
class Args:
to_revision = "abc"
Expand Down
Loading