Changes between Initial Version and Version 2 of Ticket #28715


Ignore:
Timestamp:
Oct 23, 2017, 5:39:11 AM (7 years ago)
Author:
Дилян Палаузов
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #28715

    • Property Triage Stage UnreviewedAccepted
    • Property Summary Migration DateTimeField(auto_now_add=True -> default=django.utils.timezone.new)Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL
    • Property Type UncategorizedCleanup/optimization
  • Ticket #28715 – Description

    initial v2  
    22  ALTER TABLE SET DEFAULT '2017-10-16T09:35:52.710695'::timestamp;
    33  ALTER TABLE DROP DEFAULT
    4 which have no effects, apart from locking the whole table.
     4which have no effects, apart from locking the whole table twice.
    55
    6 A proposal to recognize, when the effective default-callable doesn't change:
     6A proposal to recognize, when the effective default-callable doesn't change and skip changing the DEFAULT twice in this case, as well as not generating a migration when this is the only change on a field:
    77{{{
    88diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
    99--- a/django/db/backends/base/schema.py
    1010+++ b/django/db/backends/base/schema.py
    11 @@ -199,28 +199,32 @@ class BaseDatabaseSchemaEditor(object):
     11@@ -199,28 +199,33 @@ class BaseDatabaseSchemaEditor(object):
    1212             'requires_literal_defaults must provide a prepare_default() method'
    1313         )
    1414 
    1515-    def effective_default(self, field):
    16 +    def effective_default_before_callable(self, field):
     16+    @staticmethod
     17+    def effective_default_before_callable(field):
    1718         """
    1819-        Returns a field's effective database default value
     
    4849+        Returns a field's effective database default value
    4950+        """
    50 +        default = self.effective_default_before_callable(field)
     51+        default = BaseDatabaseSchemaEditor.effective_default_before_callable(field)
    5152         # If it's a callable, call it
    5253         if callable(default):
    5354             default = default()
    54 @@ -615,6 +619,7 @@ class BaseDatabaseSchemaEditor(object):
     55@@ -615,6 +620,7 @@ class BaseDatabaseSchemaEditor(object):
    5556             old_default != new_default and
    5657             new_default is not None and
    5758             not self.skip_default(new_field)
    58 +            and self.effective_default_before_callable(old_field) != self.effective_default_before_callable(new
     59+            and BaseDatabaseSchemaEditor.effective_default_before_callable(old_field) != BaseDatabaseSchemaEdit
    5960         )
    6061         if needs_database_default:
    6162             if self.connection.features.requires_literal_defaults:
    62 
     63diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
     64--- a/django/db/models/fields/__init__.py
     65+++ b/django/db/models/fields/__init__.py
     66@@ -1232,7 +1232,7 @@ class DateField(DateTimeCheckMixin, Field):
     67         if self.auto_now:
     68             kwargs['auto_now'] = True
     69         if self.auto_now_add:
     70-            kwargs['auto_now_add'] = True
     71+            kwargs['default'] = timezone.now
     72         if self.auto_now or self.auto_now_add:
     73             del kwargs['editable']
     74             del kwargs['blank']
    6375}}}
Back to Top