Skip to content

Commit

Permalink
Fixed #21015 -- Fixed MigrationLoader when importlib.import_module re…
Browse files Browse the repository at this point in the history
…turns a file module or an empty directory.
  • Loading branch information
loic authored and timgraham committed Sep 6, 2013
1 parent 82bbb9f commit e1266e5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions django/db/migrations/loader.py
Expand Up @@ -64,6 +64,13 @@ def load_disk(self):
self.unmigrated_apps.add(app_label)
continue
raise
else:
# PY3 will happily import empty dirs as namespaces.
if not hasattr(module, '__file__'):
continue
# Module is not a package (e.g. migrations.py).
if not hasattr(module, '__path__'):
continue
self.migrated_apps.add(app_label)
directory = os.path.dirname(module.__file__)
# Scan for .py[c|o] files
Expand Down
Empty file.
Empty file.
Empty file.
16 changes: 16 additions & 0 deletions tests/migrations/test_loader.py
@@ -1,8 +1,11 @@
from unittest import skipIf

from django.test import TestCase
from django.test.utils import override_settings
from django.db import connection
from django.db.migrations.loader import MigrationLoader, AmbiguityError
from django.db.migrations.recorder import MigrationRecorder
from django.utils import six


class RecorderTests(TestCase):
Expand Down Expand Up @@ -84,3 +87,16 @@ def test_load_import_error(self):
with override_settings(MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.import_error"}):
with self.assertRaises(ImportError):
migration_loader.load_disk()

def test_load_module_file(self):
migration_loader = MigrationLoader(connection)

with override_settings(MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.file"}):
migration_loader.load_disk()

@skipIf(six.PY2, "PY2 doesn't load empty dirs.")
def test_load_empty_dir(self):
migration_loader = MigrationLoader(connection)

with override_settings(MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.namespace"}):
migration_loader.load_disk()

0 comments on commit e1266e5

Please sign in to comment.