Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2024-03-18 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  dev
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  1
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Ülgen Sarıkavak):

 * cc: Ülgen Sarıkavak (added)

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018e50537c0a-0739a871-e338-41bd-b7e3-c4e1c5417cb6-00%40eu-central-1.amazonses.com.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-03-05 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  master
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  1
Easy pickings:  0 |UI/UX:  0
--+

Comment (by Дилян Палаузов):

 The purpose of the change is to tweak the migrations auto_now_add ->
 default for DateField and DateTimeField not to call

  ALTER TABLE SET DEFAULT '2017-10-16T09:35:52.710695'::timestamp;
  ALTER TABLE DROP DEFAULT

 towards the database. The attached auto_now_add_skip_sql_calls.2.patch
 touches BaseDatabaseSchemaEditor, and does generate a migration, which
 however skips modifying the DEFAULT.

 auto_now_add being possibly deprecated means that this type of migration
 will be created more often in the future and this patch accelerates the
 execution of the migration (as it does not lock the table).

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.d99520bec1008d0c3b62a75c7bd2f8ac%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-03-05 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  master
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  1
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Дилян Палаузов):

 * Attachment "auto_now_add_skip_sql_calls.2.patch" added.


-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.6eade91342babd2c6945f396c8b6ac9e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-03-03 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  master
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  1
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Tim Graham):

 * needs_better_patch:  0 => 1


Comment:

 Sorry, I should have looked at the initial patch before I suggested
 bringing it up to date. I don't think the approach is correct. I think
 that a migration should be generated if `auto_now_add=True` changes to
 `default=timezone.now` as they aren't entirely equivalent. The fix will
 have to be at the `SchemaEditor` level. Given that `auto_now_add` and
 `auto_now` might be deprecated (#22995), it might be a better use of time
 to focus on that issue (although the patch forward isn't entirely clear).

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.468ee251864d9541390a26954306d009%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-03-02 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  master
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Дилян Палаузов):

 * Attachment "auto_now_add_skip_sql_calls.patch" added.

 Probably somebody with more experience will tweak the
 
test.migrations.test_autodetector.test_add_date_fields_with_auto_now_add_(not_asking_for_null_addition,
 add_asking_for_default) tests, so that the line "kwargs['auto_now_add'] =
 True" in django/db/models/fields/__init__.py can be removed.  But
 otherwise this shall be ready to go.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.7b63234cee3b711844401e8f19c1cedb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-03-02 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  master
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Дилян Палаузов):

 * Attachment "auto_now_add_skip_sql_calls.patch" added.

 Probably somebody with more experience will tweak the
 
test.migrations.test_autodetector.test_add_date_fields_with_auto_now_add_(not_asking_for_null_addition,
 add_asking_for_default) tests, so that the line "kwargs['auto_now_add'] =
 True" in django/db/models/fields/__init__.py can be removed.  But
 otherwise this shall be ready to go.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.56e0d3ed77c7ed18adc2acf1464a6a62%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL (was: Migration DateTimeField(auto_now_add=True -> default=django.utils.t

2018-03-01 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  master
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Дилян Палаузов):

 * Attachment "auto_now_add_skip_sql_calls.patch" removed.

 Probably somebody with more experience will tweak the
 
test.migrations.test_autodetector.test_add_date_fields_with_auto_now_add_(not_asking_for_null_addition,
 add_asking_for_default) tests, so that the line "kwargs['auto_now_add'] =
 True" in django/db/models/fields/__init__.py can be removed.  But
 otherwise this shall be ready to go.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.36f811bf35218cfae903b3657f417594%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-03-01 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  master
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Дилян Палаузов):

 * Attachment "auto_now_add_skip_sql_calls.patch" added.

 Probably somebody with more experience will tweak the
 
test.migrations.test_autodetector.test_add_date_fields_with_auto_now_add_(not_asking_for_null_addition,
 add_asking_for_default) tests, so that the line "kwargs['auto_now_add'] =
 True" in django/db/models/fields/__init__.py can be removed.  But
 otherwise this shall be ready to go.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.e41c715e318f36b09770f9ca541fa1d6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-03-01 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  master
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  1 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Дилян Палаузов):

 * has_patch:  0 => 1
 * version:  1.11 => master


-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.b9961fdb6df9cb41ff7d1c00ce9a53f4%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-03-01 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  1.11
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Дилян Палаузов):

 * Attachment "auto_now_add_skip_sql_calls.patch" added.

 Probably somebody with more experience will tweak the
 
