From 3e2194384b965cfd83ee19e1f1ae748d893910cc 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 | 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 ------- From f1b0d98b110f4439a00361378268eedd4475440a Mon Sep 17 00:00:00 2001 From: Will Nilges Date: Tue, 2 Apr 2024 01:19:38 -0400 Subject: [PATCH 2/3] test(postgres): 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 9bc039bb..9cd2c69e 100644 --- a/dbbackup/tests/test_connectors/test_postgresql.py +++ b/dbbackup/tests/test_connectors/test_postgresql.py @@ -178,6 +178,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 5d27e34671a7e81351e317036009a93b5449cd2e 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 5df7adda..a7b6e543 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",