Skip to content

Commit 3e21943

Browse files
feat(pg_dump): Add '--if-exists' option for pg_dump (jazzband#478)
1 parent 9208919 commit 3e21943

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

dbbackup/db/postgresql.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class PgDumpBinaryConnector(PgDumpConnector):
100100
restore_cmd = "pg_restore"
101101
single_transaction = True
102102
drop = True
103+
if_exists = False
103104

104105
def _create_dump(self):
105106
cmd = f"{self.dump_cmd} "
@@ -120,6 +121,8 @@ def _restore_dump(self, dump):
120121
cmd += " --single-transaction"
121122
if self.drop:
122123
cmd += " --clean"
124+
if self.if_exists:
125+
cmd += " --if-exists"
123126
cmd = f"{self.restore_prefix} {cmd} {self.restore_suffix}"
124127
stdout, stderr = self.run_command(cmd, stdin=dump, env=self.restore_env)
125128
return stdout, stderr

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Unreleased
66

77
* Fix restore of database from S3 storage by reintroducing inputfile.seek(0) to utils.uncompress_file
88
* Fix bug where dbbackup management command would not respect settings.py:DBBACKUP_DATABASES
9+
* Add option `--if-exists` for pg_dump command
910

1011
4.1.0 (2024-01-14)
1112
------------------

docs/databases.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ This corresponds to ``--clean`` argument of ``pg_dump`` and ``pg_restore``.
166166

167167
Default: ``True``
168168

169+
IF_EXISTS
170+
~~~~
171+
172+
Use DROP ... IF EXISTS commands to drop objects in ``--clean`` mode of ``pg_dump``.
173+
174+
Default: ``False``
175+
169176
PostGIS
170177
-------
171178

0 commit comments

Comments
 (0)