test.migrations.test_autodetector.test_add_date_fields_with_auto_now_add_(not_asking_for_null_addition,
 add_asking_for_default) tests, so that the line "kwargs['auto_now_add'] =
 True" in django/db/models/fields/__init__.py can be removed.  But
 otherwise this shall be ready to go.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.9adbd3d47943dd6da2cce7ade23de4bf%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-02-28 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  1.11
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+

Comment (by Tim Graham):

 The patch must apply cleanly to Django's master branch and it must have a
 test. Ideally, you could provide the patch as a pull request and then
 check "Has patch" on the ticket so that it appears in the review queue.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.ea6b98a33af84fb29e67cc85a13aebaa%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2018-02-28 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  1.11
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+

Comment (by Дилян Палаузов):

 My plan is to work one more month with Django, so if there are concerns on
 this patch, please raise them by then.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.0c6f8c0b5c81ef3964365f36da2b798a%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.


Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2017-10-23 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  1.11
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+
Description changed by Дилян Палаузов:

Old description:

> A switch from DateTimeField(auto_now_add=True) to
> DateTimeField(default=django.utils.timezone.new) creates the statements
>   ALTER TABLE SET DEFAULT '2017-10-16T09:35:52.710695'::timestamp;
>   ALTER TABLE DROP DEFAULT
> which have no effects, apart from locking the whole table twice.
>
> A 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:
> {{{
> diff --git a/django/db/backends/base/schema.py
> b/django/db/backends/base/schema.py
> --- a/django/db/backends/base/schema.py
> +++ b/django/db/backends/base/schema.py
> @@ -199,28 +199,33 @@ class BaseDatabaseSchemaEditor(object):
>  'requires_literal_defaults must provide a prepare_default()
> method'
>  )
>
> -def effective_default(self, field):
> +@staticmethod
> +def effective_default_before_callable(field):
>  """
> -Returns a field's effective database default value
> +Returns a field's effective database default callable or value
>  """
>  if field.has_default():
> -default = field.get_default()
> +return field._get_default
>  elif not field.null and field.blank and
> field.empty_strings_allowed:
>  if field.get_internal_type() == "BinaryField":
> -default = six.binary_type()
> +return six.binary_type()
>  else:
> -default = six.text_type()
> +return six.text_type()
>  elif getattr(field, 'auto_now', False) or getattr(field,
> 'auto_now_add', False):
>  default = datetime.now()
>  internal_type = field.get_internal_type()
>  if internal_type == 'DateField':
> -default = default.date
> +return default.date
>  elif internal_type == 'TimeField':
> -default = default.time
> +return default.time
>  elif internal_type == 'DateTimeField':
> -default = timezone.now
> -else:
> -default = None
> +return timezone.now
> +
> +def effective_default(self, field):
> +"""
> +Returns a field's effective database default value
> +"""
> +default =
> BaseDatabaseSchemaEditor.effective_default_before_callable(field)
>  # If it's a callable, call it
>  if callable(default):
>  default = default()
> @@ -615,6 +620,7 @@ class BaseDatabaseSchemaEditor(object):
>  old_default != new_default and
>  new_default is not None and
>  not self.skip_default(new_field)
> +and
> BaseDatabaseSchemaEditor.effective_default_before_callable(old_field) !=
> BaseDatabaseSchemaEdit
>  )
>  if needs_database_default:
>  if self.connection.features.requires_literal_defaults:
> diff --git a/django/db/models/fields/__init__.py
> b/django/db/models/fields/__init__.py
> --- a/django/db/models/fields/__init__.py
> +++ b/django/db/models/fields/__init__.py
> @@ -1232,7 +1232,7 @@ class DateField(DateTimeCheckMixin, Field):
>  if self.auto_now:
>  kwargs['auto_now'] = True
>  if self.auto_now_add:
> -kwargs['auto_now_add'] = True
> +kwargs['default'] = timezone.now
>  if self.auto_now or self.auto_now_add:
>  del kwargs['editable']
>  del kwargs['blank']
> }}}

