Fix topological sort

Fixes https://github.com/django/django/pull/3525
Can you check for `is not None` here, please. At least on SQLite an empty string is a suitable way to be passed to `reverse_sql` to give `RunSQL` noop backwards migration. (PostgreSQL fails with `django.db.utils.ProgrammingError: can't execute an empty query`)