Skip to content
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

Fixed #26808 -- Introduce Meta.indexes #6857

Closed
wants to merge 1 commit into from

Conversation

akki
Copy link
Contributor

@akki akki commented Jun 30, 2016

Ticket #26808.

Other PR that needs to be merged before this - !6871.

---------------

.. method:: BaseDatabaseSchemaEditor.add_index(model, index)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. versionadded:: 1.11

@akki akki force-pushed the gsoc-indexes-2 branch 4 times, most recently from a466e32 to 4558449 Compare July 3, 2016 11:32
@akki akki changed the title [WIP] Fixed #26808 -- Introduce Meta.indexes Fixed #26808 -- Introduce Meta.indexes Jul 3, 2016
@@ -890,6 +890,9 @@ def _model_indexes_sql(self, model):
for field_names in model._meta.index_together:
fields = [model._meta.get_field(field) for field in field_names]
output.append(self._create_index_sql(model, fields, suffix="_idx"))

for index in model._meta.indexes:
output.append(index.create_sql(self))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebase and add model argument.

@akki
Copy link
Contributor Author

akki commented Jul 9, 2016

@timgraham @MarkusH Please let me know if anything else is required.

# Sanity-check that indexes have their names set.
for index in self.options['indexes']:
if not index._name:
raise ValueError("Indexes passed to ModelState require a name attribute, %r doesn't have one." % index)
Copy link
Member

@timgraham timgraham Jul 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use:

raise ValueError(
    "Indexes passed to ModelState require a name attribute, "
    "%r doesn't have one." % index
)

While we allow longer lines, we typically break up long strings like this.

@timgraham
Copy link
Member

Is adding an index using AddIndex if it's not in Model.indexes (as suggested in migration-operations.txt) valid? Is there a risk Django might drop that index in some future alter operation since it doesn't know about it in models?

@akki
Copy link
Contributor Author

akki commented Jul 14, 2016

Ahh, right! I think we can remove that from the docs, none of the other operations does that either. I have made this change in the last commit, if it seems right.

I also wanted to know if (and how) I should mention this in the release docs.

Thanks


If you're writing your own migration to add an index, you must assign a
``name`` to the ``index`` as done above.
It only accepts an `Index` instance for which the `name` attribute is defined.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this is important to mention since most users shouldn't encounter it and a helpful message is raised in that case. Otherwise, you need to use double backticks around "Index" and "name" to have an effect on the rendered docss.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to remove it then.

@timgraham
Copy link
Member

I think we can wait until the end of this project before writing release notes as things may evolve.

@@ -176,6 +176,9 @@ def _detect_changes(self, convert_apps=None, graph=None):
self.generate_altered_options()
self.generate_altered_managers()

# Create the altered indexes and store them in self.altered_indexes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add period

@@ -353,6 +353,13 @@ def __init__(self, app_label, name, fields, options=None, bases=None, managers=N
'ModelState.fields cannot refer to a model class - "%s.through" does. '
'Use a string reference instead.' % name
)
# Sanity-check that indexes have their names set.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

names -> name

@@ -521,6 +529,9 @@ def generate_created_models(self):
related_fields[field.name] = field
if getattr(field.remote_field, "through", None) and not field.remote_field.through._meta.auto_created:
related_fields[field.name] = field
# Are there any indexes to defer?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't really make sense. Could you explain why you're doing what you're doing?

Copy link
Contributor Author

@akki akki Jul 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have indexes defined in Meta.indexes while creating a new model we also create AddIndex operations in the migration. Similar to what has been done for AlterUniqu/IndexTogether and order_with_respect_to here.

@@ -175,13 +176,20 @@ def _detect_changes(self, convert_apps=None, graph=None):
self.generate_altered_options()
self.generate_altered_managers()

# Create the altered indexes and store them in self.altered_indexes.
# This avoids the same computation in generate_removed_indexes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generate_removed_indexes() and generate_added_indexes()
(add parentheses)

@timgraham
Copy link
Member

I merged the first commit in b92c6b7. In the end, unless you feel differently, I think we can squash the others when merging. Splitting them out definitely made review easier though.

I think this is good to go, @MarkusH are you happy?

@akki
Copy link
Contributor Author

akki commented Aug 5, 2016

I share the same rationale. Will squash them in my next push.

@akki akki force-pushed the gsoc-indexes-2 branch 2 times, most recently from 74356ed to adb03e4 Compare August 5, 2016 08:25
Added support for class based indexes in Meta class

Other changes
  * Added the index name to it's deconstruction
  * Added indexes to sqlite3.schema._remake_table
    Otherwise indexes made using class based indexes get dropped whenever
    _remake_table is called.

Thanks timgraham & MarkusH for review and advice
@timgraham
Copy link
Member

merged in 6a8372e, thanks!

@timgraham timgraham closed this Aug 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants