martinjw / dbschemareader

Read database metadata (from SqlServer/Oracle/MySql/SQLite/PostgreSql/DB2 etc) into one simple model
Microsoft Public License
293 stars 128 forks source link

Feature Request: Option to create a nullable unique index #140

Closed Arnagos closed 9 months ago

Arnagos commented 2 years ago

I can't seem to find an option to create nullable unique indices.

I think it would be reasonable to create a nullable unique index whenever any column(s) of the index can be null.

Desired SQL:

CREATE UNIQUE NONCLUSTERED INDEX [Punchclock__Company_ContactInfoId_idx] 
    ON [Punchclock__Company]([ContactInfoId])
    WHERE [ContactInfoId] IS NOT NULL;

Currently, it just generates this SQL:

CREATE UNIQUE NONCLUSTERED INDEX [Punchclock__Company_ContactInfoId_idx] 
    ON [Punchclock__Company]([ContactInfoId]);
martinjw commented 2 years ago

This is Filtered indexes, which is SqlServer 2008+ but not sure if other dbs support (maybe function indexes?)

It will have to be quite limited (just a string where you have to add the WHERE sql).

Arnagos commented 2 years ago

Sure, generating this much myself should be no problem at all. Thanks!

Arnagos commented 2 years ago

I created a local NuGet package with your changes and tried it out. Works perfectly fine.

Arnagos commented 1 year ago

@martinjw Can you also add this feature to PostgreSQL? It's supported via partial indexes. The syntax seems to be the exact same as in MSSQL.

Arnagos commented 10 months ago

Sadly the change generates invalid SQL:

CREATE INDEX "X_idx" ON "Y"("A", "B", "C"); WHERE "C" IS NOT NULL

The ; is not removed from the end of the initial SQL and also not appended to the new ending.

martinjw commented 9 months ago

Fixed in https://github.com/martinjw/dbschemareader/releases/tag/2.10.1.0