Skip to content

Commit a1b24dd

Browse files
committed
fix record of postgres version in databags
1 parent 263a1ef commit a1b24dd

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

src/charm.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
MONITORING_USER,
7272
PATRONI_CONF_PATH,
7373
PEER,
74+
UPGRADE_RELATION,
7475
POSTGRESQL_SNAP_NAME,
7576
REPLICATION_PASSWORD_KEY,
7677
REWIND_PASSWORD_KEY,
@@ -95,7 +96,9 @@
9596

9697
PRIMARY_NOT_REACHABLE_MESSAGE = "waiting for primary to be reachable from this unit"
9798
EXTENSIONS_DEPENDENCY_MESSAGE = "Unsatisfied plugin dependencies. Please check the logs"
98-
DIFFERENT_VERSIONS_PSQL_BLOCKING_MESSAGE = "Please select the correct version of postgresql to use. No need to use different versions of postgresql."
99+
DIFFERENT_VERSIONS_PSQL_BLOCKING_MESSAGE = (
100+
"Please select the correct version of postgresql to use. You cannot use different versions of postgresql!"
101+
)
99102

100103
Scopes = Literal[APP_SCOPE, UNIT_SCOPE]
101104

@@ -157,6 +160,7 @@ def __init__(self, *args):
157160
self.framework.observe(self.on.config_changed, self._on_config_changed)
158161
self.framework.observe(self.on.get_primary_action, self._on_get_primary)
159162
self.framework.observe(self.on[PEER].relation_changed, self._on_peer_relation_changed)
163+
self.framework.observe(self.on[UPGRADE_RELATION].relation_changed, self._on_upgrade_relation_changed)
160164
self.framework.observe(self.on.secret_changed, self._on_peer_relation_changed)
161165
self.framework.observe(self.on.secret_remove, self._on_peer_relation_changed)
162166
self.framework.observe(self.on[PEER].relation_departed, self._on_peer_relation_departed)
@@ -535,6 +539,17 @@ def _on_peer_relation_changed(self, event: HookEvent):
535539

536540
self._validate_database_version()
537541

542+
def _on_upgrade_relation_changed(self, event: HookEvent):
543+
if not self.unit.is_leader():
544+
return
545+
546+
if self.upgrade.idle:
547+
logger.debug("Defer _on_upgrade_relation_changed: upgrade in progress")
548+
event.defer()
549+
return
550+
551+
self._set_workload_version(self._patroni.get_postgresql_version())
552+
538553
# Split off into separate function, because of complexity _on_peer_relation_changed
539554
def _start_stop_pgbackrest_service(self, event: HookEvent) -> None:
540555
# Start or stop the pgBackRest TLS server service when TLS certificate change.
@@ -1020,7 +1035,7 @@ def _on_start(self, event: StartEvent) -> None:
10201035

10211036
self.unit_peer_data.update({"ip": self.get_hostname_by_unit(None)})
10221037

1023-
self.unit.set_workload_version(self._patroni.get_postgresql_version())
1038+
self._set_workload_version(self._patroni.get_postgresql_version())
10241039

10251040
# Open port
10261041
try:
@@ -1596,6 +1611,12 @@ def client_relations(self) -> List[Relation]:
15961611
relations.append(relation)
15971612
return relations
15981613

1614+
def _set_workload_version(self, psql_version):
1615+
"""Record the version of the software running as the workload. Also writes the version into the databags"""
1616+
self.unit.set_workload_version(psql_version)
1617+
if self.unit.is_leader():
1618+
self.app_peer_data.update({"database-version": psql_version})
1619+
15991620
def _validate_database_version(self):
16001621
"""Checking that only one version of Postgres is used."""
16011622
peer_db_version = self.app_peer_data.get("database-version")

src/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
LEGACY_DB = "db"
1111
LEGACY_DB_ADMIN = "db-admin"
1212
PEER = "database-peers"
13+
UPGRADE_RELATION = "upgrade"
1314
ALL_CLIENT_RELATIONS = [DATABASE, LEGACY_DB, LEGACY_DB_ADMIN]
1415
API_REQUEST_TIMEOUT = 5
1516
PATRONI_CLUSTER_STATUS_ENDPOINT = "cluster"

