Skip to content

Commit 8a191cf

Browse files
Merge pull request #2 from cheese-drawer/configure-port
Configure port
2 parents 715bb23 + 5d00413 commit 8a191cf

File tree

6 files changed

+15
-9
lines changed

6 files changed

+15
-9
lines changed

db_wrapper/connection.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ConnectionParameters:
2424
"""Defines connection parameters for database."""
2525

2626
host: str
27+
port: int
2728
user: str
2829
password: str
2930
database: str
@@ -37,15 +38,16 @@ async def _try_connect(
3738
user = connection_params.user
3839
password = connection_params.password
3940
host = connection_params.host
41+
port = connection_params.port
4042

41-
dsn = f'dbname={database} user={user} password={password} host={host}'
43+
dsn = f'dbname={database} user={user} password={password} host={host} port={port}'
4244

4345
# PENDS python 3.9 support in pylint
4446
# pylint: disable=unsubscriptable-object
4547
connection: Optional[aiopg.Connection] = None
4648

4749
LOGGER.info(
48-
f'Attempting to connect to database {database} as {user}@{host}...')
50+
f'Attempting to connect to database {database} as {user}@{host}:{port}...')
4951

5052
while connection is None:
5153
try:

example/example/example.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import json
5+
import logging
56
import os
67
from uuid import uuid4, UUID
78
from typing import Any, List
@@ -10,6 +11,8 @@
1011

1112
from models import AModel, ExtendedModel, ExtendedModelData
1213

14+
logging.basicConfig(level=logging.INFO)
15+
1316

1417
class UUIDJsonEncoder(json.JSONEncoder):
1518
"""Extended Json Encoder to allow encoding of objects containing UUID."""
@@ -23,6 +26,7 @@ def default(self, obj: Any) -> Any:
2326

2427
conn_params = ConnectionParameters(
2528
host=os.getenv('DB_HOST', 'localhost'),
29+
port=int(os.getenv('DB_PORT', '5432')),
2630
# user=os.getenv('DB_USER', 'postgres'),
2731
# password=os.getenv('DB_PASS', 'postgres'),
2832
# database=os.getenv('DB_NAME', 'postgres'))

example/manage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
import time
1515
from typing import Any, Optional, Generator, List, Tuple
1616

17-
from migra import Migration # type: ignore
17+
from migra import Migration
1818
from psycopg2 import connect, OperationalError # type: ignore
1919
from psycopg2 import sql
2020
from psycopg2.sql import Composed
21-
from sqlbag import ( # type: ignore
21+
from sqlbag import (
2222
S,
2323
load_sql_from_folder)
2424

@@ -170,13 +170,13 @@ def _get_schema_diff(
170170
def _temp_db(host: str, user: str, password: str) -> Generator[str, Any, Any]:
171171
"""Create, yield, & remove a temporary database as context."""
172172
connection = _resilient_connect(
173-
f'postgres://{user}:{password}@{host}/{DB_NAME}')
173+
f'postgresql://{user}:{password}@{host}/{DB_NAME}')
174174
connection.set_session(autocommit=True)
175175
name = _temp_name()
176176

177177
with connection.cursor() as cursor:
178178
_create_db(cursor, name)
179-
yield f'postgres://{user}:{password}@{host}/{name}'
179+
yield f'postgresql://{user}:{password}@{host}/{name}'
180180
_drop_db(cursor, name)
181181

182182
connection.close()

example/requirements/prod.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
psycopg2-binary>=2.8.6,<3.0.0
2-
https://github.com/cheese-drawer/lib-python-db-wrapper/releases/download/2.0.1/db_wrapper-2.0.1-py3-none-any.whl
2+
https://github.com/cheese-drawer/lib-python-db-wrapper/releases/download/2.0.2/db_wrapper-2.0.2-py3-none-any.whl

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="db_wrapper",
8-
version="2.0.1",
8+
version="2.0.2",
99
author="Andrew Chang-DeWitt",
1010
author_email="andrew@andrew-chang-dewitt.dev",
1111
description="Simple wrapper on aiopg to handle postgres connections & basic Models.",

test/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ def wrapped(instance: Any) -> None:
4444

4545
def get_client() -> Client:
4646
"""Create a client with placeholder connection data."""
47-
conn_params = ConnectionParameters('a', 'a', 'a', 'a')
47+
conn_params = ConnectionParameters('a', 'a', 'a', 'a', 'a')
4848
return Client(conn_params)

0 commit comments

Comments
 (0)