#36165: Postgres schema editor throws away custom index deletion SQL
--------------------------------+--------------------------------------
Reporter: Daniel Samuels | Type: Bug
Status: new | Component: CSRF
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
If I wanted to create a Postgres database index type that used custom SQL
for both creation and deletion, the custom SQL would only apply in the
create case.
Here's some example code:
{{{
from django.db.models import Index
class UniqueIndex(Index):
# Adjusted version of DatabaseSchemaEditor.sql_create_index that works
with materialized views
sql_create_index = "CREATE UNIQUE INDEX %(name)s ON %(table)s%(using)s
(%(columns)s);"
sql_delete_index = "DROP INDEX IF EXISTS %(name)s;"
def create_sql(self, model, schema_editor, using="", **kwargs):
kwargs.setdefault("sql", self.sql_create_index)
return super().create_sql(model, schema_editor, using, **kwargs)
def remove_sql(self, model, schema_editor, **kwargs):
kwargs.setdefault("sql", self.sql_delete_index)
return super().remove_sql(model, schema_editor, **kwargs)
}}}
The call to `create_sql` would do the right thing, and use the SQL
supplied - as seen in the underlying code:
{{{
sql = sql or (
self.sql_create_index
if not concurrently
else self.sql_create_index_concurrently
)
}}}
https://github.com/django/django/blob/5f30fd2358fd60a514bdba31594bfc8122f30167/django/db/backends/postgresql/schema.py#L349-L353
However, the same cannot be said for the delete code, which simply ignores
the provided SQL value:
{{{
sql = (
self.sql_delete_index_concurrently
if concurrently
else self.sql_delete_index
)
}}}
https://github.com/django/django/blob/5f30fd2358fd60a514bdba31594bfc8122f30167/django/db/backends/postgresql/schema.py#L324-L330
I believe this constitutes a bug and should have an obvious and simple
fix.
--
Ticket URL: <https://code.djangoproject.com/ticket/36165>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/01070194ccebe582-54272fc8-8bd3-411a-b6ee-ed098dff20b2-000000%40eu-central-1.amazonses.com.