Skip to content

Regression: index dropped in migration and not recreated #58

Open
@sparrowt

Description

@sparrowt

Problem

In the following cases (possibly more) any indexes on the column are dropped, but aren't recreated afterwards:
(a) when a column is made null-able (i.e. adding null=True)
(b) when a column is renamed
(c) when a table is renamed

This is quite a major issue because it is silently leaving the database in the wrong state - the migration doesn't fail, but now certain columns with db_index=True won't actually have an index which could cause very slow queries.

Cause

As far as I can see this is a triple-regression introduced by 2e60754 (for #24).

That commit added new calls to _delete_indexes in 2 places causing the first two cases listed above:

(a) 2e6075456a#diff-e4c1520d8b49ccbf46382bd2eed4e740R400
(b) 2e6075456a#diff-e4c1520d8b49ccbf46382bd2eed4e740R334-R335

and an explicit index deletion here before renaming a table causing the third case:

(c) 2e6075456a#diff-e4c1520d8b49ccbf46382bd2eed4e740R222-R225

but in none of those cases is the index re-instated afterwards, even if the field still has db_index=True. Index restoration only happens in certain specific cases (e.g. type change / removal of nullability) which doesn't include the above 3 cases.

Reproduction

See the 3 tests I added on this branch:
master...sparrowt:issue58-index-reinstatement
which fail due to the bugs described above, but pass if run on django-mssql-backend v2.4.2 (before #24)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions