Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit71e487e

Browse files
committed
inline _create_replication_slot() to _create_recovery_conf()
1 parentb4051cf commit71e487e

File tree

3 files changed

+31
-40
lines changed

3 files changed

+31
-40
lines changed

‎testgres/backup.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,13 @@ def spawn_primary(self, name=None, destroy=True):
154154

155155
returnnode
156156

157-
defspawn_replica(self,name=None,destroy=True,slot_name=None):
157+
defspawn_replica(self,name=None,destroy=True,slot=None):
158158
"""
159159
Create a replica of the original node from a backup.
160160
161161
Args:
162162
name: replica's application name.
163+
slot: create a replication slot with the specified name.
163164
destroy: should we convert this backup into a node?
164165
165166
Returns:
@@ -171,11 +172,11 @@ def spawn_replica(self, name=None, destroy=True, slot_name=None):
171172

172173
# Assign it a master and a recovery file (private magic)
173174
node._assign_master(self.original_node)
174-
node._create_recovery_conf(username=self.username,slot_name=slot_name)
175+
node._create_recovery_conf(username=self.username,slot=slot)
175176

176177
returnnode
177178

178179
defcleanup(self):
179180
ifself._available:
180-
rmtree(self.base_dir,ignore_errors=True)
181181
self._available=False
182+
rmtree(self.base_dir,ignore_errors=True)

‎testgres/node.py

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def _assign_master(self, master):
278278
# now this node has a master
279279
self._master=master
280280

281-
def_create_recovery_conf(self,username,slot_name=None):
281+
def_create_recovery_conf(self,username,slot=None):
282282
"""NOTE: this is a private method!"""
283283

284284
# fetch master of this node
@@ -306,8 +306,26 @@ def _create_recovery_conf(self, username, slot_name=None):
306306
"standby_mode=on\n"
307307
).format(conninfo)
308308

309-
ifslot_name:
310-
line+="primary_slot_name={}\n".format(slot_name)
309+
ifslot:
310+
# Connect to master for some additional actions
311+
withmaster.connect(username=username)ascon:
312+
# check if slot already exists
313+
res=con.execute("""
314+
select exists (
315+
select from pg_catalog.pg_replication_slots
316+
where slot_name = $1
317+
)
318+
""",slot)
319+
320+
ifres[0][0]:
321+
raiseTestgresException("Slot '{}' already exists".format(slot))
322+
323+
# TODO: we should drop this slot after replica's cleanup()
324+
con.execute("""
325+
select pg_catalog.pg_create_physical_replication_slot($1)
326+
""",slot)
327+
328+
line+="primary_slot_name={}\n".format(slot)
311329

312330
self.append_conf(RECOVERY_CONF_FILE,line)
313331

@@ -352,28 +370,6 @@ def _collect_special_files(self):
352370

353371
returnresult
354372

355-
def_create_replication_slot(self,slot_name,dbname=None,username=None):
356-
"""
357-
Create a physical replication slot.
358-
359-
Args:
360-
slot_name: slot name
361-
dbname: database name
362-
username: database user name
363-
"""
364-
rs=self.execute("select exists (select * from pg_replication_slots "
365-
"where slot_name = '{}')".format(slot_name),
366-
dbname=dbname,username=username)
367-
368-
ifrs[0][0]:
369-
raiseTestgresException("Slot '{}' already exists".format(slot_name))
370-
371-
query= (
372-
"select pg_create_physical_replication_slot('{}')"
373-
).format(slot_name)
374-
375-
self.execute(query=query,dbname=dbname,username=username)
376-
377373
definit(self,initdb_params=None,**kwargs):
378374
"""
379375
Perform initdb for this node.
@@ -969,26 +965,24 @@ def backup(self, **kwargs):
969965

970966
returnNodeBackup(node=self,**kwargs)
971967

972-
defreplicate(self,name=None,slot_name=None,**kwargs):
968+
defreplicate(self,name=None,slot=None,**kwargs):
973969
"""
974970
Create a binary replica of this node.
975971
976972
Args:
977973
name: replica's application name.
974+
slot: create a replication slot with the specified name.
978975
username: database user name.
979976
xlog_method: a method for collecting the logs ('fetch' | 'stream').
980977
base_dir: the base directory for data files and logs
981978
"""
982979

983-
ifslot_name:
984-
self._create_replication_slot(slot_name,**kwargs)
985-
986980
backup=self.backup(**kwargs)
987981

988982
# transform backup into a replica
989983
returnbackup.spawn_replica(name=name,
990984
destroy=True,
991-
slot_name=slot_name)
985+
slot=slot)
992986

993987
defcatchup(self,dbname=None,username=None):
994988
"""

‎tests/test_simple.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -384,19 +384,15 @@ def test_replicate(self):
384384
self.assertListEqual(res, [])
385385

386386
deftest_replication_slots(self):
387-
query_create='create table test as select generate_series(1, 2) as val'
388-
389387
withget_new_node()asnode:
390388
node.init(allow_streaming=True).start()
391-
node.execute(query_create)
392389

393-
withnode.replicate(slot_name='slot1').start()asreplica:
394-
res=replica.execute('select * from test')
395-
self.assertListEqual(res, [(1, ), (2, )])
390+
withnode.replicate(slot='slot1').start()asreplica:
391+
replica.execute('select 1')
396392

397393
# cannot create new slot with the same name
398394
withself.assertRaises(TestgresException):
399-
node._create_replication_slot('slot1')
395+
node.replicate(slot='slot1')
400396

401397
deftest_incorrect_catchup(self):
402398
withget_new_node()asnode:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp