Add test coverage for index recreation after altering computed columns #26656
Labels
area-migrations
area-test
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
type-enhancement
Milestone
Description
If an index exists on a column before a migration and an identical index should exist after a migration, then the index is not added during the migration. This seems to make sense but it fails to take into account that an index on a column will be automatically dropped if the column is dropped. Certain migrations will cause a column to be dropped and re-added, such as changing the SQL of a computed column. You can test it using the code below.
Code
See also this sample solution
Generate an initial migration and script with the above model.
Now modify the HasComputedColumnSql to something else, e.g.
Generate a migration from the previous migration.
The initial migration code loos like this
The next migration looks like this
Observe that the index is not re-added even though it will be dropped when the column is dropped.
Expected behavior
The index should be added after the migration.
There are a number of ways this could be done. One way would be to always drop indexes on columns that are dropped beforehand, then add the indexes again afterwards. (Dropping the index or checking if the index exists or calculating if the index will be automatically dropped is needed because I think the index may not be automatically dropped if it involves other columns).
Provider and version information
EF Core version: 5.0.11
Database provider: Any I assume but tested using Postgres
Target framework: NET 5.0
Operating system: Windows 10 21H1
IDE: Visual Studio 2019 16.11
The text was updated successfully, but these errors were encountered: