Skip to content

Add promote() method #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 4, 2018
38 changes: 37 additions & 1 deletion testgres/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,14 +683,50 @@ def reload(self, params=[]):
_params = [
get_bin_path("pg_ctl"),
"-D", self.data_dir,
"-w", # wait
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

"reload"
] + params # yapf: disable

execute_utility(_params, self.utils_log_file)

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 = "SHOW transaction_read_only"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not safe since transactions might be read-only by default.


self.poll_query_until(
query=check_query,
expected="off",
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.
Expand Down
14 changes: 14 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down