Skip to content

Commit

Permalink
Extensions not enabled when database is created
Browse files Browse the repository at this point in the history
  • Loading branch information
dragomirp committed Oct 16, 2023
1 parent de10986 commit 51e25ce
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
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 @@ -168,7 +168,13 @@ def set_up_relation(self, relation: Relation) -> bool:
self.charm.set_secret(APP_SCOPE, f"{user}-database", database)

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 @@ -84,7 +84,13 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
user = f"relation-{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
6 changes: 3 additions & 3 deletions tests/unit/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def test_set_up_relation(
self.assertTrue(self.harness.charm.legacy_db_relation.set_up_relation(relation))
user = f"relation-{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()
_update_endpoints.assert_called_once()
_update_unit_status.assert_called_once()
Expand All @@ -263,7 +263,7 @@ def test_set_up_relation(
)
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()
_update_endpoints.assert_called_once()
_update_unit_status.assert_called_once()
Expand All @@ -284,7 +284,7 @@ def test_set_up_relation(
)
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("application", user)
postgresql_mock.create_database.assert_called_once_with("application", user, plugins=[])
_enable_disable_extensions.assert_called_once()
_update_endpoints.assert_called_once()
_update_unit_status.assert_called_once()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_on_database_requested(
postgresql_mock.create_user.assert_called_once_with(
user, "test-password", extra_user_roles=EXTRA_USER_ROLES
)
postgresql_mock.create_database.assert_called_once_with(DATABASE, user)
postgresql_mock.create_database.assert_called_once_with(DATABASE, user, plugins=[])
postgresql_mock.get_postgresql_version.assert_called_once()
_update_endpoints.assert_called_once()

Expand Down

0 comments on commit 51e25ce

Please sign in to comment.