Skip to content

Commit 32c0d82

Browse files
committed
Used a database feature to prevent the jsonb test model from being migrated.
Thanks Tim for the review.
1 parent e07b182 commit 32c0d82

File tree

4 files changed

+19
-42
lines changed

4 files changed

+19
-42
lines changed

django/db/backends/postgresql/features.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
3636
@cached_property
3737
def has_select_for_update_skip_locked(self):
3838
return self.connection.pg_version >= 90500
39+
40+
@cached_property
41+
def has_jsonb_datatype(self):
42+
return self.connection.pg_version >= 90400

tests/postgres_tests/migrations/0002_create_test_models.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,6 @@ class Migration(migrations.Migration):
216216
},
217217
bases=(models.Model,),
218218
),
219-
]
220-
221-
pg_94_operations = [
222219
migrations.CreateModel(
223220
name='JSONModel',
224221
fields=[
@@ -227,17 +224,8 @@ class Migration(migrations.Migration):
227224
('field_custom', JSONField(null=True, blank=True, encoder=DjangoJSONEncoder)),
228225
],
229226
options={
227+
'required_db_features': {'has_jsonb_datatype'},
230228
},
231229
bases=(models.Model,),
232230
),
233231
]
234-
235-
def apply(self, project_state, schema_editor, collect_sql=False):
236-
try:
237-
PG_VERSION = schema_editor.connection.pg_version
238-
except AttributeError:
239-
pass # We are probably not on PostgreSQL
240-
else:
241-
if PG_VERSION >= 90400:
242-
self.operations = self.operations + self.pg_94_operations
243-
return super(Migration, self).apply(project_state, schema_editor, collect_sql)

tests/postgres_tests/models.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.core.serializers.json import DjangoJSONEncoder
2-
from django.db import connection, models
2+
from django.db import models
33

44
from .fields import (
55
ArrayField, BigIntegerRangeField, DateRangeField, DateTimeRangeField,
@@ -129,15 +129,12 @@ class RangeLookupsModel(PostgreSQLModel):
129129
date = models.DateField(blank=True, null=True)
130130

131131

132-
# Only create this model for postgres >= 9.4
133-
if connection.vendor == 'postgresql' and connection.pg_version >= 90400:
134-
class JSONModel(models.Model):
135-
field = JSONField(blank=True, null=True)
136-
field_custom = JSONField(blank=True, null=True, encoder=DjangoJSONEncoder)
137-
else:
138-
# create an object with this name so we don't have failing imports
139-
class JSONModel(object):
140-
pass
132+
class JSONModel(models.Model):
133+
field = JSONField(blank=True, null=True)
134+
field_custom = JSONField(blank=True, null=True, encoder=DjangoJSONEncoder)
135+
136+
class Meta:
137+
required_db_features = ['has_jsonb_datatype']
141138

142139

143140
class ArrayFieldSubclass(ArrayField):

tests/postgres_tests/test_json.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
from __future__ import unicode_literals
22

33
import datetime
4-
import unittest
54
import uuid
65
from decimal import Decimal
76

87
from django.core import exceptions, serializers
98
from django.core.serializers.json import DjangoJSONEncoder
10-
from django.db import connection
119
from django.forms import CharField, Form, widgets
12-
from django.test import TestCase
10+
from django.test import skipUnlessDBFeature
1311
from django.utils.html import escape
1412

1513
from . import PostgreSQLTestCase
@@ -22,18 +20,8 @@
2220
pass
2321

2422

25-
def skipUnlessPG94(test):
26-
try:
27-
PG_VERSION = connection.pg_version
28-
except AttributeError:
29-
PG_VERSION = 0
30-
if PG_VERSION < 90400:
31-
return unittest.skip('PostgreSQL >= 9.4 required')(test)
32-
return test
33-
34-
35-
@skipUnlessPG94
36-
class TestSaveLoad(TestCase):
23+
@skipUnlessDBFeature('has_jsonb_datatype')
24+
class TestSaveLoad(PostgreSQLTestCase):
3725
def test_null(self):
3826
instance = JSONModel()
3927
instance.save()
@@ -106,8 +94,8 @@ def test_custom_encoding(self):
10694
self.assertEqual(loaded.field_custom, obj_after)
10795

10896

109-
@skipUnlessPG94
110-
class TestQuerying(TestCase):
97+
@skipUnlessDBFeature('has_jsonb_datatype')
98+
class TestQuerying(PostgreSQLTestCase):
11199
@classmethod
112100
def setUpTestData(cls):
113101
cls.objs = [
@@ -250,8 +238,8 @@ def test_usage_in_subquery(self):
250238
)
251239

252240

253-
@skipUnlessPG94
254-
class TestSerialization(TestCase):
241+
@skipUnlessDBFeature('has_jsonb_datatype')
242+
class TestSerialization(PostgreSQLTestCase):
255243
test_data = (
256244
'[{"fields": {"field": {"a": "b", "c": null}, "field_custom": null}, '
257245
'"model": "postgres_tests.jsonmodel", "pk": null}]'

0 commit comments

Comments
 (0)