Repository: translate/pootle

Issue:
mlo database migration failure in suggestions: User matching query does not exist
``` pytb
 > pootle_store:0009_move_data_from_pootle_app_suggestion
 - Migration 'pootle_store:0009_move_data_from_pootle_app_suggestion' is marked for n
o-dry-run.
 ! Error found during real run of migration! Aborting.

 ! Since you have a database that does not support running
 ! schema-altering statements in transactions, we have had 
 ! to leave it in an interim state between migrations.

! You *might* be able to recover with:   (migration cannot be dry-run; cannot discove
r commands)
 ! The South developers regret this has happened, and would
 ! like to gently persuade you to consider a slightly
 ! easier-to-deal-with DBMS (one that supports DDL transactions)
 ! NOTE: The error which caused the migration to fail is further up.
Error in migration: pootle_store:0009_move_data_from_pootle_app_suggestion
Traceback (most recent call last):
  File "./manage.py", line 32, in <module>
    execute_from_command_line()
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/home/dwayne/pootle/pootle/apps/pootle_app/management/commands/setup.py", line 85, in handle_noargs
    call_command('migrate', interactive=False)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/south/management/commands/migrate.py", line 111, in handle
    ignore_ghosts = ignore_ghosts,
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/south/migration/__init__.py", line 220, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/south/migration/migrators.py", line 256, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/south/migration/migrators.py", line 331, in migrate_many
    result = self.migrate(migration, database)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate
    result = self.run(migration, database)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/south/migration/migrators.py", line 114, in run
    return self.run_migration(migration, database)
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/south/migration/migrators.py", line 84, in run_migration
    migration_function()
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/south/migration/migrators.py", line 60, in <lambda>
    return (lambda: direction(orm))
  File "/home/dwayne/pootle/pootle/apps/pootle_store/migrations/0009_move_data_from_pootle_app_suggestion.py", line 45, in forwards
    ss = list(ss)
  File "/home/dwayne/pootle/pootle/apps/pootle_store/migrations/0009_move_data_from_pootle_app_suggestion.py", line 44, in <lambda>
    for user_id, ss in groupby(store_suggestions, lambda x: x.user.id):
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 320, in __get__
    rel_obj = qs.get()
  File "/home/dwayne/.virtualenvs/pootle/local/lib/python2.7/site-packages/django/db/models/query.py", line 310, in get
    self.model._meta.object_name)
accounts.models.DoesNotExist: User matching query does not exist.
```



Discussion:

Resolving patch:
diff --git a/pootle/apps/pootle_store/migrations/0009_move_data_from_pootle_app_suggestion.py b/pootle/apps/pootle_store/migrations/0009_move_data_from_pootle_app_suggestion.py
--- a/pootle/apps/pootle_store/migrations/0009_move_data_from_pootle_app_suggestion.py
+++ b/pootle/apps/pootle_store/migrations/0009_move_data_from_pootle_app_suggestion.py
@@ -5,6 +5,7 @@
 from itertools import groupby
 
 from south.db import db
+from django.core.exceptions import ObjectDoesNotExist
 from django.db import connection
 from django.db.utils import IntegrityError
 
@@ -40,6 +41,18 @@ def forwards(self, orm):
         # units with suggestions
         units = orm['pootle_store.Unit'].objects.filter(suggestion__isnull=False).distinct()
         for unit in units:
+            for ss in unit.suggestion_set.filter(user__isnull=False):
+                # Remove the user for suggestions referring to non-existing
+                # users. This can only happen if constraints are not enforced,
+                # for example when using MyISAM.
+                # Note that this suggestions will be assigned to 'nobody' on
+                # migration 0022.
+                try:
+                    orm['accounts.User'].objects.get(id=ss.user_id)
+                except ObjectDoesNotExist:
+                    ss.user = None
+                    ss.save()
+
             store_suggestions = unit.suggestion_set.filter(user__isnull=False).order_by('user')
             for user_id, ss in groupby(store_suggestions, lambda x: x.user.id):
                 ss = list(ss)


Test patch: