Fixed #31504 -- Allowed calling makemigrations without an active database connection.
This commit is contained in:
parent
952afc166c
commit
9756c33429
|
@ -1,5 +1,6 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import warnings
|
||||||
from itertools import takewhile
|
from itertools import takewhile
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
|
@ -7,7 +8,7 @@ from django.conf import settings
|
||||||
from django.core.management.base import (
|
from django.core.management.base import (
|
||||||
BaseCommand, CommandError, no_translations,
|
BaseCommand, CommandError, no_translations,
|
||||||
)
|
)
|
||||||
from django.db import DEFAULT_DB_ALIAS, connections, router
|
from django.db import DEFAULT_DB_ALIAS, OperationalError, connections, router
|
||||||
from django.db.migrations import Migration
|
from django.db.migrations import Migration
|
||||||
from django.db.migrations.autodetector import MigrationAutodetector
|
from django.db.migrations.autodetector import MigrationAutodetector
|
||||||
from django.db.migrations.loader import MigrationLoader
|
from django.db.migrations.loader import MigrationLoader
|
||||||
|
@ -98,8 +99,15 @@ class Command(BaseCommand):
|
||||||
for app_label in consistency_check_labels
|
for app_label in consistency_check_labels
|
||||||
for model in apps.get_app_config(app_label).get_models()
|
for model in apps.get_app_config(app_label).get_models()
|
||||||
)):
|
)):
|
||||||
|
try:
|
||||||
loader.check_consistent_history(connection)
|
loader.check_consistent_history(connection)
|
||||||
|
except OperationalError as error:
|
||||||
|
warnings.warn(
|
||||||
|
"Got an error checking a consistent migration history "
|
||||||
|
"performed for database connection '%s': %s."
|
||||||
|
% (alias, error),
|
||||||
|
RuntimeWarning,
|
||||||
|
)
|
||||||
# Before anything else, see if there's conflicting apps and drop out
|
# Before anything else, see if there's conflicting apps and drop out
|
||||||
# hard if there are any and they don't want to merge
|
# hard if there are any and they don't want to merge
|
||||||
conflicts = loader.detect_conflicts()
|
conflicts = loader.detect_conflicts()
|
||||||
|
|
|
@ -824,6 +824,12 @@ Generate migration files without Django version and timestamp header.
|
||||||
Makes ``makemigrations`` exit with a non-zero status when model changes without
|
Makes ``makemigrations`` exit with a non-zero status when model changes without
|
||||||
migrations are detected.
|
migrations are detected.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.2
|
||||||
|
|
||||||
|
Support for calling ``makemigrations`` without an active database
|
||||||
|
connection was added. In that case, check for a consistent migration
|
||||||
|
history is skipped.
|
||||||
|
|
||||||
``migrate``
|
``migrate``
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
|
|
|
@ -157,6 +157,10 @@ Management Commands
|
||||||
* :djadmin:`loaddata` now supports fixtures stored in XZ archives (``.xz``) and
|
* :djadmin:`loaddata` now supports fixtures stored in XZ archives (``.xz``) and
|
||||||
LZMA archives (``.lzma``).
|
LZMA archives (``.lzma``).
|
||||||
|
|
||||||
|
* :djadmin:`makemigrations` can now be called without an active database
|
||||||
|
connection. In that case, check for a consistent migration history is
|
||||||
|
skipped.
|
||||||
|
|
||||||
Migrations
|
Migrations
|
||||||
~~~~~~~~~~
|
~~~~~~~~~~
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,8 @@ from unittest import mock
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.core.management import CommandError, call_command
|
from django.core.management import CommandError, call_command
|
||||||
from django.db import (
|
from django.db import (
|
||||||
ConnectionHandler, DatabaseError, connection, connections, models,
|
ConnectionHandler, DatabaseError, OperationalError, connection,
|
||||||
|
connections, models,
|
||||||
)
|
)
|
||||||
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
|
||||||
from django.db.backends.utils import truncate_name
|
from django.db.backends.utils import truncate_name
|
||||||
|
@ -1555,6 +1556,19 @@ class MakeMigrationsTests(MigrationTestBase):
|
||||||
with self.assertRaisesMessage(InconsistentMigrationHistory, msg):
|
with self.assertRaisesMessage(InconsistentMigrationHistory, msg):
|
||||||
call_command("makemigrations")
|
call_command("makemigrations")
|
||||||
|
|
||||||
|
def test_makemigrations_inconsistent_history_db_failure(self):
|
||||||
|
msg = (
|
||||||
|
"Got an error checking a consistent migration history performed "
|
||||||
|
"for database connection 'default': could not connect to server"
|
||||||
|
)
|
||||||
|
with mock.patch(
|
||||||
|
'django.db.migrations.loader.MigrationLoader.check_consistent_history',
|
||||||
|
side_effect=OperationalError('could not connect to server'),
|
||||||
|
):
|
||||||
|
with self.temporary_migration_module():
|
||||||
|
with self.assertWarnsMessage(RuntimeWarning, msg):
|
||||||
|
call_command('makemigrations', verbosity=0)
|
||||||
|
|
||||||
@mock.patch('builtins.input', return_value='1')
|
@mock.patch('builtins.input', return_value='1')
|
||||||
@mock.patch('django.db.migrations.questioner.sys.stdin', mock.MagicMock(encoding=sys.getdefaultencoding()))
|
@mock.patch('django.db.migrations.questioner.sys.stdin', mock.MagicMock(encoding=sys.getdefaultencoding()))
|
||||||
def test_makemigrations_auto_now_add_interactive(self, *args):
|
def test_makemigrations_auto_now_add_interactive(self, *args):
|
||||||
|
|
Loading…
Reference in New Issue