From 53462cf346d9436b1e668d2e0076001cc4f133ed Mon Sep 17 00:00:00 2001
From: Andre Anjos <andre.dos.anjos@gmail.com>
Date: Mon, 4 Jul 2016 12:02:47 +0200
Subject: [PATCH] [backup] Add pk ordering back

---
 beat/web/utils/management/commands/backup.py    | 16 ++++++++++++++++
 beat/web/utils/management/commands/xdumpdata.py | 15 +++++++--------
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/beat/web/utils/management/commands/backup.py b/beat/web/utils/management/commands/backup.py
index 1245b207e..c93586954 100644
--- a/beat/web/utils/management/commands/backup.py
+++ b/beat/web/utils/management/commands/backup.py
@@ -158,6 +158,22 @@ class Command(BaseCommand):
                 arguments = copy.deepcopy(dump_arguments)
                 logger.info("Dumping data for `%s' -> `%s'", app, destfile)
 
+                if app in only:
+
+                    app, model = only[app].split('.')
+                    model = apps.get_model(app, model)
+                    order = ('creation_date',)
+
+                    # This will check and correct objects with weird creation
+                    # dates so that the dump order is consistent
+                    while True:
+                        queryset = model.objects.order_by(*order)
+                        err = _check(app, queryset)
+                        if not err: break
+
+                    arguments['primary_keys'] = \
+                        ','.join([str(k.id) for k in queryset])
+
                 arguments['output'] = destfile #new in Django-1.8.x
                 call_command('xdumpdata', only.get(app, app), **arguments)
 
diff --git a/beat/web/utils/management/commands/xdumpdata.py b/beat/web/utils/management/commands/xdumpdata.py
index fcddbea38..b6b4404f5 100644
--- a/beat/web/utils/management/commands/xdumpdata.py
+++ b/beat/web/utils/management/commands/xdumpdata.py
@@ -155,18 +155,17 @@ class Command(BaseCommand):
                     # ###################################################
                     queryset = objects.using(using)
 
-                    if primary_keys:
-                        # in case the user specifies PKs, respect order
-                        for pk in [int(k) for k in primary_keys]:
+                    if count_only:
+                        yield queryset.count()
+                    elif primary_keys:
+                        for pk in primary_keys:
                             yield queryset.get(pk=pk)
-                    elif count_only:
-                        # ###################################################
-                        # patch for BEAT ends here
-                        # ###################################################
-                        yield queryset.order_by().count()
                     else:
                         for obj in queryset.iterator():
                             yield obj
+                    # ###################################################
+                    # patch for BEAT ends here
+                    # ###################################################
 
         try:
             self.stdout.ending = None
-- 
GitLab