tests/integration/ha_tests/helpers.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
PATRONI_SERVICE_DEFAULT_PATH = f"/etc/systemd/system/{SERVICE_NAME}"
3939
RESTART_CONDITION = "no"
4040
ORIGINAL_RESTART_CONDITION = "always"
41+
SECOND_APPLICATION = "second-cluster"
4142

4243

4344
class MemberNotListedOnClusterError(Exception):
@@ -872,10 +873,21 @@ async def reused_full_cluster_recovery_storage(ops_test: OpsTest, unit_name) ->
872873

873874

874875
async def get_db_connection(ops_test, dbname, is_primary=True, replica_unit_name=""):
876+
"""Returns a PostgreSQL connection string.
877+
878+
Args:
879+
ops_test: The ops test framework instance
880+
dbname: The name of the database
881+
is_primary: Whether to use a primary unit (default is True, so it uses the primary
882+
replica_unit_name: The name of the replica unit
883+
884+
Returns:
885+
a PostgreSQL connection string
886+
"""
875887
unit_name = await get_primary(ops_test, APP_NAME)
876888
password = await get_password(ops_test, APP_NAME)
877889
address = get_unit_address(ops_test, unit_name)
878-
if not is_primary and unit_name != "":
890+
if not is_primary and replica_unit_name != "":
879891
unit_name = replica_unit_name
880892
address = ops_test.model.applications[APP_NAME].units[unit_name].public_address
881893
connection_string = (
@@ -886,6 +898,11 @@ async def get_db_connection(ops_test, dbname, is_primary=True, replica_unit_name
886898

887899

888900
async def validate_test_data(connection_string):
901+
"""Checking test data.
902+
903+
Args:
904+
connection_string: Database connection string
905+
"""
889906
with psycopg2.connect(connection_string) as connection:
890907
connection.autocommit = True
891908
with connection.cursor() as cursor:
@@ -896,6 +913,11 @@ async def validate_test_data(connection_string):
896913

897914

898915
async def create_test_data(connection_string):
916+
"""Creating test data in the database.
917+
918+
Args:
919+
connection_string: Database connection string
920+
"""
899921
with psycopg2.connect(connection_string) as connection:
900922
connection.autocommit = True
901923
with connection.cursor() as cursor:
@@ -911,6 +933,16 @@ async def create_test_data(connection_string):
911933

912934

913935
async def get_last_added_unit(ops_test, app, prev_units):
936+
"""Returns a unit.
937+
938+
Args:
939+
ops_test: The ops test framework instance
940+
app: The name of the application
941+
prev_units: List of unit names before adding the last unit
942+
943+
Returns:
944+
last added unit
945+
"""
914946
curr_units = [unit.name for unit in ops_test.model.applications[app].units]
915947
new_unit = list(set(curr_units) - set(prev_units))[0]
916948
for unit in ops_test.model.applications[app].units:

tests/integration/ha_tests/test_restore_cluster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
add_unit_with_storage,
2121
reused_full_cluster_recovery_storage,
2222
storage_id,
23+
SECOND_APPLICATION,
2324
)
2425

2526
FIRST_APPLICATION = "first-cluster"
26-
SECOND_APPLICATION = "second-cluster"
2727

2828
logger = logging.getLogger(__name__)
2929

tests/integration/ha_tests/test_self_healing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
update_restart_condition,
5757
validate_test_data,
5858
wait_network_restore,
59+
SECOND_APPLICATION,
5960
)
60-
from .test_restore_cluster import SECOND_APPLICATION
6161

6262
logger = logging.getLogger(__name__)
6363

tests/integration/helpers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
DATABASE_APP_NAME = METADATA["name"]
3636
STORAGE_PATH = METADATA["storage"]["pgdata"]["location"]
3737
APPLICATION_NAME = "postgresql-test-app"
38-
FIRST_DATABASE_RELATION_NAME = "first-database"
3938

4039

4140
async def build_connection_string(

0 commit comments

Comments
 (0)