-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Django3: add new django.db.models.JSONField
#8868
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
Changes from 3 commits
bc8cee0
db3cfd8
39ce37b
f80ac71
870caf6
0da786e
0b90cd3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 3.2.11 on 2022-01-31 11:47 | ||
|
||
from django.db import migrations, models | ||
from django.db.models import F | ||
from django.db.models.functions import Cast | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('builds', '0037_alter_build_cold_storage'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='build', | ||
name='_config_json', | ||
field=models.JSONField(default=dict, verbose_name='Configuration used in the build'), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from django.db import migrations, models | ||
from django.db.models import F | ||
from django.db.models.functions import Cast | ||
|
||
|
||
def forwards_func(apps, schema_editor): | ||
Build = apps.get_model('builds', 'Build') | ||
( | ||
Build.objects | ||
.annotate(_config_in_json=Cast('_config', output_field=models.JSONField())) | ||
# Copy `_config` JSONField (text) into `_config_json` (jsonb) | ||
.update(_config_json=F('_config_in_json')) | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('builds', '0038_add_new_jsonfields'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(forwards_func) | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -648,6 +648,7 @@ class Build(models.Model): | |
blank=True, | ||
) | ||
_config = JSONField(_('Configuration used in the build'), default=dict) | ||
_config_json = models.JSONField(_('Configuration used in the build'), default=dict) | ||
ericholscher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
length = models.IntegerField(_('Build Length'), null=True, blank=True) | ||
|
||
|
@@ -716,6 +717,10 @@ def config(self): | |
Build object (it could be stored in this object or one of the previous | ||
ones). | ||
""" | ||
# TODO: now that we are using a proper JSONField here, we could | ||
# probably change this field to be a ForeignKey to avoid repeating the | ||
# config file over and over again and re-use them to save db data as | ||
# well | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, an FK seems reasonable but would be another field right? I agree that our current implementation is just a hacky FK :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I'm thinking about having something like
... and later in the code, I'm expecting to do:
and relying on Postgres to do the work about deciding if it's the same or a new one properly 😄 |
||
if self.CONFIG_KEY in self._config: | ||
return ( | ||
Build.objects | ||
|
@@ -759,6 +764,11 @@ def save(self, *args, **kwargs): # noqa | |
self.version_name = self.version.verbose_name | ||
self.version_slug = self.version.slug | ||
self.version_type = self.version.type | ||
|
||
# TODO: delete copying config after deploy | ||
# Copy `_config` into the new `_config_json` JSONField | ||
self._config_json = self._config | ||
|
||
super().save(*args, **kwargs) | ||
self._config_changed = False | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Generated by Django 3.2.11 on 2022-01-31 11:47 | ||
|
||
from django.db import migrations, models | ||
from django.db.models import F | ||
from django.db.models.functions import Cast | ||
humitos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('integrations', '0007_update-provider-data'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='httpexchange', | ||
name='request_headers_json', | ||
field=models.JSONField(default=None, null=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'), | ||
), | ||
migrations.AddField( | ||
model_name='integration', | ||
name='provider_data_json', | ||
field=models.JSONField(default=dict, verbose_name='Provider data'), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.db import migrations, models | ||
from django.db.models import F | ||
from django.db.models.functions import Cast | ||
|
||
|
||
def forwards_func(apps, schema_editor): | ||
HttpExchange = apps.get_model('integrations', 'HttpExchange') | ||
( | ||
HttpExchange.objects | ||
.annotate( | ||
request_headers_in_json=Cast('request_headers', output_field=models.JSONField()), | ||
response_headers_in_json=Cast('response_headers', output_field=models.JSONField()), | ||
) | ||
.update( | ||
request_headers_json=F('request_headers_in_json'), | ||
response_headers_json=F('response_headers_in_json'), | ||
) | ||
) | ||
|
||
|
||
Integration = apps.get_model('integrations', 'Integration') | ||
( | ||
Integration.objects | ||
.annotate( | ||
provider_data_in_json=Cast('provider_data', output_field=models.JSONField()), | ||
) | ||
.update( | ||
provider_data_json=F('provider_data_in_json'), | ||
) | ||
) | ||
|
||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('integrations', '0008_add_new_jsonfields'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(forwards_func) | ||
] |
Uh oh!
There was an error while loading. Please reload this page.