Skip to content

Commit

Permalink
Truncate recreate django (#57)
Browse files Browse the repository at this point in the history
* Change Django ``xload`` behaviour. #53

* Fix tests
  • Loading branch information
Stranger6667 committed Aug 11, 2018
1 parent ffb2c83 commit 094431a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 16 deletions.
8 changes: 5 additions & 3 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ Changelog
Added
~~~~~

- Possibility to truncate data in the database instead of the DB re-creation.
``truncate`` method in DB backends and ``--truncate`` option for Django integration. `#48`_
- Possibility to truncate data in the database instead of the DB re-creation. ``truncate`` method in DB backends. `#48`_
- Command Line Interface. `#6`_

Changed
~~~~~~~

- Do not recreate the DB if it the schema is absent in the dump. `#39`_
- Do not try to load schema if it is absent in the dump. `#39`_
- ``xdump.sqlite.SQLiteBackend`` now accepts only ``dbname`` and ``verbosity``. `#52`_
- Now Django's ``xload`` command doesn't recreate the DB, but provides a non required option ``--cleanup-method`` for
choosing how to clean-up the database before the dump loading. `#53`_

`0.5.0`_ - 2018-08-02
---------------------
Expand Down Expand Up @@ -106,6 +107,7 @@ Fixed
.. _0.1.2: https://github.com/Stranger6667/xdump/compare/0.1.1...0.1.2
.. _0.1.1: https://github.com/Stranger6667/xdump/compare/0.1.0...0.1.1

.. _#53: https://github.com/Stranger6667/xdump/issues/53
.. _#52: https://github.com/Stranger6667/xdump/issues/52
.. _#48: https://github.com/Stranger6667/xdump/issues/48
.. _#45: https://github.com/Stranger6667/xdump/issues/45
Expand Down
4 changes: 2 additions & 2 deletions tests/django/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_custom_backend_via_config(settings, db_helper, archive_filename):
def test_xload(archive_filename, db_helper):
call_command('xdump', archive_filename)
assert db_helper.get_tickets_count() == 5
call_command('xload', archive_filename)
call_command('xload', archive_filename, cleanup_method='recreate')
assert db_helper.get_tickets_count() == 0


Expand Down Expand Up @@ -112,7 +112,7 @@ def test_truncate_load(backend, archive_filename, db_helper):
except sqlite3.OperationalError:
pass

call_command('xload', archive_filename, truncate=True)
call_command('xload', archive_filename, cleanup_method='truncate')

assert db_helper.get_tables_count() == 3
assert backend.run('SELECT name FROM groups') == [{'name': 'Admin'}, {'name': 'User'}]
Expand Down
5 changes: 0 additions & 5 deletions xdump/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,6 @@ def load(self, filename):
self.initial_setup(archive)
self.load_data(archive)

def is_dump_without_schema(self, filename):
archive = zipfile.ZipFile(filename)
name_list = archive.namelist()
return all(name not in name_list for name in self.initial_setup_files)

def initial_setup(self, archive):
"""
Loads schema and initial database configuration.
Expand Down
13 changes: 7 additions & 6 deletions xdump/extra/django/xdump/management/commands/xload.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ class Command(XDumpCommand):
def add_arguments(self, parser):
super(Command, self).add_arguments(parser)
parser.add_argument(
'-t', '--truncate',
'-m', '--cleanup-method',
action='store',
dest='truncate',
help='Truncates tables instead of DB re-creation.',
nargs='?',
choices=['recreate', 'truncate'],
dest='cleanup_method',
help='Method of DB cleaning up',
required=False,
default=False,
)

def _handle(self, filename, backend, **options):
if options['truncate']:
if options['cleanup_method'] == 'truncate':
backend.truncate()
elif not backend.is_dump_without_schema(filename):
elif options['cleanup_method'] == 'recreate':
backend.recreate_database()
backend.load(filename)

0 comments on commit 094431a

Please sign in to comment.