Hello,
First of all, thank you for bringing to life this awesome project. I've been using it for months and it's been working great.
My concern is what will happen if the tree (which I use for categories) get ever corrupted. How to detect it, how to fix it, etc. Basically, a backup plan.
I know that the answer to fix a corrupted tree passes by executing rebuild (as in CategoryModel.objects.rebuild()) but I'd like to be able to automatically detect a corruption and not execute periodical rebuilds if they are not needed or, in the worst case, when I'm contacted by someone telling me that their categories make no sense!
Of couse, I'm also trying to avoid having to check lft and rght myself! One approach I thought about is:
1) Read the tree
2) Rebuild the tree
3) Read the tree again
4) Compare them. If they are different, there's a corruption
I've tried something like:
def are_categories_corrupted(self):
with transaction.atomic():
with Category.objects.disable_mptt_updates():
categories_pre_rebuild = Category.objects.filter(parent__isnull=True)
Category.objects.rebuild()
categories_post_rebuild = Category.objects.filter(parent__isnull=True)
corrupted = self.are_multiple_trees_corrupted(categories_pre_rebuild, categories_post_rebuild)
return corrupted
The problem with this code is that the second time I read the tree, it's read from the database which has not been updated! I would have to read it from memory (or wherever the mptt library stores it until writing it to the db). Any idea about how to do this?
Any idea about how to sove my problem? Basically, detecting mptt corruptions.
I know that not wanting to execute rebuild() periodically on the db might be seen as dumb, but I can find a lot of excuses for not doing so. You can try to convince me otherwise :)
Thanks for your time,
Albert.