36
36
37
37
# Increment this PATCH version before using `charmcraft publish-lib` or reset
38
38
# to 0 if you are raising the major API version
39
- LIBPATCH = 26
39
+ LIBPATCH = 28
40
40
41
41
INVALID_EXTRA_USER_ROLE_BLOCKING_MESSAGE = "invalid role(s) for extra user roles"
42
42
@@ -111,20 +111,19 @@ def __init__(
111
111
self .system_users = system_users
112
112
113
113
def _connect_to_database (
114
- self , database : str = None , connect_to_current_host : bool = False
114
+ self , database : str = None , database_host : str = None
115
115
) -> psycopg2 .extensions .connection :
116
116
"""Creates a connection to the database.
117
117
118
118
Args:
119
119
database: database to connect to (defaults to the database
120
120
provided when the object for this class was created).
121
- connect_to_current_host: whether to connect to the current host
122
- instead of the primary host.
121
+ database_host: host to connect to instead of the primary host.
123
122
124
123
Returns:
125
124
psycopg2 connection object.
126
125
"""
127
- host = self . current_host if connect_to_current_host else self .primary_host
126
+ host = database_host if database_host is not None else self .primary_host
128
127
connection = psycopg2 .connect (
129
128
f"dbname='{ database if database else self .database } ' user='{ self .user } ' host='{ host } '"
130
129
f"password='{ self .password } ' connect_timeout=1"
@@ -231,7 +230,10 @@ def create_user(
231
230
user_definition += f"WITH { 'NOLOGIN' if user == 'admin' else 'LOGIN' } { ' SUPERUSER' if admin else '' } ENCRYPTED PASSWORD '{ password } '{ 'IN ROLE admin CREATEDB' if admin_role else '' } "
232
231
if privileges :
233
232
user_definition += f' { " " .join (privileges )} '
233
+ cursor .execute (sql .SQL ("BEGIN;" ))
234
+ cursor .execute (sql .SQL ("SET LOCAL log_statement = 'none';" ))
234
235
cursor .execute (sql .SQL (f"{ user_definition } ;" ).format (sql .Identifier (user )))
236
+ cursor .execute (sql .SQL ("COMMIT;" ))
235
237
236
238
# Add extra user roles to the new user.
237
239
if roles :
@@ -388,7 +390,7 @@ def get_postgresql_text_search_configs(self) -> Set[str]:
388
390
Set of PostgreSQL text search configs.
389
391
"""
390
392
with self ._connect_to_database (
391
- connect_to_current_host = True
393
+ database_host = self . current_host
392
394
) as connection , connection .cursor () as cursor :
393
395
cursor .execute ("SELECT CONCAT('pg_catalog.', cfgname) FROM pg_ts_config;" )
394
396
text_search_configs = cursor .fetchall ()
@@ -401,7 +403,7 @@ def get_postgresql_timezones(self) -> Set[str]:
401
403
Set of PostgreSQL timezones.
402
404
"""
403
405
with self ._connect_to_database (
404
- connect_to_current_host = True
406
+ database_host = self . current_host
405
407
) as connection , connection .cursor () as cursor :
406
408
cursor .execute ("SELECT name FROM pg_timezone_names;" )
407
409
timezones = cursor .fetchall ()
@@ -434,7 +436,7 @@ def is_tls_enabled(self, check_current_host: bool = False) -> bool:
434
436
"""
435
437
try :
436
438
with self ._connect_to_database (
437
- connect_to_current_host = check_current_host
439
+ database_host = self . current_host if check_current_host else None
438
440
) as connection , connection .cursor () as cursor :
439
441
cursor .execute ("SHOW ssl;" )
440
442
return "on" in cursor .fetchone ()[0 ]
@@ -502,24 +504,32 @@ def set_up_database(self) -> None:
502
504
if connection is not None :
503
505
connection .close ()
504
506
505
- def update_user_password (self , username : str , password : str ) -> None :
507
+ def update_user_password (
508
+ self , username : str , password : str , database_host : str = None
509
+ ) -> None :
506
510
"""Update a user password.
507
511
508
512
Args:
509
513
username: the user to update the password.
510
514
password: the new password for the user.
515
+ database_host: the host to connect to.
511
516
512
517
Raises:
513
518
PostgreSQLUpdateUserPasswordError if the password couldn't be changed.
514
519
"""
515
520
connection = None
516
521
try :
517
- with self ._connect_to_database () as connection , connection .cursor () as cursor :
522
+ with self ._connect_to_database (
523
+ database_host = database_host
524
+ ) as connection , connection .cursor () as cursor :
525
+ cursor .execute (sql .SQL ("BEGIN;" ))
526
+ cursor .execute (sql .SQL ("SET LOCAL log_statement = 'none';" ))
518
527
cursor .execute (
519
528
sql .SQL ("ALTER USER {} WITH ENCRYPTED PASSWORD '" + password + "';" ).format (
520
529
sql .Identifier (username )
521
530
)
522
531
)
532
+ cursor .execute (sql .SQL ("COMMIT;" ))
523
533
except psycopg2 .Error as e :
524
534
logger .error (f"Failed to update user password: { e } " )
525
535
raise PostgreSQLUpdateUserPasswordError ()
@@ -610,7 +620,7 @@ def validate_date_style(self, date_style: str) -> bool:
610
620
"""
611
621
try :
612
622
with self ._connect_to_database (
613
- connect_to_current_host = True
623
+ database_host = self . current_host
614
624
) as connection , connection .cursor () as cursor :
615
625
cursor .execute (
616
626
sql .SQL (
0 commit comments