Skip to content

Commit a333ba4

Browse files
committed
Adjust port fix so it's not a breaking change
1 parent d8704c2 commit a333ba4

4 files changed

Lines changed: 36 additions & 16 deletions

File tree

parsons/databases/mysql/mysql.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,23 @@ class MySQL(DatabaseConnector, MySQLCreateTable, Alchemy):
3636
db: str
3737
Required if env variable ``MYSQL_DB`` not populated
3838
port: int
39-
Can be set by env variable ``MYSQL_PORT`` or argument.
39+
If omitted or ``None``, uses ``MYSQL_PORT`` when set, otherwise 3306. If passed
40+
(including ``3306``), the argument takes precedence over ``MYSQL_PORT``.
4041
4142
"""
4243

43-
def __init__(self, host=None, username=None, password=None, db=None, port=3306):
44+
def __init__(self, host=None, username=None, password=None, db=None, port=None):
4445
super().__init__()
4546

4647
self.username = check_env.check("MYSQL_USERNAME", username)
4748
self.password = check_env.check("MYSQL_PASSWORD", password)
4849
self.host = check_env.check("MYSQL_HOST", host)
4950
self.db = check_env.check("MYSQL_DB", db)
50-
env_port = check_env.check("MYSQL_PORT", None, optional=True)
51-
self.port = int(env_port) if env_port is not None else port
51+
if port is not None:
52+
self.port = port
53+
else:
54+
env_port = check_env.check("MYSQL_PORT", None, optional=True)
55+
self.port = int(env_port) if env_port is not None else 3306
5256

5357
@contextmanager
5458
def connection(self):

parsons/databases/postgres/postgres.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,25 @@ class Postgres(PostgresCore, Alchemy, DatabaseConnector):
2727
db: str
2828
Required if env variable ``PGDATABASE`` not populated
2929
port: int
30-
Required if env variable ``PGPORT`` not populated.
30+
If omitted or ``None``, uses ``PGPORT`` when set, otherwise 5432. If passed
31+
(including ``5432``), the argument takes precedence over ``PGPORT``.
3132
timeout: int
3233
Seconds to timeout if connection not established.
3334
3435
"""
3536

36-
def __init__(self, username=None, password=None, host=None, db=None, port=5432, timeout=10):
37+
def __init__(self, username=None, password=None, host=None, db=None, port=None, timeout=10):
3738
super().__init__()
3839

3940
self.username = check_env.check("PGUSER", username, optional=True)
4041
self.password = check_env.check("PGPASSWORD", password, optional=True)
4142
self.host = check_env.check("PGHOST", host, optional=True)
4243
self.db = check_env.check("PGDATABASE", db, optional=True)
43-
env_port = check_env.check("PGPORT", None, optional=True)
44-
self.port = int(env_port) if env_port is not None else port
44+
if port is not None:
45+
self.port = port
46+
else:
47+
env_port = check_env.check("PGPORT", None, optional=True)
48+
self.port = int(env_port) if env_port is not None else 5432
4549

4650
# Check if there is a pgpass file. Psycopg2 will search for this file first when
4751
# creating a connection.

test/test_mysql/test_mysql.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
class TestMySQLPortPrecedence(unittest.TestCase):
2020
"""
21-
Port resolution for MySQL: MYSQL_PORT env overrides the default port kwarg.
21+
Port resolution for MySQL:
2222
23-
When MYSQL_PORT is unset, the port kwarg is used (default 3306 if omitted).
23+
- ``MYSQL_PORT`` is used when ``port`` is omitted (or ``port=None``).
24+
- An explicit ``port=`` argument wins over ``MYSQL_PORT`` (including ``port=3306``).
2425
"""
2526

2627
def _env_without_mysql_port(self):
@@ -41,10 +42,15 @@ def test_explicit_port_kwarg_when_mysql_port_unset(self):
4142
mysql = MySQL(**_MYSQL_CONN_KWARGS, port=8888)
4243
assert mysql.port == 8888
4344

44-
def test_mysql_port_env_overrides_explicit_port_kwarg(self):
45+
def test_explicit_port_kwarg_overrides_mysql_port_env(self):
4546
with mock.patch.dict(os.environ, {"MYSQL_PORT": "3307"}, clear=False):
4647
mysql = MySQL(**_MYSQL_CONN_KWARGS, port=8888)
47-
assert mysql.port == 3307
48+
assert mysql.port == 8888
49+
50+
def test_explicit_default_port_kwarg_ignores_mysql_port_env(self):
51+
with mock.patch.dict(os.environ, {"MYSQL_PORT": "3307"}, clear=False):
52+
mysql = MySQL(**_MYSQL_CONN_KWARGS, port=3306)
53+
assert mysql.port == 3306
4854

4955

5056
# These tests interact directly with the MySQL database. To run, set env variable "LIVE_TEST=True"

test/test_postgres/test_postgres.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@ def test_create_statement(self):
157157

158158
class TestPostgresPortPrecedence(unittest.TestCase):
159159
"""
160-
Port resolution for Postgres: PGPORT env overrides the default port kwarg.
160+
Port resolution for Postgres:
161161
162-
When PGPORT is unset, the port kwarg is used (default 5432 if omitted).
162+
- ``PGPORT`` is used when ``port`` is omitted (or ``port=None``).
163+
- An explicit ``port=`` argument wins over ``PGPORT`` (including ``port=5432``).
163164
"""
164165

165166
def _env_without_pgport(self):
@@ -180,10 +181,15 @@ def test_explicit_port_kwarg_when_pgport_unset(self):
180181
pg = Postgres(**_POSTGRES_CONN_KWARGS, port=9999)
181182
assert pg.port == 9999
182183

183-
def test_pgport_env_overrides_explicit_port_kwarg(self):
184+
def test_explicit_port_kwarg_overrides_pgport_env(self):
184185
with mock.patch.dict(os.environ, {"PGPORT": "5433"}, clear=False):
185186
pg = Postgres(**_POSTGRES_CONN_KWARGS, port=9999)
186-
assert pg.port == 5433
187+
assert pg.port == 9999
188+
189+
def test_explicit_default_port_kwarg_ignores_pgport_env(self):
190+
with mock.patch.dict(os.environ, {"PGPORT": "5433"}, clear=False):
191+
pg = Postgres(**_POSTGRES_CONN_KWARGS, port=5432)
192+
assert pg.port == 5432
187193

188194

189195
# These tests interact directly with the Postgres database

0 commit comments

Comments
 (0)