#27996: Add pgcrypto extension  and GEN_RANDOM_UUID function to contrib.postgres
-------------------------------------+-------------------------------------
     Reporter:  Paolo Melchiorre     |                    Owner:  Paolo
                                     |  Melchiorre
         Type:  New feature          |                   Status:  assigned
    Component:  contrib.postgres     |                  Version:  master
     Severity:  Normal               |               Resolution:
     Keywords:  uuid extension       |             Triage Stage:  Accepted
  function random postgresql         |
  cryptography                       |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Paolo Melchiorre):

 * has_patch:  0 => 1


Old description:

> After the introduction of the UUID Field in Django 1.8, I believe that
> django.contrib.postgres could benefit from some custom functions based on
> the pgcrypto extension of PostgreSQL (see
> https://www.postgresql.org/docs/9.6/static/pgcrypto.html). That kind of
> functions would be very helpful for apply a migration that adds a unique
> non-nullable field to a table with existing rows.
>
> Starting from "Migrations that add unique fields" (see
> https://docs.djangoproject.com/en/dev/howto/writing-migrations
> /#migrations-that-add-unique-fields) I speed up the `gen_uuid` using
> `GEN_RANDOM_UUID` function changing it from:
>
> {{{
> import uuid
>
> def gen_uuid(apps, schema_editor):
>     MyModel = apps.get_model('myapp', 'MyModel')
>     for row in MyModel.objects.all():
>         row.uuid = uuid.uuid4()
>         row.save(update_fields=['uuid'])
> }}}
>
> to
>
> {{{
> from django.contrib.postgres.functions import RandomUUID
>
> def gen_uuid(apps, schema_editor):
>     MyModel = apps.get_model('myapp', 'MyModel')
>     MyModel.objects.update(uuid=RandomUUID())
> }}}
>
> Using this function on my system the time to migrate more than 10000
> objects decreased from
>
> {{{
> real 0m15.988s
> user 0m10.680s
> sys 0m0.508s
> }}}
>
> to
>

> {{{
> real 0m2.957s
> user 0m1.736s
> sys 0m0.072s
> }}}
>
> I already implemented a solution for thi feature and I'm crating a
> related pull request.

New description:

 After the introduction of the UUID Field in Django 1.8, I believe that
 django.contrib.postgres could benefit from some custom functions based on
 the pgcrypto extension of PostgreSQL (see
 https://www.postgresql.org/docs/9.6/static/pgcrypto.html). That kind of
 functions would be very helpful for apply a migration that adds a unique
 non-nullable field to a table with existing rows.

 Starting from "Migrations that add unique fields" (see
 https://docs.djangoproject.com/en/dev/howto/writing-migrations
 /#migrations-that-add-unique-fields) I speed up the `gen_uuid` using
 `GEN_RANDOM_UUID` function changing it from:

 {{{
 import uuid

 def gen_uuid(apps, schema_editor):
     MyModel = apps.get_model('myapp', 'MyModel')
     for row in MyModel.objects.all():
         row.uuid = uuid.uuid4()
         row.save(update_fields=['uuid'])
 }}}

 to

 {{{
 from django.contrib.postgres.functions import RandomUUID

 def gen_uuid(apps, schema_editor):
     MyModel = apps.get_model('myapp', 'MyModel')
     MyModel.objects.update(uuid=RandomUUID())
 }}}

 Using this function on my system the time to migrate more than 10000
 objects decreased from

 {{{
 real 0m15.988s
 user 0m10.680s
 sys 0m0.508s
 }}}

 to


 {{{
 real 0m2.957s
 user 0m1.736s
 sys 0m0.072s
 }}}

 I already implemented a solution for thi feature and I've created a
 [https://github.com/django/django/pull/8265 PR]

--

--
Ticket URL: <https://code.djangoproject.com/ticket/27996#comment:5>
Django <https://code.djangoproject.com/>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.011c11f4389cb9bda03b2abad2838450%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to