Skip to content

Commit 1548d00

Browse files
authored
Fix db_wizard behavior and add a test (#216)
Similar fix to dbos-inc/dbos-transact-ts#763 - DB wizard should not proceed if the `database` hostname/username/port is set in `dbos-config.yaml`. - Add a test for DB wizard, making sure it will not proceed if the database field is set.
1 parent da7b5a3 commit 1548d00

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.dbos/
12
# Byte-compiled / optimized / DLL files
23
__pycache__/
34
*.py[cod]

dbos/_db_wizard.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,16 @@ def db_wizard(config: "ConfigFile", config_file_path: str) -> "ConfigFile":
4545
f"Could not connect to Postgres: password authentication failed: {db_connection_error}"
4646
)
4747
db_config = config["database"]
48+
49+
# Read the config file and check if the database hostname/port/username are set. If so, skip the wizard.
50+
with open(config_file_path, "r") as file:
51+
content = file.read()
52+
local_config = yaml.safe_load(content)
4853
if (
49-
db_config["hostname"] != "localhost"
54+
local_config["database"]["hostname"]
55+
or local_config["database"]["port"]
56+
or local_config["database"]["username"]
57+
or db_config["hostname"] != "localhost"
5058
or db_config["port"] != 5432
5159
or db_config["username"] != "postgres"
5260
):

tests/test_config.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,3 +399,26 @@ def test_db_connect_failed(mocker):
399399
load_config(mock_filename)
400400

401401
assert "Could not connect to the database" in str(exc_info.value)
402+
403+
404+
def test_no_db_wizard(mocker):
405+
mock_config = """
406+
name: "some-app"
407+
language: "python"
408+
runtimeConfig:
409+
start:
410+
- "python3 main.py"
411+
database:
412+
hostname: 'localhost'
413+
port: 5432
414+
username: 'postgres'
415+
password: 'somerandom'
416+
417+
"""
418+
mocker.patch(
419+
"builtins.open", side_effect=generate_mock_open(mock_filename, mock_config)
420+
)
421+
422+
with pytest.raises(DBOSInitializationError) as exc_info:
423+
load_config(mock_filename)
424+
assert "Could not connect" in str(exc_info.value)

0 commit comments

Comments
 (0)