Fixed #31552 -- Added support for LZMA and XZ fixtures to loaddata.

This commit is contained in:
Paolo Melchiorre 2020-05-15 09:40:42 +02:00 committed by Mariusz Felisiak
parent 2e48cf6bd9
commit 0e3b0da2e3
6 changed files with 40 additions and 5 deletions

View File

@ -26,6 +26,12 @@ try:
except ImportError: except ImportError:
has_bz2 = False has_bz2 = False
try:
import lzma
has_lzma = True
except ImportError:
has_lzma = False
READ_STDIN = '-' READ_STDIN = '-'
@ -97,6 +103,9 @@ class Command(BaseCommand):
} }
if has_bz2: if has_bz2:
self.compression_formats['bz2'] = (bz2.BZ2File, 'r') 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 # Django's test suite repeatedly tries to load initial_data fixtures
# from apps that don't have any fixtures. Because disabling constraint # from apps that don't have any fixtures. Because disabling constraint

View File

@ -599,13 +599,14 @@ The :djadmin:`dumpdata` command can be used to generate input for ``loaddata``.
Compressed fixtures 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 django-admin loaddata mydata.json
would look for any of ``mydata.json``, ``mydata.json.zip``, would look for any of ``mydata.json``, ``mydata.json.zip``, ``mydata.json.gz``,
``mydata.json.gz``, or ``mydata.json.bz2``. The first file contained within a ``mydata.json.bz2``, ``mydata.json.lzma``, or ``mydata.json.xz``. The first
zip-compressed archive is used. file contained within a compressed archive is used.
Note that if two fixtures with the same name but different Note that if two fixtures with the same name but different
fixture type are discovered (for example, if ``mydata.json`` and 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 constraints, so if you use MyISAM, you won't get validation of fixture
data, or a rollback if multiple transaction files are found. 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 Database-specific fixtures
~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -154,7 +154,8 @@ Logging
Management Commands Management Commands
~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~
* ... * :djadmin:`loaddata` now supports fixtures stored in XZ archives (``.xz``) and
LZMA archives (``.lzma``).
Migrations Migrations
~~~~~~~~~~ ~~~~~~~~~~

Binary file not shown.

BIN
tests/fixtures/fixtures/fixture5.json.xz vendored Normal file

Binary file not shown.

View File

@ -27,6 +27,12 @@ try:
except ImportError: except ImportError:
HAS_BZ2 = False HAS_BZ2 = False
try:
import lzma # NOQA
HAS_LZMA = True
except ImportError:
HAS_LZMA = False
class TestCaseFixtureLoadingTests(TestCase): class TestCaseFixtureLoadingTests(TestCase):
fixtures = ['fixture1.json', 'fixture2.json'] fixtures = ['fixture1.json', 'fixture2.json']
@ -558,6 +564,20 @@ class FixtureLoadingTests(DumpDataAssertMixin, TestCase):
'<Article: WoW subscribers now outnumber readers>', '<Article: WoW subscribers now outnumber readers>',
]) ])
@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(), [
'<Article: WoW subscribers now outnumber readers>',
])
@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(), [
'<Article: WoW subscribers now outnumber readers>',
])
def test_ambiguous_compressed_fixture(self): def test_ambiguous_compressed_fixture(self):
# The name "fixture5" is ambiguous, so loading raises an error. # The name "fixture5" is ambiguous, so loading raises an error.
msg = "Multiple fixtures named 'fixture5'" msg = "Multiple fixtures named 'fixture5'"