mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #23408 -- Added migrations questioner prompt for adding unique fields with a callable default.
This commit is contained in:
committed by
Mariusz Felisiak
parent
0f3e1a54bf
commit
47f791f132
@@ -17,6 +17,8 @@ from django.db.migrations.exceptions import InconsistentMigrationHistory
|
||||
from django.db.migrations.recorder import MigrationRecorder
|
||||
from django.test import TestCase, override_settings, skipUnlessDBFeature
|
||||
from django.test.utils import captured_stdout
|
||||
from django.utils import timezone
|
||||
from django.utils.version import get_docs_version
|
||||
|
||||
from .models import UnicodeModel, UnserializableModel
|
||||
from .routers import TestRouter
|
||||
@@ -1870,6 +1872,57 @@ class MakeMigrationsTests(MigrationTestBase):
|
||||
with self.assertRaises(SystemExit):
|
||||
call_command('makemigrations', 'migrations', interactive=True)
|
||||
|
||||
def test_makemigrations_interactive_unique_callable_default_addition(self):
|
||||
"""
|
||||
makemigrations prompts the user when adding a unique field with
|
||||
a callable default.
|
||||
"""
|
||||
class Book(models.Model):
|
||||
created = models.DateTimeField(unique=True, default=timezone.now)
|
||||
|
||||
class Meta:
|
||||
app_label = 'migrations'
|
||||
|
||||
version = get_docs_version()
|
||||
input_msg = (
|
||||
f'Callable default on unique field book.created will not generate '
|
||||
f'unique values upon migrating.\n'
|
||||
f'Please choose how to proceed:\n\n'
|
||||
f' 1) Continue making this migration as the first step in writing '
|
||||
f'a manual migration to generate unique values described here: '
|
||||
f'https://docs.djangoproject.com/en/{version}/howto/'
|
||||
f'writing-migrations/#migrations-that-add-unique-fields.\n'
|
||||
f' 2) Quit and edit field options in models.py.\n'
|
||||
)
|
||||
with self.temporary_migration_module(module='migrations.test_migrations'):
|
||||
# 2 - quit.
|
||||
with mock.patch('builtins.input', return_value='2'):
|
||||
with captured_stdout() as out, self.assertRaises(SystemExit):
|
||||
call_command('makemigrations', 'migrations', interactive=True)
|
||||
out_value = out.getvalue()
|
||||
self.assertIn(input_msg, out_value)
|
||||
self.assertNotIn('Add field created to book', out_value)
|
||||
# 1 - continue.
|
||||
with mock.patch('builtins.input', return_value='1'):
|
||||
with captured_stdout() as out:
|
||||
call_command('makemigrations', 'migrations', interactive=True)
|
||||
out_value = out.getvalue()
|
||||
self.assertIn(input_msg, out_value)
|
||||
self.assertIn('Add field created to book', out_value)
|
||||
|
||||
def test_makemigrations_non_interactive_unique_callable_default_addition(self):
|
||||
class Book(models.Model):
|
||||
created = models.DateTimeField(unique=True, default=timezone.now)
|
||||
|
||||
class Meta:
|
||||
app_label = 'migrations'
|
||||
|
||||
with self.temporary_migration_module(module='migrations.test_migrations'):
|
||||
with captured_stdout() as out:
|
||||
call_command('makemigrations', 'migrations', interactive=False)
|
||||
out_value = out.getvalue()
|
||||
self.assertIn('Add field created to book', out_value)
|
||||
|
||||
|
||||
class SquashMigrationsTests(MigrationTestBase):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user