Skip to content

Add --if-not-exists flag to receive-wal #1065

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion barman/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,11 @@ def archive_wal(args):
help="create the replication slot, if it does not exist",
action="store_true",
),
argument(
"--if-not-exists",
help="Do not error out when --create-slot is specified and a slot with the specified name already exists.",
action="store_true",
),
argument(
"--drop-slot",
help="drop the replication slot, if it exists",
Expand Down Expand Up @@ -1764,7 +1769,7 @@ def receive_wal(args):
server.kill("receive-wal")
elif args.create_slot:
with closing(server):
server.create_physical_repslot()
server.create_physical_repslot(ignore_duplicate=args.if_not_exists)
elif args.drop_slot:
with closing(server):
server.drop_repslot()
Expand Down
10 changes: 8 additions & 2 deletions barman/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2908,7 +2908,7 @@ def archive_wal(self, verbose=True):
"on server %s. Skipping to the next server" % self.config.name
)

def create_physical_repslot(self):
def create_physical_repslot(self, ignore_duplicate=False):
"""
Create a physical replication slot using the streaming connection
"""
Expand Down Expand Up @@ -2951,7 +2951,13 @@ def create_physical_repslot(self):
self.streaming.create_physical_repslot(self.config.slot_name)
output.info("Replication slot '%s' created", self.config.slot_name)
except PostgresDuplicateReplicationSlot:
output.error("Replication slot '%s' already exists", self.config.slot_name)
if ignore_duplicate:
output.info(
"Replication slot '%s' already exists and --if-not-exists was specified. Skipping creation",
self.config.slot_name,
)
else:
output.error("Replication slot '%s' already exists", self.config.slot_name)
except PostgresReplicationSlotsFull:
output.error(
"All replication slots for server '%s' are in use\n"
Expand Down
5 changes: 5 additions & 0 deletions docs/user_guide/commands/barman/receive_wal.inc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Synopsis

receive-wal
[ --create-slot ]
[ --if-not-exists ]
[ --drop-slot ]
[ { -h | --help } ]
[ --reset ]
Expand All @@ -33,6 +34,10 @@ Parameters
Create the physical replication slot configured with the ``slot_name`` configuration
parameter.

``--if-not-exists``
Do not error out when --create-slot is specified and a slot with the specified name
already exists.

``--drop-slot``
Drop the physical replication slot configured with the ``slot_name`` configuration
parameter.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,14 @@ def test_create_physical_repslot(self, capsys):
out, err = capsys.readouterr()
assert "Replication slot 'test_repslot' already exists" in err

# If the replication slot was already created but duplicates are ignored
# check that no error is reported
create_physical_repslot.side_effect = PostgresDuplicateReplicationSlot
server.create_physical_repslot(ignore_duplicate=True)
create_physical_repslot.assert_called_with("test_repslot")
out, err = capsys.readouterr()
assert not err

# Test the method failure if the replication slots
# on the server are all taken
create_physical_repslot.side_effect = PostgresReplicationSlotsFull
Expand Down