diff --git a/django/core/management/commands/loaddata.py b/django/core/management/commands/loaddata.py index 29f7630ff7..eda3d068af 100644 --- a/django/core/management/commands/loaddata.py +++ b/django/core/management/commands/loaddata.py @@ -26,6 +26,12 @@ try: except ImportError: has_bz2 = False +try: + import lzma + has_lzma = True +except ImportError: + has_lzma = False + READ_STDIN = '-' @@ -97,6 +103,9 @@ class Command(BaseCommand): } if has_bz2: self.compression_formats['bz2'] = (bz2.BZ2File, 'r') + if has_lzma: + self.compression_formats['lzma'] = (lzma.LZMAFile, 'r') + self.compression_formats['xz'] = (lzma.LZMAFile, 'r') # Django's test suite repeatedly tries to load initial_data fixtures # from apps that don't have any fixtures. Because disabling constraint diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 329cb72215..4cd477b7a8 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -599,13 +599,14 @@ The :djadmin:`dumpdata` command can be used to generate input for ``loaddata``. Compressed fixtures ~~~~~~~~~~~~~~~~~~~ -Fixtures may be compressed in ``zip``, ``gz``, or ``bz2`` format. For example:: +Fixtures may be compressed in ``zip``, ``gz``, ``bz2``, ``lzma``, or ``xz`` +format. For example:: django-admin loaddata mydata.json -would look for any of ``mydata.json``, ``mydata.json.zip``, -``mydata.json.gz``, or ``mydata.json.bz2``. The first file contained within a -zip-compressed archive is used. +would look for any of ``mydata.json``, ``mydata.json.zip``, ``mydata.json.gz``, +``mydata.json.bz2``, ``mydata.json.lzma``, or ``mydata.json.xz``. The first +file contained within a compressed archive is used. Note that if two fixtures with the same name but different fixture type are discovered (for example, if ``mydata.json`` and @@ -619,6 +620,10 @@ installation will be aborted, and any data installed in the call to constraints, so if you use MyISAM, you won't get validation of fixture data, or a rollback if multiple transaction files are found. +.. versionchanged:: 3.2 + + Support for XZ archives (``.xz``) and LZMA archives (``.lzma``) was added. + Database-specific fixtures ~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/releases/3.2.txt b/docs/releases/3.2.txt index 18b1df2836..4ba20c4c09 100644 --- a/docs/releases/3.2.txt +++ b/docs/releases/3.2.txt @@ -154,7 +154,8 @@ Logging Management Commands ~~~~~~~~~~~~~~~~~~~ -* ... +* :djadmin:`loaddata` now supports fixtures stored in XZ archives (``.xz``) and + LZMA archives (``.lzma``). Migrations ~~~~~~~~~~ diff --git a/tests/fixtures/fixtures/fixture5.json.lzma b/tests/fixtures/fixtures/fixture5.json.lzma new file mode 100644 index 0000000000..a41fdaa82f Binary files /dev/null and b/tests/fixtures/fixtures/fixture5.json.lzma differ diff --git a/tests/fixtures/fixtures/fixture5.json.xz b/tests/fixtures/fixtures/fixture5.json.xz new file mode 100644 index 0000000000..af2e82d5c1 Binary files /dev/null and b/tests/fixtures/fixtures/fixture5.json.xz differ diff --git a/tests/fixtures/tests.py b/tests/fixtures/tests.py index d46bf65c97..ac96c48734 100644 --- a/tests/fixtures/tests.py +++ b/tests/fixtures/tests.py @@ -27,6 +27,12 @@ try: except ImportError: HAS_BZ2 = False +try: + import lzma # NOQA + HAS_LZMA = True +except ImportError: + HAS_LZMA = False + class TestCaseFixtureLoadingTests(TestCase): fixtures = ['fixture1.json', 'fixture2.json'] @@ -558,6 +564,20 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase): '', ]) + @unittest.skipUnless(HAS_LZMA, 'No lzma library detected.') + def test_compressed_loading_lzma(self): + management.call_command('loaddata', 'fixture5.json.lzma', verbosity=0) + self.assertQuerysetEqual(Article.objects.all(), [ + '', + ]) + + @unittest.skipUnless(HAS_LZMA, 'No lzma library detected.') + def test_compressed_loading_xz(self): + management.call_command('loaddata', 'fixture5.json.xz', verbosity=0) + self.assertQuerysetEqual(Article.objects.all(), [ + '', + ]) + def test_ambiguous_compressed_fixture(self): # The name "fixture5" is ambiguous, so loading raises an error. msg = "Multiple fixtures named 'fixture5'"