[Django] #31392: Avoid getting the model field default value during migrations if there is not a change from NULL to NOT NULL

24 views
Skip to first unread message

Django

unread,
Mar 22, 2020, 12:51:30 AM3/22/20
to django-...@googlegroups.com
#31392: Avoid getting the model field default value during migrations if there is
not a change from NULL to NOT NULL
-----------------------------------------+------------------------
Reporter: Shipeng Feng | Owner: nobody
Type: Uncategorized | Status: new
Component: Migrations | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Example:


{{{
#!python
def old_default_code():
return 'old'

def new_default_code():
# running something that is stateful, for example, increase a database
counter

class Student(models.Model):
code = models.CharField(default=new_default_code)
}}}

We change the default callable object, and run {{{manage.py migrate}}},
ideally {{{new_default_code}}} shouldn't be
called, however, we are getting the model field default value no matter
whether we need it or not:

https://github.com/django/django/blob/master/django/db/backends/base/schema.py#L679

I'd love to send a pull request for this if necessary.

--
Ticket URL: <https://code.djangoproject.com/ticket/31392>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 23, 2020, 4:17:02 AM3/23/20
to django-...@googlegroups.com
#31392: Avoid calling SchemaEditor.effective_default() if not necessary.
-------------------------------------+-------------------------------------

Reporter: Shipeng Feng | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* type: Uncategorized => Cleanup/optimization
* component: Migrations => Database layer (models, ORM)
* easy: 0 => 1
* stage: Unreviewed => Accepted


Comment:

Agreed, we can add this small optimization, i.e. avoid calling
`SchemaEditor.effective_default()` if we don't change a field to a non-
nullable.

--
Ticket URL: <https://code.djangoproject.com/ticket/31392#comment:1>

Django

unread,
Mar 23, 2020, 7:56:18 AM3/23/20
to django-...@googlegroups.com
#31392: Avoid calling SchemaEditor.effective_default() if not necessary.
-------------------------------------+-------------------------------------
Reporter: Shipeng Feng | Owner: Shipeng
Type: | Feng
Cleanup/optimization | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Shipeng Feng):

* owner: nobody => Shipeng Feng
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/31392#comment:2>

Django

unread,
Mar 23, 2020, 8:57:17 AM3/23/20
to django-...@googlegroups.com
#31392: Avoid calling SchemaEditor.effective_default() if not necessary.
-------------------------------------+-------------------------------------
Reporter: Shipeng Feng | Owner: Shipeng
Type: | Feng
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Shipeng Feng):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/12612 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/31392#comment:3>

Django

unread,
Mar 24, 2020, 2:15:22 AM3/24/20
to django-...@googlegroups.com
#31392: Avoid calling SchemaEditor.effective_default() if not necessary.
-------------------------------------+-------------------------------------
Reporter: Shipeng Feng | Owner: Shipeng
Type: | Feng
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/31392#comment:4>

Django

unread,
Mar 24, 2020, 8:08:32 AM3/24/20
to django-...@googlegroups.com
#31392: Avoid calling SchemaEditor.effective_default() if not necessary.
-------------------------------------+-------------------------------------
Reporter: Shipeng Feng | Owner: Shipeng
Type: | Feng
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/31392#comment:5>

Django

unread,
Mar 24, 2020, 8:10:02 AM3/24/20
to django-...@googlegroups.com
#31392: Avoid calling SchemaEditor.effective_default() if not necessary.
-------------------------------------+-------------------------------------
Reporter: Shipeng Feng | Owner: Shipeng
Type: | Feng
Cleanup/optimization | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"8fe2447a01232d9598bf4692d1face12fa074288" 8fe2447a]:
{{{
#!CommitTicketReference repository=""
revision="8fe2447a01232d9598bf4692d1face12fa074288"
Fixed #31392 -- Avoided unnecessary SchemaEditor.effective_default() calls
when altering a field.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31392#comment:6>

Reply all
Reply to author
Forward
0 new messages