You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

328 lines
14 KiB

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