deletion.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. from collections import Counter
  2. from itertools import chain
  3. from operator import attrgetter
  4. from django.db import IntegrityError, connections, transaction
  5. from django.db.models import signals, sql
  6. class ProtectedError(IntegrityError):
  7. def __init__(self, msg, protected_objects):
  8. self.protected_objects = protected_objects
  9. super().__init__(msg, protected_objects)
  10. def CASCADE(collector, field, sub_objs, using):
  11. collector.collect(sub_objs, source=field.remote_field.model,
  12. source_attr=field.name, nullable=field.null)
  13. if field.null and not connections[using].features.can_defer_constraint_checks:
  14. collector.add_field_update(field, None, sub_objs)
  15. def PROTECT(collector, field, sub_objs, using):
  16. raise ProtectedError(
  17. "Cannot delete some instances of model '%s' because they are "
  18. "referenced through a protected foreign key: '%s.%s'" % (
  19. field.remote_field.model.__name__, sub_objs[0].__class__.__name__, field.name
  20. ),
  21. sub_objs
  22. )
  23. def SET(value):
  24. if callable(value):
  25. def set_on_delete(collector, field, sub_objs, using):
  26. collector.add_field_update(field, value(), sub_objs)
  27. else:
  28. def set_on_delete(collector, field, sub_objs, using):
  29. collector.add_field_update(field, value, sub_objs)
  30. set_on_delete.deconstruct = lambda: ('django.db.models.SET', (value,), {})
  31. return set_on_delete
  32. def SET_NULL(collector, field, sub_objs, using):
  33. collector.add_field_update(field, None, sub_objs)
  34. def SET_DEFAULT(collector, field, sub_objs, using):
  35. collector.add_field_update(field, field.get_default(), sub_objs)
  36. def DO_NOTHING(collector, field, sub_objs, using):
  37. pass
  38. def get_candidate_relations_to_delete(opts):
  39. # The candidate relations are the ones that come from N-1 and 1-1 relations.
  40. # N-N (i.e., many-to-many) relations aren't candidates for deletion.
  41. return (
  42. f for f in opts.get_fields(include_hidden=True)
  43. if f.auto_created and not f.concrete and (f.one_to_one or f.one_to_many)
  44. )
  45. class Collector:
  46. def __init__(self, using):
  47. self.using = using
  48. # Initially, {model: {instances}}, later values become lists.
  49. self.data = {}
  50. self.field_updates = {} # {model: {(field, value): {instances}}}
  51. # fast_deletes is a list of queryset-likes that can be deleted without
  52. # fetching the objects into memory.
  53. self.fast_deletes = []
  54. # Tracks deletion-order dependency for databases without transactions
  55. # or ability to defer constraint checks. Only concrete model classes
  56. # should be included, as the dependencies exist only between actual
  57. # database tables; proxy models are represented here by their concrete
  58. # parent.
  59. self.dependencies = {} # {model: {models}}
  60. def add(self, objs, source=None, nullable=False, reverse_dependency=False):
  61. """
  62. Add 'objs' to the collection of objects to be deleted. If the call is
  63. the result of a cascade, 'source' should be the model that caused it,
  64. and 'nullable' should be set to True if the relation can be null.
  65. Return a list of all objects that were not already collected.
  66. """
  67. if not objs:
  68. return []
  69. new_objs = []
  70. model = objs[0].__class__
  71. instances = self.data.setdefault(model, set())
  72. for obj in objs:
  73. if obj not in instances:
  74. new_objs.append(obj)
  75. instances.update(new_objs)
  76. # Nullable relationships can be ignored -- they are nulled out before
  77. # deleting, and therefore do not affect the order in which objects have
  78. # to be deleted.
  79. if source is not None and not nullable:
  80. if reverse_dependency:
  81. source, model = model, source
  82. self.dependencies.setdefault(
  83. source._meta.concrete_model, set()).add(model._meta.concrete_model)
  84. return new_objs
  85. def add_field_update(self, field, value, objs):
  86. """
  87. Schedule a field update. 'objs' must be a homogeneous iterable
  88. collection of model instances (e.g. a QuerySet).
  89. """
  90. if not objs:
  91. return
  92. model = objs[0].__class__
  93. self.field_updates.setdefault(
  94. model, {}).setdefault(
  95. (field, value), set()).update(objs)
  96. def _has_signal_listeners(self, model):
  97. return (
  98. signals.pre_delete.has_listeners(model) or
  99. signals.post_delete.has_listeners(model)
  100. )
  101. def can_fast_delete(self, objs, from_field=None):
  102. """
  103. Determine if the objects in the given queryset-like or single object
  104. can be fast-deleted. This can be done if there are no cascades, no
  105. parents and no signal listeners for the object class.
  106. The 'from_field' tells where we are coming from - we need this to
  107. determine if the objects are in fact to be deleted. Allow also
  108. skipping parent -> child -> parent chain preventing fast delete of
  109. the child.
  110. """
  111. if from_field and from_field.remote_field.on_delete is not CASCADE:
  112. return False
  113. if hasattr(objs, '_meta'):
  114. model = type(objs)
  115. elif hasattr(objs, 'model') and hasattr(objs, '_raw_delete'):
  116. model = objs.model
  117. else:
  118. return False
  119. if self._has_signal_listeners(model):
  120. return False
  121. # The use of from_field comes from the need to avoid cascade back to
  122. # parent when parent delete is cascading to child.
  123. opts = model._meta
  124. return (
  125. all(link == from_field for link in opts.concrete_model._meta.parents.values()) and
  126. # Foreign keys pointing to this model.
  127. all(
  128. related.field.remote_field.on_delete is DO_NOTHING
  129. for related in get_candidate_relations_to_delete(opts)
  130. ) and (
  131. # Something like generic foreign key.
  132. not any(hasattr(field, 'bulk_related_objects') for field in opts.private_fields)
  133. )
  134. )
  135. def get_del_batches(self, objs, field):
  136. """
  137. Return the objs in suitably sized batches for the used connection.
  138. """
  139. conn_batch_size = max(
  140. connections[self.using].ops.bulk_batch_size([field.name], objs), 1)
  141. if len(objs) > conn_batch_size:
  142. return [objs[i:i + conn_batch_size]
  143. for i in range(0, len(objs), conn_batch_size)]
  144. else:
  145. return [objs]
  146. def collect(self, objs, source=None, nullable=False, collect_related=True,
  147. source_attr=None, reverse_dependency=False, keep_parents=False):
  148. """
  149. Add 'objs' to the collection of objects to be deleted as well as all
  150. parent instances. 'objs' must be a homogeneous iterable collection of
  151. model instances (e.g. a QuerySet). If 'collect_related' is True,
  152. related objects will be handled by their respective on_delete handler.
  153. If the call is the result of a cascade, 'source' should be the model
  154. that caused it and 'nullable' should be set to True, if the relation
  155. can be null.
  156. If 'reverse_dependency' is True, 'source' will be deleted before the
  157. current model, rather than after. (Needed for cascading to parent
  158. models, the one case in which the cascade follows the forwards
  159. direction of an FK rather than the reverse direction.)
  160. If 'keep_parents' is True, data of parent model's will be not deleted.
  161. """
  162. if self.can_fast_delete(objs):
  163. self.fast_deletes.append(objs)
  164. return
  165. new_objs = self.add(objs, source, nullable,
  166. reverse_dependency=reverse_dependency)
  167. if not new_objs:
  168. return
  169. model = new_objs[0].__class__
  170. if not keep_parents:
  171. # Recursively collect concrete model's parent models, but not their
  172. # related objects. These will be found by meta.get_fields()
  173. concrete_model = model._meta.concrete_model
  174. for ptr in concrete_model._meta.parents.values():
  175. if ptr:
  176. parent_objs = [getattr(obj, ptr.name) for obj in new_objs]
  177. self.collect(parent_objs, source=model,
  178. source_attr=ptr.remote_field.related_name,
  179. collect_related=False,
  180. reverse_dependency=True)
  181. if collect_related:
  182. if keep_parents:
  183. parents = set(model._meta.get_parent_list())
  184. for related in get_candidate_relations_to_delete(model._meta):
  185. # Preserve parent reverse relationships if keep_parents=True.
  186. if keep_parents and related.model in parents:
  187. continue
  188. field = related.field
  189. if field.remote_field.on_delete == DO_NOTHING:
  190. continue
  191. batches = self.get_del_batches(new_objs, field)
  192. for batch in batches:
  193. sub_objs = self.related_objects(related, batch)
  194. if self.can_fast_delete(sub_objs, from_field=field):
  195. self.fast_deletes.append(sub_objs)
  196. else:
  197. related_model = related.related_model
  198. # Non-referenced fields can be deferred if no signal
  199. # receivers are connected for the related model as
  200. # they'll never be exposed to the user. Skip field
  201. # deferring when some relationships are select_related
  202. # as interactions between both features are hard to
  203. # get right. This should only happen in the rare
  204. # cases where .related_objects is overridden anyway.
  205. if not (sub_objs.query.select_related or self._has_signal_listeners(related_model)):
  206. referenced_fields = set(chain.from_iterable(
  207. (rf.attname for rf in rel.field.foreign_related_fields)
  208. for rel in get_candidate_relations_to_delete(related_model._meta)
  209. ))
  210. sub_objs = sub_objs.only(*tuple(referenced_fields))
  211. if sub_objs:
  212. field.remote_field.on_delete(self, field, sub_objs, self.using)
  213. for field in model._meta.private_fields:
  214. if hasattr(field, 'bulk_related_objects'):
  215. # It's something like generic foreign key.
  216. sub_objs = field.bulk_related_objects(new_objs, self.using)
  217. self.collect(sub_objs, source=model, nullable=True)
  218. def related_objects(self, related, objs):
  219. """
  220. Get a QuerySet of objects related to `objs` via the relation `related`.
  221. """
  222. return related.related_model._base_manager.using(self.using).filter(
  223. **{"%s__in" % related.field.name: objs}
  224. )
  225. def instances_with_model(self):
  226. for model, instances in self.data.items():
  227. for obj in instances:
  228. yield model, obj
  229. def sort(self):
  230. sorted_models = []
  231. concrete_models = set()
  232. models = list(self.data)
  233. while len(sorted_models) < len(models):
  234. found = False
  235. for model in models:
  236. if model in sorted_models:
  237. continue
  238. dependencies = self.dependencies.get(model._meta.concrete_model)
  239. if not (dependencies and dependencies.difference(concrete_models)):
  240. sorted_models.append(model)
  241. concrete_models.add(model._meta.concrete_model)
  242. found = True
  243. if not found:
  244. return
  245. self.data = {model: self.data[model] for model in sorted_models}
  246. def delete(self):
  247. # sort instance collections
  248. for model, instances in self.data.items():
  249. self.data[model] = sorted(instances, key=attrgetter("pk"))
  250. # if possible, bring the models in an order suitable for databases that
  251. # don't support transactions or cannot defer constraint checks until the
  252. # end of a transaction.
  253. self.sort()
  254. # number of objects deleted for each model label
  255. deleted_counter = Counter()
  256. # Optimize for the case with a single obj and no dependencies
  257. if len(self.data) == 1 and len(instances) == 1:
  258. instance = list(instances)[0]
  259. if self.can_fast_delete(instance):
  260. with transaction.mark_for_rollback_on_error():
  261. count = sql.DeleteQuery(model).delete_batch([instance.pk], self.using)
  262. setattr(instance, model._meta.pk.attname, None)
  263. return count, {model._meta.label: count}
  264. with transaction.atomic(using=self.using, savepoint=False):
  265. # send pre_delete signals
  266. for model, obj in self.instances_with_model():
  267. if not model._meta.auto_created:
  268. signals.pre_delete.send(
  269. sender=model, instance=obj, using=self.using
  270. )
  271. # fast deletes
  272. for qs in self.fast_deletes:
  273. count = qs._raw_delete(using=self.using)
  274. deleted_counter[qs.model._meta.label] += count
  275. # update fields
  276. for model, instances_for_fieldvalues in self.field_updates.items():
  277. for (field, value), instances in instances_for_fieldvalues.items():
  278. query = sql.UpdateQuery(model)
  279. query.update_batch([obj.pk for obj in instances],
  280. {field.name: value}, self.using)
  281. # reverse instance collections
  282. for instances in self.data.values():
  283. instances.reverse()
  284. # delete instances
  285. for model, instances in self.data.items():
  286. query = sql.DeleteQuery(model)
  287. pk_list = [obj.pk for obj in instances]
  288. count = query.delete_batch(pk_list, self.using)
  289. deleted_counter[model._meta.label] += count
  290. if not model._meta.auto_created:
  291. for obj in instances:
  292. signals.post_delete.send(
  293. sender=model, instance=obj, using=self.using
  294. )
  295. # update collected instances
  296. for instances_for_fieldvalues in self.field_updates.values():
  297. for (field, value), instances in instances_for_fieldvalues.items():
  298. for obj in instances:
  299. setattr(obj, field.attname, value)
  300. for model, instances in self.data.items():
  301. for instance in instances:
  302. setattr(instance, model._meta.pk.attname, None)
  303. return sum(deleted_counter.values()), dict(deleted_counter)