Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude any indexes with hidden columns from being included in DatabaseModel #16681

Merged
merged 2 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ private void GetIndexes(DbConnection connection, IReadOnlyList<DatabaseTable> ta
{
using (var command = connection.CreateCommand())
{
command.CommandText = @"
var commandText = @"
SELECT
SCHEMA_NAME([t].[schema_id]) AS [table_schema],
[t].[name] AS [table_name],
Expand All @@ -812,9 +812,31 @@ private void GetIndexes(DbConnection connection, IReadOnlyList<DatabaseTable> ta
FROM [sys].[indexes] AS [i]
JOIN [sys].[tables] AS [t] ON [i].[object_id] = [t].[object_id]
JOIN [sys].[index_columns] AS [ic] ON [i].[object_id] = [ic].[object_id] AND [i].[index_id] = [ic].[index_id]
WHERE " + tableFilter + @"
JOIN [sys].[columns] AS [c] ON [ic].[object_id] = [c].[object_id] AND [ic].[column_id] = [c].[column_id]
WHERE " + tableFilter;

if (SupportsTemporalTable(connection))
{
commandText += @"
AND CAST([i].[object_id] AS nvarchar(12)) + CAST([i].[index_id] AS nvarchar(12)) NOT IN
(
SELECT CAST([i].[object_id] AS nvarchar(12)) + CAST([i].[index_id] AS nvarchar(12))
FROM [sys].[indexes] i
JOIN [sys].[tables] AS [t] ON [i].[object_id] = [t].[object_id]
JOIN [sys].[index_columns] AS [ic] ON [i].[object_id] = [ic].[object_id] AND [i].[index_id] = [ic].[index_id]
JOIN [sys].[columns] AS [c] ON [ic].[object_id] = [c].[object_id] AND [ic].[column_id] = [c].[column_id]
WHERE " + tableFilter;

commandText += @"
AND [c].[is_hidden] = 1
)";
}

commandText += @"
ORDER BY [table_schema], [table_name], [index_name], [ic].[key_ordinal]";

command.CommandText = commandText;

using (var reader = command.ExecuteReader())
{
var tableIndexGroups = reader.Cast<DbDataRecord>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,9 @@ Name varchar(50) NOT NULL,
PERIOD FOR SYSTEM_TIME(SysStartTime, SysEndTime)
)
WITH (SYSTEM_VERSIONING = ON (HISTORY_TABLE = dbo.HiddenColumnsTableHistory));
CREATE INDEX IX_HiddenColumnsTable_1 ON dbo.HiddenColumnsTable ( Name, SysStartTime);
CREATE INDEX IX_HiddenColumnsTable_2 ON dbo.HiddenColumnsTable ( SysStartTime);
CREATE INDEX IX_HiddenColumnsTable_3 ON dbo.HiddenColumnsTable ( Name );
",
Enumerable.Empty<string>(),
Enumerable.Empty<string>(),
Expand All @@ -1538,6 +1541,7 @@ PERIOD FOR SYSTEM_TIME(SysStartTime, SysEndTime)
Assert.Equal(2, columns.Count);
Assert.DoesNotContain(columns, c => c.Name == "SysStartTime");
Assert.DoesNotContain(columns, c => c.Name == "SysEndTime");
Assert.Equal("IX_HiddenColumnsTable_3", dbModel.Tables.Single().Indexes.Single().Name);
},
@"
ALTER TABLE dbo.HiddenColumnsTable SET (SYSTEM_VERSIONING = OFF);
Expand Down