Repository: translate/pootle

Issue:
2.6.1 0010_populate_translation_project_field.py migration is failing
Migration issue reported on [`pootle-devel`](http://markmail.org/message/uieojvgity3puid5)

Summary is that a cleanup migration seems to be failing.

Migration code: https://github.com/translate/pootle/blob/stable/2.6.1/pootle/apps/pootle_store/migrations/0010_populate_translation_project_field.py#L9-L14

As an aside. Can we actually document in the migration what the migration is hoping to achieve.

``` pytb

> pootle_store:0010_populate_translation_project_field
 - Migration 'pootle_store:0010_populate_translation_project_field' is
marked for no-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 discover 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:0010_populate_translation_project_field
```

``` pytb
Traceback (most recent call last):
  File "/var/www/pootle/env/bin/pootle", line 11, in <module>
    sys.exit(main())
  File "/var/www/pootle/env/lib/python2.7/site-packages/pootle/runner.py", line 171, in main
    django_settings_module='pootle.settings')
  File "/var/www/pootle/env/lib/python2.7/site-packages/pootle/runner.py", line 150, in run_app
    management.execute_from_command_line([runner_name] + remainder)
  File "/var/www/pootle/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/var/www/pootle/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/www/pootle/env/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/var/www/pootle/env/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/var/www/pootle/env/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/var/www/pootle/env/lib/python2.7/site-packages/pootle/apps/pootle_app/management/commands/setup.py", line 77, in handle_noargs
    call_command('migrate', interactive=False)
  File "/var/www/pootle/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/var/www/pootle/env/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/var/www/pootle/env/lib/python2.7/site-packages/south/management/commands/migrate.py", line 111, in handle
    ignore_ghosts = ignore_ghosts,
  File "/var/www/pootle/env/lib/python2.7/site-packages/south/migration/__init__.py", line 220, in migrate_app
    success = migrator.migrate_many(target, workplan, database)
  File "/var/www/pootle/env/lib/python2.7/site-packages/south/migration/migrators.py", line 256, in migrate_many
    result = migrator.__class__.migrate_many(migrator, target, migrations, database)
  File "/var/www/pootle/env/lib/python2.7/site-packages/south/migration/migrators.py", line 331, in migrate_many
    result = self.migrate(migration, database)
  File "/var/www/pootle/env/lib/python2.7/site-packages/south/migration/migrators.py", line 133, in migrate
    result = self.run(migration, database)
  File "/var/www/pootle/env/lib/python2.7/site-packages/south/migration/migrators.py", line 114, in run
    return self.run_migration(migration, database)
  File "/var/www/pootle/env/lib/python2.7/site-packages/south/migration/migrators.py", line 84, in run_migration
    migration_function()
  File "/var/www/pootle/env/lib/python2.7/site-packages/south/migration/migrators.py", line 60, in <lambda>
    return (lambda: direction(orm))
  File "/var/www/pootle/env/lib/python2.7/site-packages/pootle/apps/pootle_store/migrations/0010_populate_translation_project_field.py", line 13, in forwards
    bs.translation_project = bs.unit.store.translation_project
  File "/var/www/pootle/env/lib/python2.7/site-packages/django/db/models/fields/related.py", line 320, in __get__
    rel_obj = qs.get()
  File "/var/www/pootle/env/lib/python2.7/site-packages/django/db/models/query.py", line 310, in get
    self.model._meta.object_name)
pootle_store.models.DoesNotExist: Unit matching query does not exist.
```



Discussion:
looks like orphaned suggestions to me


Resolving patch:
diff --git a/pootle/apps/pootle_store/migrations/0010_populate_translation_project_field.py b/pootle/apps/pootle_store/migrations/0010_populate_translation_project_field.py
--- a/pootle/apps/pootle_store/migrations/0010_populate_translation_project_field.py
+++ b/pootle/apps/pootle_store/migrations/0010_populate_translation_project_field.py
@@ -10,8 +10,15 @@ def forwards(self, orm):
         broken_suggestions = orm['pootle_store.Suggestion'].objects.filter(translation_project__isnull=True)
 
         for bs in broken_suggestions:
-            bs.translation_project = bs.unit.store.translation_project
-            bs.save()
+            try:
+                unit = getattr(bs, "unit", None)
+            except orm['pootle_store.Unit'].DoesNotExist:
+                unit = None
+            if unit is None:
+                bs.delete()
+            else:
+                bs.translation_project = unit.store.translation_project
+                bs.save()
 
     def backwards(self, orm):
         pass


Test patch: