-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better Optimization of AttachmentInline #414
base: master
Are you sure you want to change the base?
Conversation
Hey, I do want to move to use Django's native Would the switch to Django's |
Mind fixing the conflict so we can prepare for the next major version of post office @petrprikryl ? |
Yes, I will look at it soon.
I will test it again against our DB and bring some data. I think it will lock but it should be quite a smooth and quick one. |
d6efc9f
to
0b5fdda
Compare
Some migration metrics for our production DB:
I have also bumped the minimal Django version to 3.1 because lower versions don't have generic |
0b5fdda
to
5c1f8f2
Compare
5c1f8f2
to
4694d99
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally we need to add the currently used encoding, to make sure we don't break other peoples code.
I have added this (copy / paste) from jsonfield in the file default_encoder.py
Then we need to import this encoder and use it in the necessary places.
Such as the migrations file (0012) and the models.
Also the context_field_class settings is overridden with this PR.
@petrprikryl mind fixing the conflict so I can merge this in? |
4694d99
to
1a17814
Compare
@@ -12,6 +12,6 @@ class Migration(migrations.Migration): | |||
migrations.AddField( | |||
model_name='attachment', | |||
name='headers', | |||
field=models.JSONField(blank=True, null=True, verbose_name='Headers'), | |||
field=models.TextField(blank=True, null=True, verbose_name='Headers'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, sorry but is there a reason why we're changing past migrations?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/rpkilby/jsonfield/blob/master/src/jsonfield/fields.py#L97 Old JSONField
inherits from TextField
so it is text type in DB on existing (migrated) projects. In this commit https://github.com/ui/django-post_office/pull/438/files#diff-2db5629c948ee3989f24ffc66174cb1c66aeb90d8ae1b01322084ea7987f9144 the JSONField
s have been replaced. But the DB types of columns were not changed in DB because there is no new migration. And because Django's JSONField
has different type in DB we need migrate the columns https://github.com/django/django/blob/4.2b1/django/db/backends/postgresql/base.py#L113. So new migration is required (0012). And without change in previous migrations, Django will skip this DB operations because "there are no model changes between migrations". You can try it. So you need to force Django via field changing to run the column altering. Btw it is more backward compatible because of old JSONField
was text DB type.
Next, this problem wouldn't occur on new projects which are migrated completely from scratch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach doesn't feel right to me. How about adding a SQL migration which changes the type of that field by force.
The problem with backward compatibility is, that migration 0008_attachment_headers
already ran on the affected installations, so wouldn't have any effect anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then we would need write custom SQL for each database that Django supports? :-/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach doesn't feel right to me. How about adding a SQL migration which changes the type of that field by force.
I agree with @jrief here.
How about this? We'll just add a new columns using Django's built in JSONField
in this release and start reading from the new field, with fallback to the old one.
In a future version, we'll drop the old JSON field altogether.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is resolved by #438 so I have removed all migrations from this PR.
1a17814
to
01e0b6a
Compare
Preventing iteration over all attachments in database.
I think that this PR is better than #413 but in cost of replacing https://pypi.org/project/jsonfield/ in favor of Django's native
JSONField
that could be breaking somewhere.