From 3e2194384b965cfd83ee19e1f1ae748d893910cc Mon Sep 17 00:00:00 2001 From: Dmytro Litvinov Date: Tue, 5 Mar 2024 18:20:44 +0200 Subject: [PATCH] feat(pg_dump): Add '--if-exists' option for pg_dump (#478) --- dbbackup/db/postgresql.py | 3 +++ docs/changelog.rst | 1 + docs/databases.rst | 7 +++++++ 3 files changed, 11 insertions(+) diff --git a/dbbackup/db/postgresql.py b/dbbackup/db/postgresql.py index b7433d31..ccdcbe65 100644 --- a/dbbackup/db/postgresql.py +++ b/dbbackup/db/postgresql.py @@ -100,6 +100,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} " @@ -120,6 +121,8 @@ def _restore_dump(self, dump): cmd += " --single-transaction" if self.drop: cmd += " --clean" + 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 diff --git a/docs/changelog.rst b/docs/changelog.rst index 57ee1f10..6970bbd4 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -6,6 +6,7 @@ Unreleased * 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) ------------------ diff --git a/docs/databases.rst b/docs/databases.rst index ee36be76..83b58e83 100644 --- a/docs/databases.rst +++ b/docs/databases.rst @@ -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 -------