1
0
mirror of https://github.com/django/django.git synced 2025-03-06 15:32:33 +00:00

Used a database feature to prevent the jsonb test model from being migrated.

Thanks Tim for the review.
This commit is contained in:
Simon Charette 2016-09-10 19:20:16 -04:00
parent e07b18252b
commit 32c0d823e5
4 changed files with 19 additions and 42 deletions

View File

@ -36,3 +36,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
@cached_property @cached_property
def has_select_for_update_skip_locked(self): def has_select_for_update_skip_locked(self):
return self.connection.pg_version >= 90500 return self.connection.pg_version >= 90500
@cached_property
def has_jsonb_datatype(self):
return self.connection.pg_version >= 90400

View File

@ -216,9 +216,6 @@ class Migration(migrations.Migration):
}, },
bases=(models.Model,), bases=(models.Model,),
), ),
]
pg_94_operations = [
migrations.CreateModel( migrations.CreateModel(
name='JSONModel', name='JSONModel',
fields=[ fields=[
@ -227,17 +224,8 @@ class Migration(migrations.Migration):
('field_custom', JSONField(null=True, blank=True, encoder=DjangoJSONEncoder)), ('field_custom', JSONField(null=True, blank=True, encoder=DjangoJSONEncoder)),
], ],
options={ options={
'required_db_features': {'has_jsonb_datatype'},
}, },
bases=(models.Model,), bases=(models.Model,),
), ),
] ]
def apply(self, project_state, schema_editor, collect_sql=False):
try:
PG_VERSION = schema_editor.connection.pg_version
except AttributeError:
pass # We are probably not on PostgreSQL
else:
if PG_VERSION >= 90400:
self.operations = self.operations + self.pg_94_operations
return super(Migration, self).apply(project_state, schema_editor, collect_sql)

View File

@ -1,5 +1,5 @@
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.db import connection, models from django.db import models
from .fields import ( from .fields import (
ArrayField, BigIntegerRangeField, DateRangeField, DateTimeRangeField, ArrayField, BigIntegerRangeField, DateRangeField, DateTimeRangeField,
@ -129,15 +129,12 @@ class RangeLookupsModel(PostgreSQLModel):
date = models.DateField(blank=True, null=True) date = models.DateField(blank=True, null=True)
# Only create this model for postgres >= 9.4 class JSONModel(models.Model):
if connection.vendor == 'postgresql' and connection.pg_version >= 90400: field = JSONField(blank=True, null=True)
class JSONModel(models.Model): field_custom = JSONField(blank=True, null=True, encoder=DjangoJSONEncoder)
field = JSONField(blank=True, null=True)
field_custom = JSONField(blank=True, null=True, encoder=DjangoJSONEncoder) class Meta:
else: required_db_features = ['has_jsonb_datatype']
# create an object with this name so we don't have failing imports
class JSONModel(object):
pass
class ArrayFieldSubclass(ArrayField): class ArrayFieldSubclass(ArrayField):

View File

@ -1,15 +1,13 @@
from __future__ import unicode_literals from __future__ import unicode_literals
import datetime import datetime
import unittest
import uuid import uuid
from decimal import Decimal from decimal import Decimal
from django.core import exceptions, serializers from django.core import exceptions, serializers
from django.core.serializers.json import DjangoJSONEncoder from django.core.serializers.json import DjangoJSONEncoder
from django.db import connection
from django.forms import CharField, Form, widgets from django.forms import CharField, Form, widgets
from django.test import TestCase from django.test import skipUnlessDBFeature
from django.utils.html import escape from django.utils.html import escape
from . import PostgreSQLTestCase from . import PostgreSQLTestCase
@ -22,18 +20,8 @@ except ImportError:
pass pass
def skipUnlessPG94(test): @skipUnlessDBFeature('has_jsonb_datatype')
try: class TestSaveLoad(PostgreSQLTestCase):
PG_VERSION = connection.pg_version
except AttributeError:
PG_VERSION = 0
if PG_VERSION < 90400:
return unittest.skip('PostgreSQL >= 9.4 required')(test)
return test
@skipUnlessPG94
class TestSaveLoad(TestCase):
def test_null(self): def test_null(self):
instance = JSONModel() instance = JSONModel()
instance.save() instance.save()
@ -106,8 +94,8 @@ class TestSaveLoad(TestCase):
self.assertEqual(loaded.field_custom, obj_after) self.assertEqual(loaded.field_custom, obj_after)
@skipUnlessPG94 @skipUnlessDBFeature('has_jsonb_datatype')
class TestQuerying(TestCase): class TestQuerying(PostgreSQLTestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.objs = [ cls.objs = [
@ -250,8 +238,8 @@ class TestQuerying(TestCase):
) )
@skipUnlessPG94 @skipUnlessDBFeature('has_jsonb_datatype')
class TestSerialization(TestCase): class TestSerialization(PostgreSQLTestCase):
test_data = ( test_data = (
'[{"fields": {"field": {"a": "b", "c": null}, "field_custom": null}, ' '[{"fields": {"field": {"a": "b", "c": null}, "field_custom": null}, '
'"model": "postgres_tests.jsonmodel", "pk": null}]' '"model": "postgres_tests.jsonmodel", "pk": null}]'