Opened 8 years ago
Last modified 4 years ago
#27996 closed New feature
Add pgcrypto extension and GEN_RANDOM_UUID function to contrib.postgres — at Version 5
Reported by: | Paolo Melchiorre | Owned by: | Paolo Melchiorre |
---|---|---|---|
Component: | contrib.postgres | Version: | dev |
Severity: | Normal | Keywords: | uuid extension function random postgresql cryptography |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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 PR
Change History (5)
comment:1 by , 8 years ago
comment:2 by , 8 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:3 by , 8 years ago
Description: | modified (diff) |
---|
comment:4 by , 8 years ago
Has patch: | unset |
---|
comment:5 by , 8 years ago
Description: | modified (diff) |
---|---|
Has patch: | set |
I just submitted a full featured pull request on ghithub https://github.com/django/django/pull/8265