Skip to content

Commit

Permalink
Rectified flake errors; added some comments and a test
Browse files Browse the repository at this point in the history
  • Loading branch information
akki committed Jun 7, 2016
1 parent d1696a5 commit d032273
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 11 deletions.
6 changes: 0 additions & 6 deletions django/db/migrations/operations/models.py
Expand Up @@ -747,13 +747,11 @@ def state_forwards(self, app_label, state):
model_state.options['indexes'].append(self.index)

def database_forwards(self, app_label, schema_editor, from_state, to_state):
to_model = to_state.apps.get_model(app_label, self.model_name)
schema_editor.create_index(
self.index,
)

def database_backwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.apps.get_model(app_label, self.model_name)
schema_editor.drop_index(
self.index,
)
Expand Down Expand Up @@ -793,15 +791,11 @@ def state_forwards(self, app_label, state):
model_state.options['indexes'].remove(self.index)

def database_forwards(self, app_label, schema_editor, from_state, to_state):
to_model = to_state.apps.get_model(app_label, self.model_name)
from_model_state = from_state.models[app_label, self.model_name.lower()]
schema_editor.drop_index(
self.index,
)

def database_backwards(self, app_label, schema_editor, from_state, to_state):
from_model = from_state.apps.get_model(app_label, self.model_name)
to_model_state = to_state.models[app_label, self.model_name.lower()]
schema_editor.create_index(
self.index,
)
Expand Down
4 changes: 2 additions & 2 deletions django/db/models/indexes.py
Expand Up @@ -6,7 +6,7 @@ class BaseIndex(object):

# The type of index - BTree, Hash, RTree, etc.
# Used in deconstruction to differentiate indexes of different types on
# the same fields in de
# the same fields in deconstruct.
index_type = None

def __init__(self, *fields, **kwargs):
Expand All @@ -32,7 +32,7 @@ def deconstruct(self):
kwargs = {
'type': self.index_type,
}
return (self.__class__.__name__, self.fields, {'type':self.index_type})
return (self.__class__.__name__, self.fields, kwargs)

def __repr__(self):
return '<%s: fields="%s">' % (self.__class__.__name__, ', '.join(self.fields))
Expand Down
5 changes: 2 additions & 3 deletions tests/migrations/test_operations.py
Expand Up @@ -1344,13 +1344,13 @@ def test_create_index(self):
Tests the CreateIndex operation.
"""
project_state = self.set_up_test_model("test_crin")
Pony = project_state.apps.get_model("test_crin", "Pony")
index = models.Index("pink")
operation = migrations.CreateIndex("Pony", index)
self.assertEqual(operation.describe(), "Create index on field(s) pink of model Pony")
new_state = project_state.clone()
operation.state_forwards("test_crin", new_state)
# Test the database alteration
self.assertEqual(len(new_state.models["test_crin", "pony"].options['indexes']), 1)
self.assertIndexNotExists("test_crin_pony", ["pink"])
with connection.schema_editor() as editor:
operation.database_forwards("test_crin", editor, project_state, new_state)
Expand All @@ -1372,13 +1372,12 @@ def test_drop_index(self):
project_state = self.set_up_test_model("test_rmin", index=True)
self.assertTableExists("test_rmin_pony")
self.assertIndexExists("test_rmin_pony", ["pink"])
# Test the state alteration
Pony = project_state.apps.get_model("test_rmin", "Pony")
index = models.Index("pink", name="pony_test_idx")
operation = migrations.DropIndex("Pony", index)
self.assertEqual(operation.describe(), "Remove index pony_test_idx from Pony")
new_state = project_state.clone()
operation.state_forwards("test_rmin", new_state)
# Test the state alteration
self.assertEqual(len(new_state.models["test_rmin", "pony"].options['indexes']), 0)
self.assertIndexExists("test_rmin_pony", ["pink"])
# Test the database alteration
Expand Down

0 comments on commit d032273

Please sign in to comment.