Skip to content

Commit 26a6cd5

Browse files
Clean up expression index handling (#50)
This PR fixes up the definitions around expression indexes, which are not supported by DSQL. It defines the `supports_expression_indexes` feature flag to indicate this, and adds tests to cover the wrapper-level behavior of the `add_index` and `remove_index` overrides in the `schema.py` file. By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
1 parent 5eed6c2 commit 26a6cd5

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

aurora_dsql_django/features.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class DatabaseFeatures(features.DatabaseFeatures):
5757
# Can the database rename an index?
5858
can_rename_index = True
5959

60+
supports_expression_indexes = False
61+
6062
# Does the database use savepoints for nested transactions?
6163
uses_savepoints = False
6264

aurora_dsql_django/tests/unit/test_features.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ def test_can_release_savepoints(self):
4848
def test_can_rename_index(self):
4949
self.assertTrue(self.features.can_rename_index)
5050

51+
def test_supports_expression_indexes(self):
52+
self.assertFalse(self.features.supports_expression_indexes)
53+
5154
def test_inheritance(self):
5255
from django.db.backends.postgresql.features import DatabaseFeatures as PostgreSQLDatabaseFeatures
5356
self.assertIsInstance(self.features, PostgreSQLDatabaseFeatures)

aurora_dsql_django/tests/unit/test_wrapper.py

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import django
44
from django.conf import settings
55
from django.db import models
6-
from django.db.models import CheckConstraint, Q
6+
from django.db.models import CheckConstraint, Q, Index
7+
from django.db.models.functions import Upper
78
from aurora_dsql_django.base import DatabaseWrapper
89
from aurora_dsql_django.features import DatabaseFeatures
910
from aurora_dsql_django.schema import DatabaseSchemaEditor
@@ -47,7 +48,8 @@ def mock_execute(sql, params=None):
4748
quote_patch = patch.object(self.schema_editor, 'quote_value', side_effect=simple_quote_value)
4849

4950
with execute_patch, quote_patch:
50-
operation_func()
51+
with self.schema_editor:
52+
operation_func()
5153

5254
all_sql = [str(sql) for sql, _ in executed_sql]
5355
if hasattr(self.schema_editor, 'deferred_sql'):
@@ -70,8 +72,7 @@ class Meta:
7072
app_label = 'test_app'
7173

7274
def operation():
73-
with self.schema_editor:
74-
self.schema_editor.create_model(ChildModel)
75+
self.schema_editor.create_model(ChildModel)
7576

7677
self._assert_sql_not_generated(
7778
operation,
@@ -92,8 +93,7 @@ class Meta:
9293
]
9394

9495
def operation():
95-
with self.schema_editor:
96-
self.schema_editor.create_model(CheckConstraintModel)
96+
self.schema_editor.create_model(CheckConstraintModel)
9797

9898
self._assert_sql_not_generated(
9999
operation,
@@ -142,5 +142,46 @@ def operation():
142142
)
143143

144144

145+
def test_add_index_expression_ignored(self):
146+
"""Ensure add_index operations ignore expression indexes when the feature is disabled"""
147+
148+
class AddIndexModel(models.Model):
149+
name = models.CharField(max_length=100)
150+
151+
class Meta:
152+
app_label = 'test_app'
153+
154+
expression_index = Index(Upper('name'), name='upper_name_idx')
155+
156+
def operation():
157+
self.schema_editor.add_index(AddIndexModel, expression_index)
158+
159+
self._assert_sql_not_generated(
160+
operation,
161+
['CREATE INDEX'],
162+
"Should not generate index creation SQL for expression indexes"
163+
)
164+
165+
def test_remove_index_expression_ignored(self):
166+
"""Ensure remove_index operations ignore expression indexes when the feature is disabled"""
167+
168+
class RemoveIndexModel(models.Model):
169+
name = models.CharField(max_length=100)
170+
171+
class Meta:
172+
app_label = 'test_app'
173+
174+
expression_index = Index(Upper('name'), name='upper_name_idx')
175+
176+
def operation():
177+
self.schema_editor.remove_index(RemoveIndexModel, expression_index)
178+
179+
self._assert_sql_not_generated(
180+
operation,
181+
['DROP INDEX'],
182+
"Should not generate index removal SQL for expression indexes"
183+
)
184+
185+
145186
if __name__ == '__main__':
146187
unittest.main()

0 commit comments

Comments
 (0)