Skip to content

Commit

Permalink
Fixed #35075 -- Added deduplicate_items parameter to BTreeIndex.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngnpope authored and felixxm committed Jan 2, 2024
1 parent f412add commit 45f778e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
9 changes: 8 additions & 1 deletion django/contrib/postgres/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,27 @@ def get_with_params(self):
class BTreeIndex(PostgresIndex):
suffix = "btree"

def __init__(self, *expressions, fillfactor=None, **kwargs):
def __init__(self, *expressions, fillfactor=None, deduplicate_items=None, **kwargs):
self.fillfactor = fillfactor
self.deduplicate_items = deduplicate_items
super().__init__(*expressions, **kwargs)

def deconstruct(self):
path, args, kwargs = super().deconstruct()
if self.fillfactor is not None:
kwargs["fillfactor"] = self.fillfactor
if self.deduplicate_items is not None:
kwargs["deduplicate_items"] = self.deduplicate_items
return path, args, kwargs

def get_with_params(self):
with_params = []
if self.fillfactor is not None:
with_params.append("fillfactor = %d" % self.fillfactor)
if self.deduplicate_items is not None:
with_params.append(
"deduplicate_items = %s" % ("on" if self.deduplicate_items else "off")
)
return with_params


Expand Down
11 changes: 10 additions & 1 deletion docs/ref/contrib/postgres/indexes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,23 @@ available from the ``django.contrib.postgres.indexes`` module.
``BTreeIndex``
==============

.. class:: BTreeIndex(*expressions, fillfactor=None, **options)
.. class:: BTreeIndex(*expressions, fillfactor=None, deduplicate_items=None, **options)

Creates a B-Tree 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.

Provide a boolean value to the deduplicate_items_ parameter to control
whether deduplication is enabled. PostgreSQL enables deduplication by
default.

.. versionchanged:: 5.1

The ``deduplicate_items`` parameter was added.

.. _fillfactor: https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-STORAGE-PARAMETERS
.. _deduplicate_items: https://www.postgresql.org/docs/current/btree-implementation.html#BTREE-DEDUPLICATION

``GinIndex``
============
Expand Down
3 changes: 2 additions & 1 deletion docs/releases/5.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ Minor features
:mod:`django.contrib.postgres`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* ...
* :class:`~django.contrib.postgres.indexes.BTreeIndex` now supports the
``deduplicate_items`` parameter.

:mod:`django.contrib.redirects`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
1 change: 1 addition & 0 deletions docs/spelling_wordlist
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ datetimes
declaratively
decrementing
deduplicates
deduplication
deepcopy
deferrable
deprecations
Expand Down
32 changes: 27 additions & 5 deletions tests/postgres_tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,29 @@ def test_suffix(self):
self.assertEqual(BTreeIndex.suffix, "btree")

def test_deconstruction(self):
index = BTreeIndex(fields=["title"], name="test_title_btree", fillfactor=80)
index = BTreeIndex(fields=["title"], name="test_title_btree")
path, args, kwargs = index.deconstruct()
self.assertEqual(path, "django.contrib.postgres.indexes.BTreeIndex")
self.assertEqual(args, ())
self.assertEqual(kwargs, {"fields": ["title"], "name": "test_title_btree"})

index = BTreeIndex(
fields=["title"],
name="test_title_btree",
fillfactor=80,
deduplicate_items=False,
)
path, args, kwargs = index.deconstruct()
self.assertEqual(path, "django.contrib.postgres.indexes.BTreeIndex")
self.assertEqual(args, ())
self.assertEqual(
kwargs, {"fields": ["title"], "name": "test_title_btree", "fillfactor": 80}
kwargs,
{
"fields": ["title"],
"name": "test_title_btree",
"fillfactor": 80,
"deduplicate_items": False,
},
)


Expand Down Expand Up @@ -455,13 +472,18 @@ def test_btree_index(self):
)

def test_btree_parameters(self):
index_name = "integer_array_btree_fillfactor"
index = BTreeIndex(fields=["field"], name=index_name, fillfactor=80)
index_name = "integer_array_btree_parameters"
index = BTreeIndex(
fields=["field"], name=index_name, fillfactor=80, deduplicate_items=False
)
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"], BTreeIndex.suffix)
self.assertEqual(constraints[index_name]["options"], ["fillfactor=80"])
self.assertEqual(
constraints[index_name]["options"],
["fillfactor=80", "deduplicate_items=off"],
)
with connection.schema_editor() as editor:
editor.remove_index(CharFieldModel, index)
self.assertNotIn(
Expand Down

0 comments on commit 45f778e

Please sign in to comment.