Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 41 additions & 9 deletions src/Core/RevEng.Core.60/PatchedSqlServerDatabaseModelFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.Logging;

#nullable enable

Expand Down Expand Up @@ -635,10 +636,7 @@ FROM [sys].[views] AS [v]
// This is done separately due to MARS property may be turned off
GetColumns(connection, tables, filter, viewFilter, typeAliases, databaseCollation);

if (SupportsIndexes())
{
GetIndexes(connection, tables, filter);
}
GetIndexes(connection, tables, filter);

GetForeignKeys(connection, tables, filter);

Expand Down Expand Up @@ -929,7 +927,7 @@ private void GetIndexes(DbConnection connection, IReadOnlyList<DatabaseTable> ta
[i].[has_filter],
[i].[filter_definition],
[i].[fill_factor],
COL_NAME([ic].[object_id], [ic].[column_id]) AS [column_name],
[c].[name] AS [column_name],
[ic].[is_included_column]
FROM [sys].[indexes] AS [i]
JOIN [sys].[tables] AS [t] ON [i].[object_id] = [t].[object_id]
Expand Down Expand Up @@ -986,7 +984,8 @@ FROM [sys].[indexes] i

if (primaryKeyGroups.Length == 1)
{
if (TryGetPrimaryKey(primaryKeyGroups[0], out var primaryKey))
if (TryGetPrimaryKey(primaryKeyGroups[0], out var primaryKey)
&& IsValidPrimaryKey(primaryKey))
{
_logger.PrimaryKeyFound(primaryKey.Name!, DisplayName(tableSchema, tableName));
table.PrimaryKey = primaryKey;
Expand Down Expand Up @@ -1062,6 +1061,14 @@ bool TryGetPrimaryKey(
return true;
}

bool IsValidPrimaryKey(DatabasePrimaryKey primaryKey)
{
if (_engineEdition != 1000)
return true;

return primaryKey.Columns.Count == 1 && primaryKey.Columns[0].StoreType == "uniqueidentifier";
}

bool TryGetUniqueConstraint(
IGrouping<(string? Name, string? TypeDesc), DbDataRecord> uniqueConstraintGroup,
[NotNullWhen(true)] out DatabaseUniqueConstraint? uniqueConstraint)
Expand Down Expand Up @@ -1260,6 +1267,14 @@ FROM [sys].[foreign_keys] AS [f]
foreignKey.PrincipalColumns.Add(principalColumn);
}

if (!invalid && _engineEdition == 1000 && !IsValidDataverseForeignKey(foreignKey))
{
invalid = true;
_logger.Logger.LogWarning("ForeignKey {ForeignKeyName} on table {TableName} is not supported in Dataverse.",
fkName!,
DisplayName(table.Schema, table.Name));
}

if (!invalid)
{
if (foreignKey.Columns.SequenceEqual(foreignKey.PrincipalColumns))
Expand Down Expand Up @@ -1287,6 +1302,26 @@ FROM [sys].[foreign_keys] AS [f]
table.ForeignKeys.Add(foreignKey);
}
}

bool IsValidDataverseForeignKey(DatabaseForeignKey foreignKey)
{
if (foreignKey.Columns.Count != 1)
return false;

if (foreignKey.Columns[0].StoreType != "uniqueidentifier")
return false;

if (foreignKey.PrincipalTable.PrimaryKey == null)
return false;

if (foreignKey.PrincipalTable.PrimaryKey.Columns.Count != 1)
return false;

if (foreignKey.PrincipalTable.PrimaryKey.Columns[0].Name != foreignKey.PrincipalColumns[0].Name)
return false;

return true;
}
}
}
}
Expand All @@ -1300,9 +1335,6 @@ private bool SupportsMemoryOptimizedTable()
private bool SupportsSequences()
=> _compatibilityLevel >= 110 && (_engineEdition != 6 && _engineEdition != 11 && _engineEdition != 1000);

private bool SupportsIndexes()
=> _engineEdition != 1000;

private bool SupportsViews()
=> _engineEdition != 1000;

Expand Down
1 change: 0 additions & 1 deletion src/Core/RevEng.Core.80/RevEng.Core.80.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
</ItemGroup>

<ItemGroup>
<Compile Remove="..\RevEng.Core.60\CrmDatabaseModelFactory.cs" />
<Compile Remove="..\RevEng.Core.60\PatchedSqlServerDatabaseModelFactory.cs" />
</ItemGroup>

Expand Down
1 change: 0 additions & 1 deletion src/Core/RevEng.Core.90/RevEng.Core.90.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
</ItemGroup>

<ItemGroup>
<Compile Remove="..\RevEng.Core.60\CrmDatabaseModelFactory.cs" />
<Compile Remove="..\RevEng.Core.60\CustomTemplateFileService.cs" />
<Compile Remove="..\RevEng.Core.60\PatchedSqlServerDatabaseModelFactory.cs" />
</ItemGroup>
Expand Down