Skip to content
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

feat(pg_dump): Add '--if-exists' option for pg_dump (and add tests) #515

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(pg_dump): Add '--if-exists' option for pg_dump (#478)
  • Loading branch information
DmytroLitvinov authored and WillNilges committed Apr 2, 2024
commit 1c26b21f2ecc46b2e7611189dd2f23c6d1127447
4 changes: 4 additions & 0 deletions dbbackup/db/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class PgDumpBinaryConnector(PgDumpConnector):
restore_cmd = "pg_restore"
single_transaction = True
drop = True
if_exists = False

def _create_dump(self):
cmd = f"{self.dump_cmd} "
Expand Down Expand Up @@ -145,6 +146,9 @@ def _restore_dump(self, dump):
if self.schemas:
cmd += " -n " + " -n ".join(self.schemas)

if self.if_exists:
cmd += " --if-exists"

cmd = f"{self.restore_prefix} {cmd} {self.restore_suffix}"
stdout, stderr = self.run_command(cmd, stdin=dump, env=self.restore_env)
return stdout, stderr
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Unreleased
* Add PostgreSQL Schema support by @angryfoxx in https://github.com/jazzband/django-dbbackup/pull/507
* Fix restore of database from S3 storage by reintroducing inputfile.seek(0) to utils.uncompress_file
* Fix bug where dbbackup management command would not respect settings.py:DBBACKUP_DATABASES
* Add option `--if-exists` for pg_dump command

4.1.0 (2024-01-14)
------------------
Expand Down
7 changes: 7 additions & 0 deletions docs/databases.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ This corresponds to ``--clean`` argument of ``pg_dump`` and ``pg_restore``.

Default: ``True``

IF_EXISTS
~~~~

Use DROP ... IF EXISTS commands to drop objects in ``--clean`` mode of ``pg_dump``.

Default: ``False``

PostGIS
-------

Expand Down