Skip to content

Commit

Permalink
SQL Server scaffolding:
Browse files Browse the repository at this point in the history
Use compatibility_level for feature detection
Unblock scaffolding on Azure SQL DW

fixes dotnet#14566
fixes dotnet#5553
  • Loading branch information
ErikEJ committed Apr 22, 2019
1 parent a69d84b commit 71c047b
Showing 1 changed file with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ public override DatabaseModel Create(DbConnection connection, DatabaseModelFacto
var tableList = options.Tables.ToList();
var tableFilter = GenerateTableFilter(tableList.Select(Parse).ToList(), schemaFilter);

if (Version.TryParse(connection.ServerVersion, out var serverVersion)
&& serverVersion.Major >= 11)
if (SupportsSequences(connection))
{
foreach (var sequence in GetSequences(connection, schemaFilter, typeAliases))
{
Expand Down Expand Up @@ -438,9 +437,8 @@ private IEnumerable<DatabaseTable> GetTables(
using (var command = connection.CreateCommand())
{
var tables = new List<DatabaseTable>();
Version.TryParse(connection.ServerVersion, out var serverVersion);
var supportsMemoryOptimizedTable = serverVersion?.Major >= 12;
var supportsTemporalTable = serverVersion?.Major >= 13;
var supportsMemoryOptimizedTable = SupportsMemoryOptimizedTable(connection);
var supportsTemporalTable = SupportsTemporalTable(connection);

var commandText = @"
SELECT
Expand Down Expand Up @@ -546,8 +544,7 @@ FROM [sys].[columns] AS [c]
LEFT JOIN [sys].[computed_columns] AS [cc] ON [c].[object_id] = [cc].[object_id] AND [c].[column_id] = [cc].[column_id]
WHERE " + tableFilter;

if (Version.TryParse(connection.ServerVersion, out var serverVersion)
&& serverVersion.Major >= 13)
if (SupportsTemporalTable(connection))
{
commandText += " AND [c].[is_hidden] = 0";
}
Expand Down Expand Up @@ -1022,6 +1019,45 @@ FROM [sys].[foreign_keys] AS [f]
}
}

private bool SupportsTemporalTable(DbConnection connection)
{
return CompatibilityLevel(connection) >= 130 && EngineEdition(connection) != 6;
}

private bool SupportsMemoryOptimizedTable(DbConnection connection)
{
return CompatibilityLevel(connection) >= 120 && EngineEdition(connection) != 6;
}

private bool SupportsSequences(DbConnection connection)
{
return CompatibilityLevel(connection) >= 110 && EngineEdition(connection) != 6;
}

private int EngineEdition(DbConnection connection)
{
using (var command = connection.CreateCommand())
{
command.CommandText = @"
SELECT SERVERPROPERTY('EngineEdition');";
return (int)command.ExecuteScalar();
}
}

private byte CompatibilityLevel(DbConnection connection)
{
using (var command = connection.CreateCommand())
{
command.CommandText = $@"
SELECT compatibility_level
FROM sys.databases
WHERE name = '{connection.Database}';";

var result = command.ExecuteScalar();
return result != null ? Convert.ToByte(result) : (byte)0;
}
}

private static string DisplayName(string schema, string name)
=> (!string.IsNullOrEmpty(schema) ? schema + "." : "") + name;

Expand Down

0 comments on commit 71c047b

Please sign in to comment.