Skip to content

Commit 24a8f1a

Browse files
authored
Merge pull request #47 from zilder/promote
Add promote() method
2 parents 72d0373 + f5fe963 commit 24a8f1a

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

testgres/node.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -683,14 +683,50 @@ def reload(self, params=[]):
683683
_params = [
684684
get_bin_path("pg_ctl"),
685685
"-D", self.data_dir,
686-
"-w", # wait
687686
"reload"
688687
] + params # yapf: disable
689688

690689
execute_utility(_params, self.utils_log_file)
691690

692691
return self
693692

693+
def promote(self, dbname=None, username=None):
694+
"""
695+
Promote standby instance to master using pg_ctl. For PostgreSQL versions
696+
below 10 some additional actions required to ensure that instance
697+
became writable and hence `dbname` and `username` parameters may be
698+
needed.
699+
700+
Returns:
701+
This instance of :class:`.PostgresNode`.
702+
"""
703+
704+
_params = [
705+
get_bin_path("pg_ctl"),
706+
"-D", self.data_dir,
707+
"-w", # wait
708+
"promote"
709+
] # yapf: disable
710+
711+
execute_utility(_params, self.utils_log_file)
712+
713+
# for versions below 10 `promote` is asynchronous so we need to wait
714+
# until it actually becomes writable
715+
if self._pg_version < '10':
716+
check_query = "SELECT pg_is_in_recovery()"
717+
718+
self.poll_query_until(
719+
query=check_query,
720+
expected=False,
721+
dbname=dbname,
722+
username=username,
723+
max_attempts=0) # infinite
724+
725+
# node becomes master itself
726+
self._master = None
727+
728+
return self
729+
694730
def pg_ctl(self, params):
695731
"""
696732
Invoke pg_ctl with params.

tests/test_simple.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,20 @@ def test_incorrect_catchup(self):
529529
with self.assertRaises(TestgresException):
530530
node.catchup()
531531

532+
def test_promotion(self):
533+
with get_new_node() as master:
534+
master.init().start()
535+
master.safe_psql('create table abc(id serial)')
536+
537+
with master.replicate().start() as replica:
538+
master.stop()
539+
replica.promote()
540+
541+
# make standby becomes writable master
542+
replica.safe_psql('insert into abc values (1)')
543+
res = replica.safe_psql('select * from abc')
544+
self.assertEqual(res, b'1\n')
545+
532546
def test_dump(self):
533547
query_create = 'create table test as select generate_series(1, 2) as val'
534548
query_select = 'select * from test order by val asc'

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy