-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refs #27064 -- Added RenameIndex migration operation. #15677
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@David-Wobrock Thanks 👍 I left comments.
Thanks a lot for the review @felixxm 🙏
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@David-Wobrock Thanks for updates 👍 I rebased and pushed some edits.
django/db/backends/oracle/schema.py
Outdated
@@ -27,6 +27,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): | |||
) | |||
sql_delete_table = "DROP TABLE %(table)s CASCADE CONSTRAINTS" | |||
sql_create_index = "CREATE INDEX %(name)s ON %(table)s (%(columns)s)%(extra)s" | |||
sql_renamed_index = "ALTER INDEX %(old_name)s RENAME TO %(new_name)s" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo:
sql_renamed_index = "ALTER INDEX %(old_name)s RENAME TO %(new_name)s" | |
sql_rename_index = "ALTER INDEX %(old_name)s RENAME TO %(new_name)s" |
) | ||
if len(matching_index_name) != 1: | ||
raise ValueError( | ||
f"Found wrong number ({len(matching_index_name)}) of indexes for " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f-strings should not contain function calls. This guideline is from Python coding style.
if self.old_fields: | ||
from_model = from_state.apps.get_model(app_label, self.model_name) | ||
matching_index_name = schema_editor._constraint_names( | ||
from_model, column_names=self.old_fields, index=True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Users would use field names not column names 🤔 we must take this into account, e.g.
columns = [from_model._meta.get_field(field).column for field in self.old_fields]
tests/migrations/test_operations.py
Outdated
def test_rename_index_reduction(self): | ||
operation_one = migrations.RenameIndex( | ||
"Pony", new_name="mid_name", old_fields=("weight", "pink") | ||
) | ||
operation_two = migrations.RenameIndex( | ||
"Pony", new_name="new_name", old_name="mid_name" | ||
) | ||
result_operations = operation_one.reduce(operation_two, []) | ||
self.assertEqual(len(result_operations), 1) | ||
self.assertEqual( | ||
result_operations[0].deconstruct(), | ||
migrations.RenameIndex( | ||
"Pony", | ||
new_name="new_name", | ||
old_fields=("weight", "pink"), | ||
).deconstruct(), | ||
) | ||
|
||
operation_one = migrations.RenameIndex( | ||
"Pony", new_name="mid_name", old_name="old_name" | ||
) | ||
operation_two = migrations.RenameIndex( | ||
"Pony", new_name="new_name", old_name="mid_name" | ||
) | ||
result_operations = operation_one.reduce(operation_two, []) | ||
self.assertEqual(len(result_operations), 1) | ||
self.assertEqual( | ||
result_operations[0].deconstruct(), | ||
migrations.RenameIndex( | ||
"Pony", new_name="new_name", old_name="old_name" | ||
).deconstruct(), | ||
) | ||
|
||
operation_one = migrations.RenameIndex( | ||
"Pony", new_name="mid_name", old_name="old_name" | ||
) | ||
operation_two = migrations.RenameIndex( | ||
"Pony", new_name="new_name", old_fields=("weight", "pink") | ||
) | ||
result_operations = operation_one.reduce(operation_two, []) | ||
self.assertIs(result_operations, False) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to test_optimizer
.
Awesome, thanks a lot for the edits @felixxm 🚀 |
Adds for https://code.djangoproject.com/ticket/27064 the
RenameIndex
operationSplit from #15651. Here we implement the operation, in #15651 we use it in the autodetector.
This PR:
RenameIndex
operation, that nearly always has 2 behaviours for each method: 1/ renaming with anold_name
or renaming an unnamed index withold_fields
indexes
or bothindexes
+index_together
supports_index_renaming
feature flag on DB backends, to know if one can use aRENAME INDEX
database statement or needs to drop/create the index