Skip to content

Commit 1a1540a

Browse files
authored
[DPE-2569] Extensions not enabled when database is created (#252)
* Extensions not enabled when database is created * Bump agent version * Bump lib
1 parent 8706c52 commit 1a1540a

File tree

7 files changed

+34
-10
lines changed

7 files changed

+34
-10
lines changed

.github/workflows/ci.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ jobs:
8686
- upgrade-from-stable-integration
8787
agent-versions:
8888
- "2.9.45" # renovate: latest juju 2
89-
- "3.1.5" # renovate: latest juju 3
89+
- "3.1.6" # renovate: latest juju 3
9090
free-disk-space:
9191
- false
9292
include:
9393
- tox-environments: database-relation-integration
9494
agent-versions: "2.9.45" # renovate: latest juju 2
9595
free-disk-space: true
9696
- tox-environments: database-relation-integration
97-
agent-versions: "3.1.5" # renovate: latest juju 3
97+
agent-versions: "3.1.6" # renovate: latest juju 3
9898
free-disk-space: true
9999
- tox-environments: db-admin-relation-integration
100100
agent-versions: "2.9.45" # renovate: latest juju 2

lib/charms/postgresql_k8s/v0/postgresql.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
# Increment this PATCH version before using `charmcraft publish-lib` or reset
3434
# to 0 if you are raising the major API version
35-
LIBPATCH = 16
35+
LIBPATCH = 17
3636

3737
INVALID_EXTRA_USER_ROLE_BLOCKING_MESSAGE = "invalid role(s) for extra user roles"
3838

@@ -117,12 +117,13 @@ def _connect_to_database(
117117
connection.autocommit = True
118118
return connection
119119

120-
def create_database(self, database: str, user: str) -> None:
120+
def create_database(self, database: str, user: str, plugins: List[str] = []) -> None:
121121
"""Creates a new database and grant privileges to a user on it.
122122
123123
Args:
124124
database: database to be created.
125125
user: user that will have access to the database.
126+
plugins: extensions to enable in the new database.
126127
"""
127128
try:
128129
connection = self._connect_to_database()
@@ -170,6 +171,10 @@ def create_database(self, database: str, user: str) -> None:
170171
logger.error(f"Failed to create database: {e}")
171172
raise PostgreSQLCreateDatabaseError()
172173

174+
# Enable preset extensions
175+
for plugin in plugins:
176+
self.enable_disable_extension(plugin, True, database)
177+
173178
def create_user(
174179
self, user: str, password: str = None, admin: bool = False, extra_user_roles: str = None
175180
) -> None:

src/charm.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,17 +930,22 @@ def enable_disable_extensions(self, database: str = None) -> None:
930930
Args:
931931
database: optional database where to enable/disable the extension.
932932
"""
933+
original_status = self.unit.status
933934
for plugin in self.config.plugin_keys():
934935
enable = self.config[plugin]
935936

936937
# Enable or disable the plugin/extension.
937938
extension = "_".join(plugin.split("_")[1:-1])
939+
self.unit.status = WaitingStatus(
940+
f"{'Enabling' if enable else 'Disabling'} {extension}"
941+
)
938942
try:
939943
self.postgresql.enable_disable_extension(extension, enable, database)
940944
except PostgreSQLEnableDisableExtensionError as e:
941945
logger.exception(
942946
f"failed to {'enable' if enable else 'disable'} {extension} plugin: %s", str(e)
943947
)
948+
self.unit.status = original_status
944949

945950
def _get_ips_to_remove(self) -> Set[str]:
946951
"""List the IPs that were part of the cluster but departed."""

src/relations/db.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,13 @@ def set_up_relation(self, relation: Relation) -> bool:
168168
self.charm.set_secret(APP_SCOPE, f"{user}-database", database)
169169

170170
self.charm.postgresql.create_user(user, password, self.admin)
171-
self.charm.postgresql.create_database(database, user)
171+
plugins = [
172+
"_".join(plugin.split("_")[1:-1])
173+
for plugin in self.charm.config.plugin_keys()
174+
if self.charm.config[plugin]
175+
]
176+
177+
self.charm.postgresql.create_database(database, user, plugins=plugins)
172178

173179
# Enable/disable extensions in the new database.
174180
self.charm.enable_disable_extensions(database)

src/relations/postgresql_provider.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,13 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
8484
user = f"relation-{event.relation.id}"
8585
password = new_password()
8686
self.charm.postgresql.create_user(user, password, extra_user_roles=extra_user_roles)
87-
self.charm.postgresql.create_database(database, user)
87+
plugins = [
88+
"_".join(plugin.split("_")[1:-1])
89+
for plugin in self.charm.config.plugin_keys()
90+
if self.charm.config[plugin]
91+
]
92+
93+
self.charm.postgresql.create_database(database, user, plugins=plugins)
8894

8995
# Share the credentials with the application.
9096
self.database_provides.set_credentials(event.relation.id, user, password)

tests/unit/test_db.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def test_set_up_relation(
236236
self.assertTrue(self.harness.charm.legacy_db_relation.set_up_relation(relation))
237237
user = f"relation-{self.rel_id}"
238238
postgresql_mock.create_user.assert_called_once_with(user, "test-password", False)
239-
postgresql_mock.create_database.assert_called_once_with(DATABASE, user)
239+
postgresql_mock.create_database.assert_called_once_with(DATABASE, user, plugins=[])
240240
_enable_disable_extensions.assert_called_once()
241241
_update_endpoints.assert_called_once()
242242
_update_unit_status.assert_called_once()
@@ -263,7 +263,7 @@ def test_set_up_relation(
263263
)
264264
self.assertTrue(self.harness.charm.legacy_db_relation.set_up_relation(relation))
265265
postgresql_mock.create_user.assert_called_once_with(user, "test-password", False)
266-
postgresql_mock.create_database.assert_called_once_with(DATABASE, user)
266+
postgresql_mock.create_database.assert_called_once_with(DATABASE, user, plugins=[])
267267
_enable_disable_extensions.assert_called_once()
268268
_update_endpoints.assert_called_once()
269269
_update_unit_status.assert_called_once()
@@ -284,7 +284,9 @@ def test_set_up_relation(
284284
)
285285
self.assertTrue(self.harness.charm.legacy_db_relation.set_up_relation(relation))
286286
postgresql_mock.create_user.assert_called_once_with(user, "test-password", False)
287-
postgresql_mock.create_database.assert_called_once_with("application", user)
287+
postgresql_mock.create_database.assert_called_once_with(
288+
"application", user, plugins=[]
289+
)
288290
_enable_disable_extensions.assert_called_once()
289291
_update_endpoints.assert_called_once()
290292
_update_unit_status.assert_called_once()

tests/unit/test_postgresql_provider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def test_on_database_requested(
123123
postgresql_mock.create_user.assert_called_once_with(
124124
user, "test-password", extra_user_roles=EXTRA_USER_ROLES
125125
)
126-
postgresql_mock.create_database.assert_called_once_with(DATABASE, user)
126+
postgresql_mock.create_database.assert_called_once_with(DATABASE, user, plugins=[])
127127
postgresql_mock.get_postgresql_version.assert_called_once()
128128
_update_endpoints.assert_called_once()
129129

0 commit comments

Comments
 (0)