From 4c36e9e492b5c127d839ebf280fdf5556af8e824 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Tue, 5 Dec 2017 09:48:04 +0000 Subject: [PATCH] Fixed #28887 -- Added SpGistIndex to django.contrib.postgres. --- django/contrib/postgres/indexes.py | 22 +++++++++++++- docs/ref/contrib/postgres/indexes.txt | 15 +++++++++ docs/releases/2.2.txt | 5 +-- tests/postgres_tests/test_indexes.py | 44 ++++++++++++++++++++++++++- 4 files changed, 82 insertions(+), 4 deletions(-) diff --git a/django/contrib/postgres/indexes.py b/django/contrib/postgres/indexes.py index 3b77b97f3e..43c5588d58 100644 --- a/django/contrib/postgres/indexes.py +++ b/django/contrib/postgres/indexes.py @@ -1,7 +1,7 @@ from django.db.models import Index from django.utils.functional import cached_property -__all__ = ['BrinIndex', 'GinIndex', 'GistIndex', 'HashIndex'] +__all__ = ['BrinIndex', 'GinIndex', 'GistIndex', 'HashIndex', 'SpGistIndex'] class PostgresIndex(Index): @@ -118,3 +118,23 @@ class HashIndex(PostgresIndex): if self.fillfactor is not None: with_params.append('fillfactor = %d' % self.fillfactor) return with_params + + +class SpGistIndex(PostgresIndex): + suffix = 'spgist' + + def __init__(self, *, fillfactor=None, **kwargs): + self.fillfactor = fillfactor + super().__init__(**kwargs) + + def deconstruct(self): + path, args, kwargs = super().deconstruct() + if self.fillfactor is not None: + kwargs['fillfactor'] = self.fillfactor + return path, args, kwargs + + def get_with_params(self): + with_params = [] + if self.fillfactor is not None: + with_params.append('fillfactor = %d' % self.fillfactor) + return with_params diff --git a/docs/ref/contrib/postgres/indexes.txt b/docs/ref/contrib/postgres/indexes.txt index 2d966553c3..67a8e1b98b 100644 --- a/docs/ref/contrib/postgres/indexes.txt +++ b/docs/ref/contrib/postgres/indexes.txt @@ -91,3 +91,18 @@ available from the ``django.contrib.postgres.indexes`` module. they suffer from a number of data integrity issues in older versions. .. _fillfactor: https://www.postgresql.org/docs/current/static/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS + +``SpGistIndex`` +=============== + +.. class:: SpGistIndex(fillfactor=None, **options) + + .. versionadded:: 2.2 + + Creates an `SP-GiST index + `_. + + Provide an integer value from 10 to 100 to the fillfactor_ parameter to + tune how packed the index pages will be. PostgreSQL's default is 90. + + .. _fillfactor: https://www.postgresql.org/docs/current/static/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS diff --git a/docs/releases/2.2.txt b/docs/releases/2.2.txt index cf3e9bff80..82018cba18 100644 --- a/docs/releases/2.2.txt +++ b/docs/releases/2.2.txt @@ -82,8 +82,9 @@ Minor features :class:`~django.contrib.postgres.aggregates.StringAgg` determines the ordering of the aggregated elements. -* The new :class:`~django.contrib.postgres.indexes.HashIndex` class - allows creating ``hash`` indexes in the database. +* The new :class:`~django.contrib.postgres.indexes.HashIndex` and + :class:`~django.contrib.postgres.indexes.SpGistIndex` classes allow + creating ``hash`` and ``SP-GiST`` indexes in the database. :mod:`django.contrib.redirects` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/postgres_tests/test_indexes.py b/tests/postgres_tests/test_indexes.py index dcb285cc1c..b07716e40d 100644 --- a/tests/postgres_tests/test_indexes.py +++ b/tests/postgres_tests/test_indexes.py @@ -1,5 +1,5 @@ from django.contrib.postgres.indexes import ( - BrinIndex, GinIndex, GistIndex, HashIndex, + BrinIndex, GinIndex, GistIndex, HashIndex, SpGistIndex, ) from django.db import connection from django.test import skipUnlessDBFeature @@ -99,6 +99,20 @@ class HashIndexTests(IndexTestMixin, PostgreSQLTestCase): self.assertEqual(kwargs, {'fields': ['title'], 'name': 'test_title_hash', 'fillfactor': 80}) +class SpGistIndexTests(IndexTestMixin, PostgreSQLTestCase): + index_class = SpGistIndex + + def test_suffix(self): + self.assertEqual(SpGistIndex.suffix, 'spgist') + + def test_deconstruction(self): + index = SpGistIndex(fields=['title'], name='test_title_spgist', fillfactor=80) + path, args, kwargs = index.deconstruct() + self.assertEqual(path, 'django.contrib.postgres.indexes.SpGistIndex') + self.assertEqual(args, ()) + self.assertEqual(kwargs, {'fields': ['title'], 'name': 'test_title_spgist', 'fillfactor': 80}) + + class SchemaTests(PostgreSQLTestCase): def get_constraints(self, table): @@ -217,3 +231,31 @@ class SchemaTests(PostgreSQLTestCase): with connection.schema_editor() as editor: editor.remove_index(CharFieldModel, index) self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table)) + + def test_spgist_index(self): + # Ensure the table is there and doesn't have an index. + self.assertNotIn('field', self.get_constraints(CharFieldModel._meta.db_table)) + # Add the index. + index_name = 'char_field_model_field_spgist' + index = SpGistIndex(fields=['field'], name=index_name) + with connection.schema_editor() as editor: + editor.add_index(CharFieldModel, index) + constraints = self.get_constraints(CharFieldModel._meta.db_table) + # The index was added. + self.assertEqual(constraints[index_name]['type'], SpGistIndex.suffix) + # Drop the index. + with connection.schema_editor() as editor: + editor.remove_index(CharFieldModel, index) + self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table)) + + def test_spgist_parameters(self): + index_name = 'integer_array_spgist_fillfactor' + index = SpGistIndex(fields=['field'], name=index_name, fillfactor=80) + with connection.schema_editor() as editor: + editor.add_index(CharFieldModel, index) + constraints = self.get_constraints(CharFieldModel._meta.db_table) + self.assertEqual(constraints[index_name]['type'], SpGistIndex.suffix) + self.assertEqual(constraints[index_name]['options'], ['fillfactor=80']) + with connection.schema_editor() as editor: + editor.remove_index(CharFieldModel, index) + self.assertNotIn(index_name, self.get_constraints(CharFieldModel._meta.db_table))