Skip to content

Commit bc8cee0

Browse files
committed
Django3: add new django.db.models.JSONField
Create new JSON fields postfixed with `_json` for all the JSON fields defined via `jsonfield` third party package.
1 parent e7102a4 commit bc8cee0

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 3.2.11 on 2022-01-31 11:47
2+
3+
from django.db import migrations, models
4+
from django.db.models import F
5+
from django.db.models.functions import Cast
6+
7+
8+
def forwards_func(apps, schema_editor):
9+
Build = apps.get_model('builds', 'Build')
10+
(
11+
Build.objects
12+
.annotate(_config_in_json=Cast('_config', output_field=models.JSONField()))
13+
# Copy `_config` JSONField (text) into `_config_json` (jsonb)
14+
.update(_config_json=F('_config_in_json'))
15+
)
16+
17+
18+
class Migration(migrations.Migration):
19+
20+
dependencies = [
21+
('builds', '0037_alter_build_cold_storage'),
22+
]
23+
24+
operations = [
25+
migrations.AddField(
26+
model_name='build',
27+
name='_config_json',
28+
field=models.JSONField(default=dict, verbose_name='Configuration used in the build'),
29+
),
30+
migrations.RunPython(forwards_func)
31+
]

readthedocs/builds/models.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,7 @@ class Build(models.Model):
648648
blank=True,
649649
)
650650
_config = JSONField(_('Configuration used in the build'), default=dict)
651+
_config_json = models.JSONField(_('Configuration used in the build'), default=dict)
651652

652653
length = models.IntegerField(_('Build Length'), null=True, blank=True)
653654

@@ -716,6 +717,10 @@ def config(self):
716717
Build object (it could be stored in this object or one of the previous
717718
ones).
718719
"""
720+
# TODO: now that we are using a proper JSONField here, we could
721+
# probably change this field to be a ForeignKey to avoid repeating the
722+
# config file over and over again and re-use them to save db data as
723+
# well
719724
if self.CONFIG_KEY in self._config:
720725
return (
721726
Build.objects

readthedocs/integrations/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class IntegrationAdmin(admin.ModelAdmin):
9393
search_fields = ('project__slug', 'project__name')
9494
readonly_fields = ['exchanges']
9595

96+
# TODO: review this now that we are using official Django's JSONField
9697
def exchanges(self, obj):
9798
"""
9899
Manually make an inline-ish block.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Generated by Django 3.2.11 on 2022-01-31 11:47
2+
3+
from django.db import migrations, models
4+
from django.db.models import F
5+
from django.db.models.functions import Cast
6+
7+
8+
def forwards_func(apps, schema_editor):
9+
HttpExchange = apps.get_model('integrations', 'HttpExchange')
10+
(
11+
HttpExchange.objects
12+
.annotate(
13+
request_headers_in_json=Cast('request_headers', output_field=models.JSONField()),
14+
response_headers_in_json=Cast('response_headers', output_field=models.JSONField()),
15+
)
16+
.update(
17+
request_headers_json=F('request_headers_in_json'),
18+
response_headers_json=F('response_headers_in_json'),
19+
)
20+
)
21+
22+
23+
Integration = apps.get_model('integrations', 'Integration')
24+
(
25+
Integration.objects
26+
.annotate(
27+
provider_data_in_json=Cast('provider_data', output_field=models.JSONField()),
28+
)
29+
.update(
30+
provider_data_json=F('provider_data_in_json'),
31+
)
32+
)
33+
34+
35+
class Migration(migrations.Migration):
36+
37+
dependencies = [
38+
('integrations', '0007_update-provider-data'),
39+
]
40+
41+
operations = [
42+
migrations.AddField(
43+
model_name='httpexchange',
44+
name='request_headers_json',
45+
field=models.JSONField(default=None, null=True, verbose_name='Request headers'),
46+
),
47+
migrations.AddField(
48+
model_name='httpexchange',
49+
name='response_headers_json',
50+
field=models.JSONField(default=None, null=True, verbose_name='Request headers'),
51+
),
52+
migrations.AddField(
53+
model_name='integration',
54+
name='provider_data_json',
55+
field=models.JSONField(default=dict, verbose_name='Provider data'),
56+
),
57+
migrations.RunPython(forwards_func)
58+
]

readthedocs/integrations/models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,21 @@ class HttpExchange(models.Model):
145145
date = models.DateTimeField(_('Date'), auto_now_add=True)
146146

147147
request_headers = JSONField(_('Request headers'))
148+
request_headers_json = models.JSONField(
149+
_('Request headers'),
150+
# Delete after deploy
151+
null=True,
152+
default=None,
153+
)
148154
request_body = models.TextField(_('Request body'))
149155

150156
response_headers = JSONField(_('Request headers'))
157+
response_headers_json = models.JSONField(
158+
_('Request headers'),
159+
# Delete after deploy
160+
null=True,
161+
default=None,
162+
)
151163
response_body = models.TextField(_('Response body'))
152164

153165
status_code = models.IntegerField(
@@ -278,6 +290,7 @@ class Integration(models.Model):
278290
choices=INTEGRATIONS,
279291
)
280292
provider_data = JSONField(_('Provider data'), default=dict)
293+
provider_data_json = models.JSONField(_('Provider data'), default=dict)
281294
exchanges = GenericRelation(
282295
'HttpExchange',
283296
related_query_name='integrations',

0 commit comments

Comments
 (0)