Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deleting select historical records #313

Closed
dwasyl opened this issue Sep 8, 2017 · 4 comments · Fixed by #483
Closed

Deleting select historical records #313

dwasyl opened this issue Sep 8, 2017 · 4 comments · Fixed by #483

Comments

@dwasyl
Copy link

dwasyl commented Sep 8, 2017

Not sure if this is related to the situation raised in #254, but the app had a runaway save problem and created a ton of nearly duplicate historical entries which need to be cleared.

Running a routine that found each of the duplicate historical objects from Model.history.first().instance. And then using .delete() seems to delete the original record rather than the history record.

Is it possible to do this through the queryset manager?

@macro1
Copy link
Collaborator

macro1 commented Sep 8, 2017

Have you tried Model.history.first().delete()? instance is a re-constituted original model instance. Any kind of database work done on instance is not done on the history table, it's done on the original.

@fopina
Copy link
Contributor

fopina commented Oct 10, 2017

I'm using this app and, initially, I wanted something more "field-oriented" but now I like to have the full info in each historical record.
The only annoying thing is that each.save() will create a new record no matter if there was any change...

I'm not sure if there would be a big impact of adding that check (if all old fields are already loaded in memory I guess it's ok) but maybe something useful would be a management command to eliminate duplicate records (or django admin action since this is well admin-oriented).

I wouldn't mind having that in crontab to cleanup database (instead of doing the check directly) and it would solve also issues like this one.

Would that be of interest?

EDIT: I was just checking the post_save that creates the historical record and as it saves the in-memory instance (without having already old values in memory) I guess it'd mean the previous HistoricRecord had to be retrieved from database in order to compare which would make it quite slower

@rossmechanic
Copy link
Collaborator

@fopina exactly – we've had this feature request a few times, but checking for dirty fields on save would definitely be a lot slower. I'm open to having a management command that deletes duplicate records that users can run periodically. Feel free to put up a PR for that.

@dwasyl
Copy link
Author

dwasyl commented Jul 2, 2018

@fopina @rossmechanic To clean up a dirty model history I ended up building a function like that, but it was quite messy and I'm sure there are better ways to do it. What I've done in the meantime is had programs check for fields worth saving being changed and otherwise use save_without_historical_record (instead of save), but a cleanup crontab would be great. Could be quite memory/process intense thugh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants