diff --git a/testgres/node.py b/testgres/node.py index 9978dcf6..b597dd69 100644 --- a/testgres/node.py +++ b/testgres/node.py @@ -683,7 +683,6 @@ def reload(self, params=[]): _params = [ get_bin_path("pg_ctl"), "-D", self.data_dir, - "-w", # wait "reload" ] + params # yapf: disable @@ -691,6 +690,43 @@ def reload(self, params=[]): return self + def promote(self, dbname=None, username=None): + """ + Promote standby instance to master using pg_ctl. For PostgreSQL versions + below 10 some additional actions required to ensure that instance + became writable and hence `dbname` and `username` parameters may be + needed. + + Returns: + This instance of :class:`.PostgresNode`. + """ + + _params = [ + get_bin_path("pg_ctl"), + "-D", self.data_dir, + "-w", # wait + "promote" + ] # yapf: disable + + execute_utility(_params, self.utils_log_file) + + # for versions below 10 `promote` is asynchronous so we need to wait + # until it actually becomes writable + if self._pg_version < '10': + check_query = "SELECT pg_is_in_recovery()" + + self.poll_query_until( + query=check_query, + expected=False, + dbname=dbname, + username=username, + max_attempts=0) # infinite + + # node becomes master itself + self._master = None + + return self + def pg_ctl(self, params): """ Invoke pg_ctl with params. diff --git a/tests/test_simple.py b/tests/test_simple.py index 230cff47..91348f34 100755 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -529,6 +529,20 @@ def test_incorrect_catchup(self): with self.assertRaises(TestgresException): node.catchup() + def test_promotion(self): + with get_new_node() as master: + master.init().start() + master.safe_psql('create table abc(id serial)') + + with master.replicate().start() as replica: + master.stop() + replica.promote() + + # make standby becomes writable master + replica.safe_psql('insert into abc values (1)') + res = replica.safe_psql('select * from abc') + self.assertEqual(res, b'1\n') + def test_dump(self): query_create = 'create table test as select generate_series(1, 2) as val' query_select = 'select * from test order by val asc'