syncdata.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. # -*- coding: utf-8 -*-
  2. """
  3. SyncData
  4. ========
  5. Django command similar to 'loaddata' but also deletes.
  6. After 'syncdata' has run, the database will have the same data as the fixture - anything
  7. missing will of been added, anything different will of been updated,
  8. and anything extra will of been deleted.
  9. """
  10. import os
  11. import six
  12. from django.apps import apps
  13. from django.conf import settings
  14. from django.core import serializers
  15. from django.core.management.base import BaseCommand, CommandError
  16. from django.core.management.color import no_style
  17. from django.db import DEFAULT_DB_ALIAS, connections, transaction
  18. from django.template.defaultfilters import pluralize
  19. from django_extensions.management.utils import signalcommand
  20. def humanize(dirname):
  21. return "'%s'" % dirname if dirname else 'absolute path'
  22. class SyncDataError(Exception):
  23. pass
  24. class Command(BaseCommand):
  25. """ syncdata command """
  26. help = 'Makes the current database have the same data as the fixture(s), no more, no less.'
  27. args = "fixture [fixture ...]"
  28. def add_arguments(self, parser):
  29. super().add_arguments(parser)
  30. parser.add_argument(
  31. '--skip-remove', action='store_false', dest='remove', default=True,
  32. help='Avoid remove any object from db',
  33. )
  34. parser.add_argument(
  35. '--remove-before', action='store_true', dest='remove_before', default=False,
  36. help='Remove existing objects before inserting and updating new ones',
  37. )
  38. parser.add_argument(
  39. '--database', default=DEFAULT_DB_ALIAS,
  40. help='Nominates a specific database to load fixtures into. Defaults to the "default" database.',
  41. )
  42. parser.add_argument(
  43. 'fixture_labels', nargs='?', type=str,
  44. help='Specify the fixture label (comma separated)',
  45. )
  46. def remove_objects_not_in(self, objects_to_keep, verbosity):
  47. """
  48. Delete all the objects in the database that are not in objects_to_keep.
  49. - objects_to_keep: A map where the keys are classes, and the values are a
  50. set of the objects of that class we should keep.
  51. """
  52. for class_ in objects_to_keep.keys():
  53. current = class_.objects.all()
  54. current_ids = set(x.pk for x in current)
  55. keep_ids = set(x.pk for x in objects_to_keep[class_])
  56. remove_these_ones = current_ids.difference(keep_ids)
  57. if remove_these_ones:
  58. for obj in current:
  59. if obj.pk in remove_these_ones:
  60. obj.delete()
  61. if verbosity >= 2:
  62. print("Deleted object: %s" % six.u(obj))
  63. if verbosity > 0 and remove_these_ones:
  64. num_deleted = len(remove_these_ones)
  65. if num_deleted > 1:
  66. type_deleted = six.u(class_._meta.verbose_name_plural)
  67. else:
  68. type_deleted = six.u(class_._meta.verbose_name)
  69. print("Deleted %s %s" % (str(num_deleted), type_deleted))
  70. @signalcommand
  71. def handle(self, *args, **options):
  72. self.style = no_style()
  73. self.using = options['database']
  74. fixture_labels = options['fixture_labels'].split(',') if options['fixture_labels'] else ()
  75. try:
  76. with transaction.atomic():
  77. self.syncdata(fixture_labels, options)
  78. except SyncDataError as exc:
  79. raise CommandError(exc)
  80. finally:
  81. # Close the DB connection -- unless we're still in a transaction. This
  82. # is required as a workaround for an edge case in MySQL: if the same
  83. # connection is used to create tables, load data, and query, the query
  84. # can return incorrect results. See Django #7572, MySQL #37735.
  85. if transaction.get_autocommit(self.using):
  86. connections[self.using].close()
  87. def syncdata(self, fixture_labels, options):
  88. verbosity = options['verbosity']
  89. show_traceback = options['traceback']
  90. # Keep a count of the installed objects and fixtures
  91. fixture_count = 0
  92. object_count = 0
  93. objects_per_fixture = []
  94. models = set()
  95. # Get a cursor (even though we don't need one yet). This has
  96. # the side effect of initializing the test database (if
  97. # it isn't already initialized).
  98. cursor = connections[self.using].cursor()
  99. app_modules = [app.module for app in apps.get_app_configs()]
  100. app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in app_modules]
  101. for fixture_label in fixture_labels:
  102. parts = fixture_label.split('.')
  103. if len(parts) == 1:
  104. fixture_name = fixture_label
  105. formats = serializers.get_public_serializer_formats()
  106. else:
  107. fixture_name, format_ = '.'.join(parts[:-1]), parts[-1]
  108. if format_ in serializers.get_public_serializer_formats():
  109. formats = [format_]
  110. else:
  111. formats = []
  112. if formats:
  113. if verbosity > 1:
  114. print("Loading '%s' fixtures..." % fixture_name)
  115. else:
  116. raise SyncDataError("Problem installing fixture '%s': %s is not a known serialization format." % (fixture_name, format_))
  117. if os.path.isabs(fixture_name):
  118. fixture_dirs = [fixture_name]
  119. else:
  120. fixture_dirs = app_fixtures + list(settings.FIXTURE_DIRS) + ['']
  121. for fixture_dir in fixture_dirs:
  122. if verbosity > 1:
  123. print("Checking %s for fixtures..." % humanize(fixture_dir))
  124. label_found = False
  125. for format_ in formats:
  126. if verbosity > 1:
  127. print("Trying %s for %s fixture '%s'..." % (humanize(fixture_dir), format_, fixture_name))
  128. try:
  129. full_path = os.path.join(fixture_dir, '.'.join([fixture_name, format_]))
  130. fixture = open(full_path, 'r')
  131. if label_found:
  132. fixture.close()
  133. raise SyncDataError("Multiple fixtures named '%s' in %s. Aborting." % (fixture_name, humanize(fixture_dir)))
  134. else:
  135. fixture_count += 1
  136. objects_per_fixture.append(0)
  137. if verbosity > 0:
  138. print("Installing %s fixture '%s' from %s." % (format_, fixture_name, humanize(fixture_dir)))
  139. try:
  140. objects_to_keep = {}
  141. objects = list(serializers.deserialize(format_, fixture))
  142. for obj in objects:
  143. class_ = obj.object.__class__
  144. if class_ not in objects_to_keep:
  145. objects_to_keep[class_] = set()
  146. objects_to_keep[class_].add(obj.object)
  147. if options['remove'] and options['remove_before']:
  148. self.remove_objects_not_in(objects_to_keep, verbosity)
  149. for obj in objects:
  150. object_count += 1
  151. objects_per_fixture[-1] += 1
  152. models.add(obj.object.__class__)
  153. obj.save()
  154. if options['remove'] and not options['remove_before']:
  155. self.remove_objects_not_in(objects_to_keep, verbosity)
  156. label_found = True
  157. except (SystemExit, KeyboardInterrupt):
  158. raise
  159. except Exception:
  160. import traceback
  161. fixture.close()
  162. if show_traceback:
  163. traceback.print_exc()
  164. raise SyncDataError("Problem installing fixture '%s': %s\n" % (full_path, traceback.format_exc()))
  165. fixture.close()
  166. except SyncDataError as e:
  167. raise e
  168. except Exception:
  169. if verbosity > 1:
  170. print("No %s fixture '%s' in %s." % (format_, fixture_name, humanize(fixture_dir)))
  171. # If any of the fixtures we loaded contain 0 objects, assume that an
  172. # error was encountered during fixture loading.
  173. if 0 in objects_per_fixture:
  174. raise SyncDataError("No fixture data found for '%s'. (File format may be invalid.)" % fixture_name)
  175. # If we found even one object in a fixture, we need to reset the
  176. # database sequences.
  177. if object_count > 0:
  178. sequence_sql = connections[self.using].ops.sequence_reset_sql(self.style, models)
  179. if sequence_sql:
  180. if verbosity > 1:
  181. print("Resetting sequences")
  182. for line in sequence_sql:
  183. cursor.execute(line)
  184. if object_count == 0:
  185. if verbosity > 1:
  186. print("No fixtures found.")
  187. else:
  188. if verbosity > 0:
  189. print("Installed %d object%s from %d fixture%s" % (
  190. object_count, pluralize(object_count),
  191. fixture_count, pluralize(fixture_count)
  192. ))