From 35b831fd6446a4e8c85d0b909a7dca87a82991dd Mon Sep 17 00:00:00 2001 From: Tobias McNulty Date: Mon, 29 Jan 2024 20:54:46 -0500 Subject: [PATCH] Respect `DBBACKUP_DATABASES` in `dbbackup` management command (#492) Co-authored-by: Mark Bakhit Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Archmonger <16909269+Archmonger@users.noreply.github.com> --- dbbackup/management/commands/dbbackup.py | 6 ++++-- dbbackup/tests/commands/test_dbbackup.py | 10 ++++++++++ docs/changelog.rst | 1 + 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/dbbackup/management/commands/dbbackup.py b/dbbackup/management/commands/dbbackup.py index 7172e12f..75f0c444 100644 --- a/dbbackup/management/commands/dbbackup.py +++ b/dbbackup/management/commands/dbbackup.py @@ -80,9 +80,8 @@ def handle(self, **options): self.storage = get_storage() self.database = options.get("database") or "" - database_keys = self.database.split(",") or settings.DATABASES - for database_key in database_keys: + for database_key in self._get_database_keys(): self.connector = get_connector(database_key) if self.connector and self.exclude_tables: self.connector.exclude.extend( @@ -96,6 +95,9 @@ def handle(self, **options): except StorageError as err: raise CommandError(err) from err + def _get_database_keys(self): + return self.database.split(",") if self.database else settings.DATABASES + def _save_new_backup(self, database): """ Save a new backup file. diff --git a/dbbackup/tests/commands/test_dbbackup.py b/dbbackup/tests/commands/test_dbbackup.py index 11fbaedf..4325a37d 100644 --- a/dbbackup/tests/commands/test_dbbackup.py +++ b/dbbackup/tests/commands/test_dbbackup.py @@ -50,6 +50,16 @@ def test_path(self): # tearDown os.remove(self.command.path) + @patch("dbbackup.settings.DATABASES", ["db-from-settings"]) + def test_get_database_keys(self): + with self.subTest("use --database from CLI"): + self.command.database = "db-from-cli" + self.assertEqual(self.command._get_database_keys(), ["db-from-cli"]) + + with self.subTest("fallback to DBBACKUP_DATABASES"): + self.command.database = "" + self.assertEqual(self.command._get_database_keys(), ["db-from-settings"]) + @patch("dbbackup.settings.GPG_RECIPIENT", "test@test") @patch("sys.stdout", DEV_NULL) diff --git a/docs/changelog.rst b/docs/changelog.rst index 176b7573..3ae3ce08 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -5,6 +5,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 4.1.0 (2024-01-14) ------------------