Skip to content

Commit

Permalink
[DPE-2569] Extensions not enabled when database is created (#290)
Browse files Browse the repository at this point in the history
* Bump libs

* Enable configured extensions when a database is created
  • Loading branch information
dragomirp authored Oct 17, 2023
1 parent b4c51b0 commit 68b92c3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/charms/data_platform_libs/v0/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def restart(self, event) -> None:

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 13
LIBPATCH = 14

PYDEPS = ["pydantic>=1.10,<2", "poetry-core"]

Expand Down Expand Up @@ -895,6 +895,10 @@ def _on_upgrade_charm(self, event: UpgradeCharmEvent) -> None:
self.charm.unit.status = WaitingStatus("other units upgrading first...")
self.peer_relation.data[self.charm.unit].update({"state": "ready"})

if self.charm.app.planned_units() == 1:
# single unit upgrade, emit upgrade_granted event right away
getattr(self.on, "upgrade_granted").emit()

else:
# for k8s run version checks only on highest ordinal unit
if (
Expand Down
7 changes: 6 additions & 1 deletion lib/charms/postgresql_k8s/v0/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ def _connect_to_database(
connection.autocommit = True
return connection

def create_database(self, database: str, user: str) -> None:
def create_database(self, database: str, user: str, plugins: List[str] = []) -> None:
"""Creates a new database and grant privileges to a user on it.
Args:
database: database to be created.
user: user that will have access to the database.
plugins: extensions to enable in the new database.
"""
try:
connection = self._connect_to_database()
Expand Down Expand Up @@ -170,6 +171,10 @@ def create_database(self, database: str, user: str) -> None:
logger.error(f"Failed to create database: {e}")
raise PostgreSQLCreateDatabaseError()

# Enable preset extensions
for plugin in plugins:
self.enable_disable_extension(plugin, True, database)

def create_user(
self, user: str, password: str = None, admin: bool = False, extra_user_roles: str = None
) -> None:
Expand Down
8 changes: 7 additions & 1 deletion src/relations/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,13 @@ def set_up_relation(self, relation: Relation) -> bool:
user = f"relation_id_{relation.id}"
password = unit_relation_databag.get("password", new_password())
self.charm.postgresql.create_user(user, password, self.admin)
self.charm.postgresql.create_database(database, user)
plugins = [
"_".join(plugin.split("_")[1:-1])
for plugin in self.charm.config.plugin_keys()
if self.charm.config[plugin]
]

self.charm.postgresql.create_database(database, user, plugins=plugins)

# Enable/disable extensions in the new database.
self.charm.enable_disable_extensions(database)
Expand Down
8 changes: 7 additions & 1 deletion src/relations/postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
user = f"relation_id_{event.relation.id}"
password = new_password()
self.charm.postgresql.create_user(user, password, extra_user_roles=extra_user_roles)
self.charm.postgresql.create_database(database, user)
plugins = [
"_".join(plugin.split("_")[1:-1])
for plugin in self.charm.config.plugin_keys()
if self.charm.config[plugin]
]

self.charm.postgresql.create_database(database, user, plugins=plugins)

# Share the credentials with the application.
self.database_provides.set_credentials(event.relation.id, user, password)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def test_set_up_relation(
self.assertTrue(self.harness.charm.legacy_db_relation.set_up_relation(relation))
user = f"relation_id_{self.rel_id}"
postgresql_mock.create_user.assert_called_once_with(user, "test-password", False)
postgresql_mock.create_database.assert_called_once_with(DATABASE, user)
postgresql_mock.create_database.assert_called_once_with(DATABASE, user, plugins=[])
_enable_disable_extensions.assert_called_once()
self.assertEqual(postgresql_mock.get_postgresql_version.call_count, 2)
_update_unit_status.assert_called_once()
Expand Down Expand Up @@ -288,7 +288,7 @@ def test_set_up_relation(
self.clear_relation_data()
self.assertTrue(self.harness.charm.legacy_db_relation.set_up_relation(relation))
postgresql_mock.create_user.assert_called_once_with(user, "test-password", False)
postgresql_mock.create_database.assert_called_once_with(DATABASE, user)
postgresql_mock.create_database.assert_called_once_with(DATABASE, user, plugins=[])
_enable_disable_extensions.assert_called_once()
self.assertEqual(postgresql_mock.get_postgresql_version.call_count, 2)
_update_unit_status.assert_called_once()
Expand Down

0 comments on commit 68b92c3

Please sign in to comment.