Is it possible to create a filtered index with FluentMigrator? The scenario is that I want to create a unique index on a column that may contain NULLs, so the filter should exclude rows with NULL for the indexed column.
I've modified an index in SQL Server 2012 generated by FluentMigrator to use such a filter and can confirm that it works well, so the remaining piece of the puzzle is to generate this option.
A filtered index is an optimized disk-based rowstore nonclustered index especially suited to cover queries that select from a well-defined subset of data. It uses a filter predicate to index a portion of rows in the table.
Filtered Indexes can be found by querying the built in sys. indexes DMV. To get the schema and table that the indexes belong to, you'll also need to join to the sys. schemas and sys.
As this is a very Sql Server specific feature you might as well fall back to sql.
One of the reasons for the fluent style is that it is not database specific so the same migration can be run for different database types. But if you are only ever going to use Sql Server and want to use database specific features then the great thing about FluentMigrator is that it allows you to execute sql statements. This is recommended for advanced stuff that we will never support in FluentMigrator and for changes in Stored Procedures.
It would simply be:
Execute.Sql(@"CREATE NONCLUSTERED INDEX FIBillOfMaterialsWithEndDate
ON Production.BillOfMaterials (ComponentID, StartDate)
WHERE EndDate IS NOT NULL;");
Postgres has partial indexes too so this could be something that we will add to FluentMigrator in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With