Code

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#7159 closed (wontfix)

Patch to add a --delete option to loaddata management command

Reported by: Graham King <graham@…> Owned by: nobody
Component: Tools Version: master
Severity: Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The attached patch adds a '--delete' option to the loaddata command. This deletes any objects from the database that are not in the fixture. The fixture must contain at least one object of the class for the delete to run.

The result is that loaddata can now insert/update/delete to make a database match the fixture, becoming a sort of 'syncdata' command. A 'dumpdata' followed by 'loaddata --delete' effectively publishes one database onto another. We use this to publish our staging system to live.

The patch contains changes to management/commands/loadata.py, to docs/django-admin.txt and to tests/modeltests/fixtures/models.py. File fixture4.json goes in tests/modeltests/fixtures/fixtures/.

Attachments (3)

loaddata-delete.diff (5.4 KB) - added by Graham King <graham@…> 6 years ago.
The patch
fixture4.json (606 bytes) - added by Graham King <graham@…> 6 years ago.
Fixture for the test case
loaddata-delete-2.diff (5.3 KB) - added by Graham King <graham@…> 6 years ago.
Patch updated for Django r7899

Download all attachments as: .zip

Change History (7)

Changed 6 years ago by Graham King <graham@…>

The patch

Changed 6 years ago by Graham King <graham@…>

Fixture for the test case

comment:1 Changed 6 years ago by programmerq

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

Changed 6 years ago by Graham King <graham@…>

Patch updated for Django r7899

comment:2 Changed 6 years ago by Graham King <graham@…>

loaddata-delete-2.diff works as is with r8075 (1.0 Alpha).

comment:3 Changed 6 years ago by russellm

  • Resolution set to wontfix
  • Status changed from new to closed

The way the feature is described in help text and options, it is equivalent to 'manage.py flush', followed by 'manage.py loaddata', which makes the proposed option redundant.

However, looking at the implementation, it is doing something quite different, and much more concerning. All you would need is one Foo object to accidentally get dropped into a fixture, and you lose all of your Foo data in the database. Alternately, you could have a model dependency between a model you _do_ want to flush, and one that you _dont_ want to flush; the cascading deletes from this option could cause you to lose data outside the classes that you think are going to be removed. I'm not in favour of adding commands or options that could lead to unexpected data loss on the scale that this command has the potential to do.

comment:4 Changed 6 years ago by Graham King <graham@…>

Thanks for taking the time to review this patch - I know 1.0 is keeping everyone very busy.

It is a bit like 'manage.py reset', followed by 'manage.py loaddata'. 'flush' doesn't work if you are loading accounts from the auth app.
The contenttypes initial data gets re-created, so after a flush the loaddata might fail because auth_permission.content_type_id foreign key points at a non-existent row.

I understand your concerns, if someone wanted to load only part of their database, and somehow specified --delete. This patch only applies for loading the entire database.

I have moved this to Django Snippets as a new 'syncdata' command: http://www.djangosnippets.org/snippets/926/
The downside is lots of the 'loaddata' code is duplicated.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.