Skip to content

Commit

Permalink
handling different versions of Postgres of unit
Browse files Browse the repository at this point in the history
  • Loading branch information
BalabaDmitri committed Apr 12, 2024
1 parent 18211ed commit a63da74
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@

PRIMARY_NOT_REACHABLE_MESSAGE = "waiting for primary to be reachable from this unit"
EXTENSIONS_DEPENDENCY_MESSAGE = "Unsatisfied plugin dependencies. Please check the logs"
DIFFERENT_VERSIONS_PSQL_BLOCKING_MESSAGE = "Please select the correct version of postgresql to use. No need to use different versions of postgresql."

Scopes = Literal[APP_SCOPE, UNIT_SCOPE]

Expand Down Expand Up @@ -519,6 +520,8 @@ def _on_peer_relation_changed(self, event: HookEvent):

self._update_new_unit_status()

self._validate_database_version()

# Split off into separate function, because of complexity _on_peer_relation_changed
def _start_stop_pgbackrest_service(self, event: HookEvent) -> None:
# Start or stop the pgBackRest TLS server service when TLS certificate change.
Expand Down Expand Up @@ -1577,6 +1580,18 @@ def client_relations(self) -> List[Relation]:
relations.append(relation)
return relations

def _validate_database_version(self):
"""Checking that only one version of Postgres is used."""
peer_db_version = self.app_peer_data.get("database-version")

if self.unit.is_leader() and peer_db_version is None:
self.app_peer_data.update({"database-version": self._patroni.get_postgresql_version()})
return

if peer_db_version != self._patroni.get_postgresql_version():
self.unit.status = BlockedStatus(DIFFERENT_VERSIONS_PSQL_BLOCKING_MESSAGE)
return


if __name__ == "__main__":
main(PostgresqlOperatorCharm)
2 changes: 2 additions & 0 deletions tests/integration/ha_tests/test_self_healing.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,8 @@ async def test_deploy_zero_units(ops_test: OpsTest, charm):
logger.info("connect to DB and create test table")
await create_test_data(connection_string)

# TODO: implement a test using the store with a different version of Postgres

unit_ip_addresses = []
primary_storage = ""
for unit in ops_test.model.applications[app].units:
Expand Down

0 comments on commit a63da74

Please sign in to comment.