Skip to content

Commit f71c652

Browse files
authored
Merge pull request #19 from Tyskiep99/feat_add_dbname_parameter_to_connection
feat: add database name parameter to watcher.
2 parents 8881961 + dd6bed9 commit f71c652

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

postgresql_watcher/watcher.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from typing import Optional, Callable, Any
1+
from typing import Optional, Callable
22
from psycopg2 import connect, extensions
3-
from multiprocessing import Process, Pipe, connection
3+
from multiprocessing import Process, Pipe
44
import time
55
from select import select
66

7+
78
POSTGRESQL_CHANNEL_NAME = "casbin_role_watcher"
89

910

@@ -13,12 +14,19 @@ def casbin_subscription(
1314
user: str,
1415
password: str,
1516
port: Optional[int] = 5432,
17+
dbname: Optional[str] = "postgres",
1618
delay: Optional[int] = 2,
1719
channel_name: Optional[str] = POSTGRESQL_CHANNEL_NAME,
1820
):
1921
# delay connecting to postgresql (postgresql connection failure)
2022
time.sleep(delay)
21-
conn = connect(host=host, port=port, user=user, password=password)
23+
conn = connect(
24+
host=host,
25+
port=port,
26+
user=user,
27+
password=password,
28+
dbname=dbname
29+
)
2230
# Can only receive notifications when not in transaction, set this for easier usage
2331
conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)
2432
curs = conn.cursor()
@@ -41,6 +49,7 @@ def __init__(
4149
user: str,
4250
password: str,
4351
port: Optional[int] = 5432,
52+
dbname: Optional[str] = "postgres",
4453
channel_name: Optional[str] = POSTGRESQL_CHANNEL_NAME,
4554
start_process: Optional[bool] = True,
4655
):
@@ -50,6 +59,7 @@ def __init__(
5059
self.port = port
5160
self.user = user
5261
self.password = password
62+
self.dbname = dbname
5363
self.channel_name = channel_name
5464
self.subscribed_process = self.create_subscriber_process(start_process)
5565

@@ -69,6 +79,7 @@ def create_subscriber_process(
6979
self.user,
7080
self.password,
7181
self.port,
82+
self.dbname,
7283
delay,
7384
self.channel_name,
7485
),
@@ -89,6 +100,7 @@ def update(self):
89100
port=self.port,
90101
user=self.user,
91102
password=self.password,
103+
dbname=self.dbname,
92104
)
93105
# Can only receive notifications when not in transaction, set this for easier usage
94106
conn.set_isolation_level(extensions.ISOLATION_LEVEL_AUTOCOMMIT)

tests/test_postgresql_watcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
PORT = 5432
1111
USER = "postgres"
1212
PASSWORD = "123456"
13+
DBNAME = "postgres"
1314

1415

1516
def get_watcher():
16-
return PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD)
17+
return PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME)
1718

1819

1920
pg_watcher = get_watcher()

0 commit comments

Comments
 (0)