Skip to content

Commit

Permalink
fix: Display a better error message when database URI is null (#8015)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarrmondragon committed Aug 14, 2023
1 parent 37fe05a commit 892ebca
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/meltano/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ def __init__(self, reason: str):
super().__init__(reason, self.INSTRUCTION)


class NullConnectionStringError(MeltanoError):
"""Raised when the database is not compatible with Meltano."""

REASON = "The `database_uri` setting has a null value"
INSTRUCTION = (
"Verify that the `database_uri` setting points to a valid database connection "
"URI, or use `MELTANO_FF_STRICT_ENV_VAR_MODE=1 meltano config meltano list` "
"to check for missing environment variables"
)

def __init__(self):
"""Initialize the exception."""
super().__init__(self.REASON, self.INSTRUCTION)


def project_engine(
project: Project,
default: bool = False,
Expand All @@ -50,6 +65,9 @@ def project_engine(
Returns:
The engine, and a session maker bound to the engine.
Raises:
NullConnectionStringError: The `database_uri` setting has a null value.
"""
existing_engine = _engines.get(project)
if existing_engine:
Expand All @@ -58,6 +76,9 @@ def project_engine(
engine_uri = project.settings.get("database_uri")
logging.debug(f"Creating engine '{project}@{engine_uri}'")

if engine_uri is None:
raise NullConnectionStringError

engine = create_engine(engine_uri, poolclass=NullPool)

# Connect to the database to ensure it is available.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from __future__ import annotations

import typing as t

import pytest
import yaml
from mock import Mock
from sqlalchemy.exc import OperationalError

from meltano.core.db import connect
from meltano.core.db import NullConnectionStringError, connect, project_engine
from meltano.core.project import Project

if t.TYPE_CHECKING:
from pathlib import Path


class TestConnectionRetries:
Expand All @@ -31,3 +38,25 @@ def test_ping_failure(self):

connect(engine=engine_mock, max_retries=3, retry_timeout=0.1)
assert engine_mock.connect.call_count == 2


class TestProjectEngine:
def test_project_engine(
self,
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
):
with tmp_path.joinpath("meltano.yml").open("w") as meltano_yml:
yaml.dump(
{
"project_id": "test",
"send_anonymous_usage_stat": False,
"database_uri": "$DATABASE",
},
meltano_yml,
)
monkeypatch.delenv("MELTANO_DATABASE_URI")

project = Project(tmp_path)
with pytest.raises(NullConnectionStringError):
project_engine(project)

0 comments on commit 892ebca

Please sign in to comment.