Skip to content

Commit

Permalink
Update postgresql lib (#546)
Browse files Browse the repository at this point in the history
Signed-off-by: Marcelo Henrique Neppel <[email protected]>
  • Loading branch information
marceloneppel authored Jul 4, 2024
1 parent b657618 commit e5bfc9d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/charms/postgresql_k8s/v0/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

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

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

Expand Down Expand Up @@ -79,6 +79,10 @@ class PostgreSQLEnableDisableExtensionError(Exception):
"""Exception raised when enabling/disabling an extension fails."""


class PostgreSQLGetLastArchivedWALError(Exception):
"""Exception raised when retrieving last archived WAL fails."""


class PostgreSQLGetPostgreSQLVersionError(Exception):
"""Exception raised when retrieving PostgreSQL version fails."""

Expand Down Expand Up @@ -383,6 +387,16 @@ def _generate_database_privileges_statements(
)
return statements

def get_last_archived_wal(self) -> str:
"""Get the name of the last archived wal for the current PostgreSQL cluster."""
try:
with self._connect_to_database() as connection, connection.cursor() as cursor:
cursor.execute("SELECT last_archived_wal FROM pg_stat_archiver;")
return cursor.fetchone()[0]
except psycopg2.Error as e:
logger.error(f"Failed to get PostgreSQL last archived WAL: {e}")
raise PostgreSQLGetLastArchivedWALError()

def get_postgresql_text_search_configs(self) -> Set[str]:
"""Returns the PostgreSQL available text search configs.
Expand Down
28 changes: 27 additions & 1 deletion tests/unit/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import psycopg2
import pytest
from charms.postgresql_k8s.v0.postgresql import PostgreSQLCreateDatabaseError
from charms.postgresql_k8s.v0.postgresql import (
PostgreSQLCreateDatabaseError,
PostgreSQLGetLastArchivedWALError,
)
from ops.testing import Harness
from psycopg2.sql import SQL, Composed, Identifier

Expand Down Expand Up @@ -232,6 +235,29 @@ def test_generate_database_privileges_statements(harness):
]


def test_get_last_archived_wal(harness):
with patch(
"charms.postgresql_k8s.v0.postgresql.PostgreSQL._connect_to_database"
) as _connect_to_database:
# Test a successful call.
execute = _connect_to_database.return_value.__enter__.return_value.cursor.return_value.__enter__.return_value.execute
_connect_to_database.return_value.__enter__.return_value.cursor.return_value.__enter__.return_value.fetchone.return_value = (
"000000010000000100000001",
)
assert harness.charm.postgresql.get_last_archived_wal() == "000000010000000100000001"
execute.assert_called_once_with("SELECT last_archived_wal FROM pg_stat_archiver;")

# Test a failed call.
execute.reset_mock()
execute.side_effect = psycopg2.Error
try:
harness.charm.postgresql.get_last_archived_wal()
assert False
except PostgreSQLGetLastArchivedWALError:
pass
execute.assert_called_once_with("SELECT last_archived_wal FROM pg_stat_archiver;")


def test_build_postgresql_parameters(harness):
# Test when not limit is imposed to the available memory.
config_options = {
Expand Down

0 comments on commit e5bfc9d

Please sign in to comment.