Skip to content

Deploy: avoid locking the table when adding new JSON field #8926

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

Merged
merged 4 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion readthedocs/builds/migrations/0038_add_new_jsonfields.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='build',
name='_config_json',
field=models.JSONField(default=dict, verbose_name='Configuration used in the build'),
field=models.JSONField(null=True, blank=True, verbose_name='Configuration used in the build'),
),
]
22 changes: 0 additions & 22 deletions readthedocs/builds/migrations/0040_remove_old_jsonfields.py

This file was deleted.

8 changes: 7 additions & 1 deletion readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ModificationDateTimeField,
)
from django_extensions.db.models import TimeStampedModel
from jsonfield import JSONField
from polymorphic.models import PolymorphicModel

import readthedocs.builds.automation_actions as actions
Expand Down Expand Up @@ -661,7 +662,12 @@ class Build(models.Model):
null=True,
blank=True,
)
_config = models.JSONField(_('Configuration used in the build'), default=dict)
_config = JSONField(_('Configuration used in the build'), default=dict)
_config_json = models.JSONField(
_('Configuration used in the build'),
null=True,
blank=True,
)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='httpexchange',
name='request_headers_json',
field=models.JSONField(default=None, null=True, verbose_name='Request headers'),
field=models.JSONField(null=True, blank=True, verbose_name='Request headers'),
),
migrations.AddField(
model_name='httpexchange',
name='response_headers_json',
field=models.JSONField(default=None, null=True, verbose_name='Request headers'),
field=models.JSONField(null=True, blank=True, verbose_name='Request headers'),
),
migrations.AddField(
model_name='integration',
name='provider_data_json',
field=models.JSONField(default=dict, verbose_name='Provider data'),
field=models.JSONField(null=True, blank=True, verbose_name='Provider data'),
),
]
40 changes: 0 additions & 40 deletions readthedocs/integrations/migrations/0010_remove_old_jsonfields.py

This file was deleted.

25 changes: 15 additions & 10 deletions readthedocs/integrations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.db import models, transaction
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from jsonfield import JSONField
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import JsonLexer
Expand Down Expand Up @@ -101,15 +102,12 @@ def from_requests_exchange(self, response, related_object):
:param related_object: Object to use for generic relationship.
"""
request = response.request
# NOTE: we need to cast ``request.headers`` and ``response.headers``
# because it's a ``requests.structures.CaseInsensitiveDict`` which is
# not JSON serializable.
obj = self.create(
related_object=related_object,
request_headers=dict(request.headers) or {},
request_headers=request.headers or {},
request_body=request.body or '',
status_code=response.status_code,
response_headers=dict(response.headers),
response_headers=response.headers,
response_body=response.text,
)
self.delete_limit(related_object)
Expand Down Expand Up @@ -146,19 +144,21 @@ class HttpExchange(models.Model):

date = models.DateTimeField(_('Date'), auto_now_add=True)

request_headers = models.JSONField(
request_headers = JSONField(_('Request headers'))
request_headers_json = models.JSONField(
_('Request headers'),
# Delete after deploy
null=True,
default=None,
blank=True,
)
request_body = models.TextField(_('Request body'))

response_headers = models.JSONField(
response_headers = JSONField(_('Request headers'))
response_headers_json = models.JSONField(
_('Request headers'),
# Delete after deploy
null=True,
default=None,
blank=True,
)
response_body = models.TextField(_('Response body'))

Expand Down Expand Up @@ -298,7 +298,12 @@ class Integration(models.Model):
max_length=32,
choices=INTEGRATIONS,
)
provider_data = models.JSONField(_('Provider data'), default=dict)
provider_data = JSONField(_('Provider data'), default=dict)
provider_data_json = models.JSONField(
_('Provider data'),
null=True,
blank=True,
)
exchanges = GenericRelation(
'HttpExchange',
related_query_name='integrations',
Expand Down
4 changes: 0 additions & 4 deletions requirements/pip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ drf-flex-fields==0.9.7
drf-extensions==0.7.1

django-vanilla-views==3.0.0

# This module is only used on migrations. We are now using Django's official
# JSONField. We should probably squash these migrations and remove this
# dependency as well.
jsonfield==3.1.0

requests==2.27.1
Expand Down