Python django.db.migrations.AddIndex() Examples

The following are 11 code examples for showing how to use django.db.migrations.AddIndex(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.db.migrations , or try the search function .

Example 1
Project: django-sqlserver   Author: denisenkom   File: test_operations.py    License: MIT License 6 votes vote down vote up
def test_indexes_ignore_swapped(self):
        """
        Add/RemoveIndex operations ignore swapped models.
        """
        operation = migrations.AddIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
        project_state, new_state = self.make_test_state('test_adinigsw', operation)
        with connection.schema_editor() as editor:
            # No database queries should be run for swapped models
            operation.database_forwards('test_adinigsw', editor, project_state, new_state)
            operation.database_backwards('test_adinigsw', editor, new_state, project_state)

        operation = migrations.RemoveIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
        project_state, new_state = self.make_test_state("test_rminigsw", operation)
        with connection.schema_editor() as editor:
            operation.database_forwards('test_rminigsw', editor, project_state, new_state)
            operation.database_backwards('test_rminigsw', editor, new_state, project_state) 
Example 2
Project: djongo   Author: nesdis   File: test_operations.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_indexes_ignore_swapped(self):
        """
        Add/RemoveIndex operations ignore swapped models.
        """
        operation = migrations.AddIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
        project_state, new_state = self.make_test_state('test_adinigsw', operation)
        with connection.schema_editor() as editor:
            # No database queries should be run for swapped models
            operation.database_forwards('test_adinigsw', editor, project_state, new_state)
            operation.database_backwards('test_adinigsw', editor, new_state, project_state)

        operation = migrations.RemoveIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
        project_state, new_state = self.make_test_state("test_rminigsw", operation)
        with connection.schema_editor() as editor:
            operation.database_forwards('test_rminigsw', editor, project_state, new_state)
            operation.database_backwards('test_rminigsw', editor, new_state, project_state) 
Example 3
Project: djongo   Author: nesdis   File: test_operations.py    License: GNU Affero General Public License v3.0 6 votes vote down vote up
def test_indexes_ignore_swapped(self):
        """
        Add/RemoveIndex operations ignore swapped models.
        """
        operation = migrations.AddIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
        project_state, new_state = self.make_test_state('test_adinigsw', operation)
        with connection.schema_editor() as editor:
            # No database queries should be run for swapped models
            operation.database_forwards('test_adinigsw', editor, project_state, new_state)
            operation.database_backwards('test_adinigsw', editor, new_state, project_state)

        operation = migrations.RemoveIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
        project_state, new_state = self.make_test_state("test_rminigsw", operation)
        with connection.schema_editor() as editor:
            operation.database_forwards('test_rminigsw', editor, project_state, new_state)
            operation.database_backwards('test_rminigsw', editor, new_state, project_state) 
Example 4
Project: django-sqlserver   Author: denisenkom   File: test_operations.py    License: MIT License 5 votes vote down vote up
def test_add_index(self):
        """
        Test the AddIndex operation.
        """
        project_state = self.set_up_test_model("test_adin")
        msg = (
            "Indexes passed to AddIndex operations require a name argument. "
            "<Index: fields='pink'> doesn't have one."
        )
        with self.assertRaisesMessage(ValueError, msg):
            migrations.AddIndex("Pony", models.Index(fields=["pink"]))
        index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx")
        operation = migrations.AddIndex("Pony", index)
        self.assertEqual(operation.describe(), "Create index test_adin_pony_pink_idx on field(s) pink of model Pony")
        new_state = project_state.clone()
        operation.state_forwards("test_adin", new_state)
        # Test the database alteration
        self.assertEqual(len(new_state.models["test_adin", "pony"].options['indexes']), 1)
        self.assertIndexNotExists("test_adin_pony", ["pink"])
        with connection.schema_editor() as editor:
            operation.database_forwards("test_adin", editor, project_state, new_state)
        self.assertIndexExists("test_adin_pony", ["pink"])
        # And test reversal
        with connection.schema_editor() as editor:
            operation.database_backwards("test_adin", editor, new_state, project_state)
        self.assertIndexNotExists("test_adin_pony", ["pink"])
        # And deconstruction
        definition = operation.deconstruct()
        self.assertEqual(definition[0], "AddIndex")
        self.assertEqual(definition[1], [])
        self.assertEqual(definition[2], {'model_name': "Pony", 'index': index}) 
Example 5
Project: django-sqlserver   Author: denisenkom   File: test_operations.py    License: MIT License 5 votes vote down vote up
def test_add_index_state_forwards(self):
        project_state = self.set_up_test_model('test_adinsf')
        index = models.Index(fields=['pink'], name='test_adinsf_pony_pink_idx')
        old_model = project_state.apps.get_model('test_adinsf', 'Pony')
        new_state = project_state.clone()

        operation = migrations.AddIndex('Pony', index)
        operation.state_forwards('test_adinsf', new_state)
        new_model = new_state.apps.get_model('test_adinsf', 'Pony')
        self.assertIsNot(old_model, new_model) 
Example 6
Project: django-sqlserver   Author: denisenkom   File: test_operations.py    License: MIT License 5 votes vote down vote up
def test_remove_index_state_forwards(self):
        project_state = self.set_up_test_model('test_rminsf')
        index = models.Index(fields=['pink'], name='test_rminsf_pony_pink_idx')
        migrations.AddIndex('Pony', index).state_forwards('test_rminsf', project_state)
        old_model = project_state.apps.get_model('test_rminsf', 'Pony')
        new_state = project_state.clone()

        operation = migrations.RemoveIndex('Pony', 'test_rminsf_pony_pink_idx')
        operation.state_forwards('test_rminsf', new_state)
        new_model = new_state.apps.get_model('test_rminsf', 'Pony')
        self.assertIsNot(old_model, new_model) 
Example 7
Project: djongo   Author: nesdis   File: test_operations.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_add_index(self):
        """
        Test the AddIndex operation.
        """
        project_state = self.set_up_test_model("test_adin")
        msg = (
            "Indexes passed to AddIndex operations require a name argument. "
            "<Index: fields='pink'> doesn't have one."
        )
        with self.assertRaisesMessage(ValueError, msg):
            migrations.AddIndex("Pony", models.Index(fields=["pink"]))
        index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx")
        operation = migrations.AddIndex("Pony", index)
        self.assertEqual(operation.describe(), "Create index test_adin_pony_pink_idx on field(s) pink of model Pony")
        new_state = project_state.clone()
        operation.state_forwards("test_adin", new_state)
        # Test the database alteration
        self.assertEqual(len(new_state.models["test_adin", "pony"].options['indexes']), 1)
        self.assertIndexNotExists("test_adin_pony", ["pink"])
        with connection.schema_editor() as editor:
            operation.database_forwards("test_adin", editor, project_state, new_state)
        self.assertIndexExists("test_adin_pony", ["pink"])
        # And test reversal
        with connection.schema_editor() as editor:
            operation.database_backwards("test_adin", editor, new_state, project_state)
        self.assertIndexNotExists("test_adin_pony", ["pink"])
        # And deconstruction
        definition = operation.deconstruct()
        self.assertEqual(definition[0], "AddIndex")
        self.assertEqual(definition[1], [])
        self.assertEqual(definition[2], {'model_name': "Pony", 'index': index}) 
Example 8
Project: djongo   Author: nesdis   File: test_operations.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_add_index_state_forwards(self):
        project_state = self.set_up_test_model('test_adinsf')
        index = models.Index(fields=['pink'], name='test_adinsf_pony_pink_idx')
        old_model = project_state.apps.get_model('test_adinsf', 'Pony')
        new_state = project_state.clone()

        operation = migrations.AddIndex('Pony', index)
        operation.state_forwards('test_adinsf', new_state)
        new_model = new_state.apps.get_model('test_adinsf', 'Pony')
        self.assertIsNot(old_model, new_model) 
Example 9
Project: djongo   Author: nesdis   File: test_operations.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_remove_index_state_forwards(self):
        project_state = self.set_up_test_model('test_rminsf')
        index = models.Index(fields=['pink'], name='test_rminsf_pony_pink_idx')
        migrations.AddIndex('Pony', index).state_forwards('test_rminsf', project_state)
        old_model = project_state.apps.get_model('test_rminsf', 'Pony')
        new_state = project_state.clone()

        operation = migrations.RemoveIndex('Pony', 'test_rminsf_pony_pink_idx')
        operation.state_forwards('test_rminsf', new_state)
        new_model = new_state.apps.get_model('test_rminsf', 'Pony')
        self.assertIsNot(old_model, new_model) 
Example 10
Project: djongo   Author: nesdis   File: test_operations.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_add_index(self):
        """
        Test the AddIndex operation.
        """
        project_state = self.set_up_test_model("test_adin")
        msg = (
            "Indexes passed to AddIndex operations require a name argument. "
            "<Index: fields='pink'> doesn't have one."
        )
        with self.assertRaisesMessage(ValueError, msg):
            migrations.AddIndex("Pony", models.Index(fields=["pink"]))
        index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx")
        operation = migrations.AddIndex("Pony", index)
        self.assertEqual(operation.describe(), "Create index test_adin_pony_pink_idx on field(s) pink of model Pony")
        new_state = project_state.clone()
        operation.state_forwards("test_adin", new_state)
        # Test the database alteration
        self.assertEqual(len(new_state.models["test_adin", "pony"].options['indexes']), 1)
        self.assertIndexNotExists("test_adin_pony", ["pink"])
        with connection.schema_editor() as editor:
            operation.database_forwards("test_adin", editor, project_state, new_state)
        self.assertIndexExists("test_adin_pony", ["pink"])
        # And test reversal
        with connection.schema_editor() as editor:
            operation.database_backwards("test_adin", editor, new_state, project_state)
        self.assertIndexNotExists("test_adin_pony", ["pink"])
        # And deconstruction
        definition = operation.deconstruct()
        self.assertEqual(definition[0], "AddIndex")
        self.assertEqual(definition[1], [])
        self.assertEqual(definition[2], {'model_name': "Pony", 'index': index}) 
Example 11
Project: djongo   Author: nesdis   File: test_operations.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_remove_index_state_forwards(self):
        project_state = self.set_up_test_model('test_rminsf')
        index = models.Index(fields=['pink'], name='test_rminsf_pony_pink_idx')
        migrations.AddIndex('Pony', index).state_forwards('test_rminsf', project_state)
        old_model = project_state.apps.get_model('test_rminsf', 'Pony')
        new_state = project_state.clone()

        operation = migrations.RemoveIndex('Pony', 'test_rminsf_pony_pink_idx')
        operation.state_forwards('test_rminsf', new_state)
        new_model = new_state.apps.get_model('test_rminsf', 'Pony')
        self.assertIsNot(old_model, new_model)