diff options
| author | Nasir Hussain <nasirhjafri@gmail.com> | 2019-01-23 03:49:30 +0500 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2019-01-22 17:49:30 -0500 |
| commit | 2804b8d2153505ec49b191db2168302dfb92c3af (patch) | |
| tree | c683570a1ec7f6939d968c7ac500f1c52809d855 | |
| parent | d02b2aa11e5b6c351a9a2c0673c23569889f90d6 (diff) | |
Fixed #30111 -- Fixed AppRegistryNotReady error with django.contrib.postgres in INSTALLED_APPS.
Regression in e192223ed996ed30fe83787efdfa7f2be6b1a2ee.
| -rw-r--r-- | django/db/migrations/recorder.py | 32 | ||||
| -rw-r--r-- | tests/postgres_tests/integration_settings.py | 5 | ||||
| -rw-r--r-- | tests/postgres_tests/test_integration.py | 17 |
3 files changed, 44 insertions, 10 deletions
diff --git a/django/db/migrations/recorder.py b/django/db/migrations/recorder.py index 3a972fe4c6..ad5435d906 100644 --- a/django/db/migrations/recorder.py +++ b/django/db/migrations/recorder.py @@ -1,6 +1,7 @@ from django.apps.registry import Apps from django.db import models from django.db.utils import DatabaseError +from django.utils.decorators import classproperty from django.utils.timezone import now from .exceptions import MigrationSchemaMissing @@ -18,19 +19,30 @@ class MigrationRecorder: If a migration is unapplied its row is removed from the table. Having a row in the table always means a migration is applied. """ + _migration_class = None - class Migration(models.Model): - app = models.CharField(max_length=255) - name = models.CharField(max_length=255) - applied = models.DateTimeField(default=now) + @classproperty + def Migration(cls): + """ + Lazy load to avoid AppRegistryNotReady if installed apps import + MigrationRecorder. + """ + if cls._migration_class is None: + class Migration(models.Model): + app = models.CharField(max_length=255) + name = models.CharField(max_length=255) + applied = models.DateTimeField(default=now) - class Meta: - apps = Apps() - app_label = "migrations" - db_table = "django_migrations" + class Meta: + apps = Apps() + app_label = 'migrations' + db_table = 'django_migrations' - def __str__(self): - return "Migration %s for %s" % (self.name, self.app) + def __str__(self): + return 'Migration %s for %s' % (self.name, self.app) + + cls._migration_class = Migration + return cls._migration_class def __init__(self, connection): self.connection = connection diff --git a/tests/postgres_tests/integration_settings.py b/tests/postgres_tests/integration_settings.py new file mode 100644 index 0000000000..c4ec0d1157 --- /dev/null +++ b/tests/postgres_tests/integration_settings.py @@ -0,0 +1,5 @@ +SECRET_KEY = 'abcdefg' + +INSTALLED_APPS = [ + 'django.contrib.postgres', +] diff --git a/tests/postgres_tests/test_integration.py b/tests/postgres_tests/test_integration.py new file mode 100644 index 0000000000..829b862027 --- /dev/null +++ b/tests/postgres_tests/test_integration.py @@ -0,0 +1,17 @@ +import os +import subprocess +import sys + +from tests.postgres_tests import PostgreSQLSimpleTestCase + + +class PostgresIntegrationTests(PostgreSQLSimpleTestCase): + def test_check(self): + os.chdir(os.path.dirname(__file__)) + result = subprocess.run( + [sys.executable, '-m', 'django', 'check', '--settings', 'integration_settings'], + stdout=subprocess.DEVNULL, + stderr=subprocess.PIPE, + ) + stderr = '\n'.join([e.decode() for e in result.stderr.splitlines()]) + self.assertEqual(result.returncode, 0, msg=stderr) |
