From 1c26b21f2ecc46b2e7611189dd2f23c6d1127447 Mon Sep 17 00:00:00 2001 From: Dmytro Litvinov Date: Tue, 5 Mar 2024 18:20:44 +0200 Subject: [PATCH 1/3] feat(pg_dump): Add '--if-exists' option for pg_dump (#478) --- dbbackup/db/postgresql.py | 4 ++++ docs/changelog.rst | 1 + docs/databases.rst | 7 +++++++ 3 files changed, 12 insertions(+) diff --git a/dbbackup/db/postgresql.py b/dbbackup/db/postgresql.py index 91f1577..198ddf0 100644 --- a/dbbackup/db/postgresql.py +++ b/dbbackup/db/postgresql.py @@ -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} " @@ -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 diff --git a/docs/changelog.rst b/docs/changelog.rst index 4638af3..412ac9a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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) ------------------ diff --git a/docs/databases.rst b/docs/databases.rst index ac04af4..5832017 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 ------- From 62048411ff5beac4beac1578f686824214f1f33a Mon Sep 17 00:00:00 2001 From: Will Nilges Date: Tue, 2 Apr 2024 01:19:38 -0400 Subject: [PATCH 2/3] Add simple if_exists test --- dbbackup/tests/test_connectors/test_postgresql.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/dbbackup/tests/test_connectors/test_postgresql.py b/dbbackup/tests/test_connectors/test_postgresql.py index f1923ce..5df7add 100644 --- a/dbbackup/tests/test_connectors/test_postgresql.py +++ b/dbbackup/tests/test_connectors/test_postgresql.py @@ -213,6 +213,16 @@ def test_create_dump_drop(self, mock_dump_cmd): self.connector.create_dump() self.assertNotIn(" --clean", mock_dump_cmd.call_args[0][0]) + def test_create_dump_if_exists(self, mock_dump_cmd): + # Without + self.connector.if_exists = False + self.connector.create_dump() + self.assertNotIn(" --if-exists", mock_dump_cmd.call_args[0][0]) + # With + self.connector.if_exists = True + self.connector.create_dump() + self.assertIn(" --if-exists", mock_dump_cmd.call_args[0][0]) + @patch( "dbbackup.db.postgresql.PgDumpBinaryConnector.run_command", return_value=(BytesIO(), BytesIO()), From 2e2828d10816536b8e3036caf74c303cffe25288 Mon Sep 17 00:00:00 2001 From: Will Nilges Date: Fri, 5 Apr 2024 20:42:50 -0400 Subject: [PATCH 3/3] Flip if_exists test to check restore, not create --- dbbackup/tests/test_connectors/test_postgresql.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/dbbackup/tests/test_connectors/test_postgresql.py b/dbbackup/tests/test_connectors/test_postgresql.py index 5df7add..a7b6e54 100644 --- a/dbbackup/tests/test_connectors/test_postgresql.py +++ b/dbbackup/tests/test_connectors/test_postgresql.py @@ -213,15 +213,16 @@ def test_create_dump_drop(self, mock_dump_cmd): self.connector.create_dump() self.assertNotIn(" --clean", mock_dump_cmd.call_args[0][0]) - def test_create_dump_if_exists(self, mock_dump_cmd): + def test_create_dump_if_exists(self, mock_dump_cmd, mock_restore_cmd): + dump = self.connector.create_dump() # Without self.connector.if_exists = False - self.connector.create_dump() - self.assertNotIn(" --if-exists", mock_dump_cmd.call_args[0][0]) + self.connector.restore_dump(dump) + self.assertNotIn(" --if-exists", mock_restore_cmd.call_args[0][0]) # With self.connector.if_exists = True - self.connector.create_dump() - self.assertIn(" --if-exists", mock_dump_cmd.call_args[0][0]) + self.connector.restore_dump(dump) + self.assertIn(" --if-exists", mock_restore_cmd.call_args[0][0]) @patch( "dbbackup.db.postgresql.PgDumpBinaryConnector.run_command",