-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Replace data_json
TextField
with data
JSONField
in BaseLogEntry
#8021
Conversation
@@ -978,9 +978,9 @@ Database fields | |||
|
|||
A foreign key to the user that triggered the action. | |||
|
|||
.. attribute:: data_json | |||
.. attribute:: data |
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.
Probably need to add a note here somewhere to indicate the name change.
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 agree this would be good - you could pair it with a .. versionchanged:: version
to let people know about the details of the change (see https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html)
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.
Added.
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('wagtailcore', '0067_alter_modellogentry_data_json_and_more'), |
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.
We can squash the migrations if desired.
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.
Yep, I think combining the migrations would be good.
wagtail/core/models/audit_log.py
Outdated
@@ -100,7 +99,7 @@ def log_action(self, instance, action, **kwargs): | |||
% (instance,) | |||
) | |||
|
|||
data = kwargs.pop("data", "") | |||
data = kwargs.pop("data", {}) or {} |
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 is just in case the user uses ""
, []
, or other falsy values.
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.
The kwargs.pop("data", {})
, if data
isn't present, will return {}
and then immediately get overwritten because {}
is also an empty value? Nitpick, but I think None
might be clearer here - also, I suggest adding a comment to the effect that this is to prevent ""
due to the old TextField
type, since usually we wouldn't need to be quite so defensive.
Note: this fixes #6942 |
Thanks. I'll take a look into it, but it's likely to be trickier. |
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.
Just a few little tweaks and testing on postgres - generally this is looking great!
Maybe also worth a brief note in upgrade considerations for the new release (along with the other PR)
@@ -978,9 +978,9 @@ Database fields | |||
|
|||
A foreign key to the user that triggered the action. | |||
|
|||
.. attribute:: data_json | |||
.. attribute:: data |
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 agree this would be good - you could pair it with a .. versionchanged:: version
to let people know about the details of the change (see https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html)
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('wagtailcore', '0067_alter_modellogentry_data_json_and_more'), |
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.
Yep, I think combining the migrations would be good.
wagtail/core/models/audit_log.py
Outdated
@@ -100,7 +99,7 @@ def log_action(self, instance, action, **kwargs): | |||
% (instance,) | |||
) | |||
|
|||
data = kwargs.pop("data", "") | |||
data = kwargs.pop("data", {}) or {} |
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.
The kwargs.pop("data", {})
, if data
isn't present, will return {}
and then immediately get overwritten because {}
is also an empty value? Nitpick, but I think None
might be clearer here - also, I suggest adding a comment to the effect that this is to prevent ""
due to the old TextField
type, since usually we wouldn't need to be quite so defensive.
Testing this PR on wagtail-bakery using docker-wagtail-develop: BeforeAfterI can query with JSON-specific operators on I can also query with JSONField-specific path lookup, and the model field type will be Potential problemWe use wagtail/wagtail/core/management/commands/create_log_entries_from_revisions.py Lines 103 to 112 in 4a7fb00
wagtail/wagtail/core/models/audit_log.py Line 103 in 4a7fb00
When the This still works fine on PostgreSQL, but it breaks on Oracle, which does not support storing scalar values on the top-level of a JSON column (it can only store JSON object or array). See https://github.com/django/django/blob/7fd2deb3e86a021b3108027d786d45657243532c/django/db/backends/oracle/features.py#L66 Possible fixCreate a migration that changes all |
9f52875
to
43bc490
Compare
Manage this branch in SquashTest this branch here: https://laymonageuse-jsonfield-logentr-qjkom.squash.io |
2eecc02
to
ef86147
Compare
ef86147
to
8716236
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.
LGTM, thanks @laymonage!
Hi @laymonage, I want to report something that just happened to me that may be related to this PR. I was attempting to
Here is the relevant snipped of the dumped JSON: {
"model": "wagtailcore.pagelogentry",
"pk": 2,
"fields": {
"content_type": [
"standard_page",
"standardpage"
],
"label": "Services",
"action": "wagtail.publish",
"data": null,
"timestamp": "2022-04-11T19:15:26.045Z",
"uuid": "49cef16c-4ef3-4492-a03e-58a59e3637da",
"user": [
"kmrichar"
],
"content_changed": true,
"deleted": false,
"page": 4,
"revision": 1
}
}, And if I directly access the production DB, the
Potentially relevant details:
It seems like the Any idea what might be happening here? |
Hey @Scotchester, I'm not too sure about it but I'm guessing that for some reason the log entry was created with an explicit After the upgrade, I assume doing
When doing Do you have somewhere in your code that creates a log entry with |
Almost a year later, but I think I've found the cause of the above issue when investigating a potential problem in #9590. Apparently the
Any new logs created since then will always normalise to an empty JSON object wagtail/wagtail/models/audit_log.py Line 115 in 041cdd2
The |
Old logs may have the data set to JSON `null` instead of an empty JSON object `{}`. See wagtail#8021 (comment) and wagtail#9590 (comment) for more details.
Old logs may have the data set to JSON `null` instead of an empty JSON object `{}`. See wagtail#8021 (comment) and wagtail#9590 (comment) for more details.
Old logs may have the data set to JSON `null` instead of an empty JSON object `{}`. See wagtail#8021 (comment) and wagtail#9590 (comment) for more details.
Old logs may have the data set to JSON `null` instead of an empty JSON object `{}`. See #8021 (comment) and #9590 (comment) for more details.
Fixes #6942.
Since we only support Django 3.2+, we can now replace fields that hold JSON data using
TextField
with the cross-databaseJSONField
from Django 3.1.Not sure if this should be a breaking change, though. The public API mostly stays the same, as users are likely to use the previously
@cached_property
data
in order to access the deserialized data. Thelog_action()
method also acceptsdata
instead ofdata_json
, so this change would mostly be unnoticable. Unless there are users that accessdata_json
directly...More docs would probably needed. Let me know where to add more info.
The next one would be
wagtailcore.PageRevision.content_json
. I tried to include it in this PR, but it seems trickier than I expected as it requires some changes todjango-modelcluster
.