-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a class to remove any indexes for a column
- Loading branch information
1 parent
6512cf7
commit cba04a4
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.db import connection, migrations | ||
|
||
|
||
class RemoveIndexForField(migrations.RunPython): | ||
def __init__(self, app_name, model_name, field_name) -> None: | ||
self.app_name = app_name | ||
self.model_name = model_name | ||
self.field_name = field_name | ||
super().__init__(self.remove_index, self.reverse_remove_index) | ||
|
||
def remove_index(self, app, schema_editor): | ||
model = app.get_model(self.app_name, self.model_name) | ||
field = model._meta.get_field(self.field_name) | ||
|
||
with connection.cursor() as cursor: | ||
constraints = connection.introspection.get_constraints(cursor, model._meta.db_table) | ||
for constraint_name, constraint_info in constraints.items(): | ||
if constraint_info["index"] and any(field.column.lower() == col.lower() for col in constraint_info["columns"]): | ||
cursor.execute(f"DROP INDEX {constraint_name} ON {model._meta.db_table}") | ||
|
||
def reverse_remove_index(self, app, schema_editor): | ||
pass |