New description:

 A switch from DateTimeField(auto_now_add=True) to
 DateTimeField(default=django.utils.timezone.new) creates the statements
   ALTER TABLE SET DEFAULT '2017-10-16T09:35:52.710695'::timestamp;
   ALTER TABLE DROP DEFAULT
 which have no effects, apart from locking the whole table twice.

 A 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:
 {{{
 diff --git 

Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL

2017-10-23 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  1.11
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+
Description changed by Дилян Палаузов:

Old description:

> A switch from DateTimeField(auto_now_add=True) to
> DateTimeField(default=django.utils.timezone.new) creates the statements
>   ALTER TABLE SET DEFAULT '2017-10-16T09:35:52.710695'::timestamp;
>   ALTER TABLE DROP DEFAULT
> which have no effects, apart from locking the whole table.
>
> A proposal to recognize, when the effective default-callable doesn't
> change:
> {{{
> diff --git a/django/db/backends/base/schema.py
> b/django/db/backends/base/schema.py
> --- a/django/db/backends/base/schema.py
> +++ b/django/db/backends/base/schema.py
> @@ -199,28 +199,32 @@ class BaseDatabaseSchemaEditor(object):
>  'requires_literal_defaults must provide a prepare_default()
> method'
>  )
>
> -def effective_default(self, field):
> +def effective_default_before_callable(self, field):
>  """
> -Returns a field's effective database default value
> +Returns a field's effective database default callable or value
>  """
>  if field.has_default():
> -default = field.get_default()
> +return field._get_default
>  elif not field.null and field.blank and
> field.empty_strings_allowed:
>  if field.get_internal_type() == "BinaryField":
> -default = six.binary_type()
> +return six.binary_type()
>  else:
> -default = six.text_type()
> +return six.text_type()
>  elif getattr(field, 'auto_now', False) or getattr(field,
> 'auto_now_add', False):
>  default = datetime.now()
>  internal_type = field.get_internal_type()
>  if internal_type == 'DateField':
> -default = default.date
> +return default.date
>  elif internal_type == 'TimeField':
> -default = default.time
> +return default.time
>  elif internal_type == 'DateTimeField':
> -default = timezone.now
> -else:
> -default = None
> +return timezone.now
> +
> +def effective_default(self, field):
> +"""
> +Returns a field's effective database default value
> +"""
> +default = self.effective_default_before_callable(field)
>  # If it's a callable, call it
>  if callable(default):
>  default = default()
> @@ -615,6 +619,7 @@ class BaseDatabaseSchemaEditor(object):
>  old_default != new_default and
>  new_default is not None and
>  not self.skip_default(new_field)
> +and self.effective_default_before_callable(old_field) !=
> self.effective_default_before_callable(new
>  )
>  if needs_database_default:
>  if self.connection.features.requires_literal_defaults:
>
> }}}

New description:

 A switch from DateTimeField(auto_now_add=True) to
 DateTimeField(default=django.utils.timezone.new) creates the statements
   ALTER TABLE SET DEFAULT '2017-10-16T09:35:52.710695'::timestamp;
   ALTER TABLE DROP DEFAULT
 which have no effects, apart from locking the whole table twice.

 A 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:
 {{{
 diff --git a/django/db/backends/base/schema.py
 b/django/db/backends/base/schema.py
 --- a/django/db/backends/base/schema.py
 +++ b/django/db/backends/base/schema.py
 @@ -199,28 +199,33 @@ class BaseDatabaseSchemaEditor(object):
  'requires_literal_defaults must provide a prepare_default()
 method'
  )

 -def effective_default(self, field):
 +@staticmethod
 +def effective_default_before_callable(field):
  """
 -Returns a field's effective database default value
 +Returns a field's effective database default callable or value
  """
  if field.has_default():
 -default = field.get_default()
 +return field._get_default
  elif not field.null and 

Re: [Django] #28715: Prevent a migration changing DateTimeField(auto_now_add=True) to default=timezone.now from generating SQL (was: Migration DateTimeField(auto_now_add=True -> default=django.utils.t

2017-10-19 Thread Django
#28715: Prevent a migration changing DateTimeField(auto_now_add=True) to
default=timezone.now from generating SQL
--+
 Reporter:  Дилян Палаузов|Owner:  nobody
 Type:  Cleanup/optimization  |   Status:  new
Component:  Migrations|  Version:  1.11
 Severity:  Normal|   Resolution:
 Keywords:| Triage Stage:  Accepted
Has patch:  0 |  Needs documentation:  0
  Needs tests:  0 |  Patch needs improvement:  0
Easy pickings:  0 |UI/UX:  0
--+
Changes (by Tim Graham):

 * stage:  Unreviewed => Accepted
 * type:  Uncategorized => Cleanup/optimization


-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/072.6ee1053ee6f29ec77f86d6ab51485c4d%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.