Skip to content

Commit

Permalink
Add support for SQL Azure database edition options
Browse files Browse the repository at this point in the history
Fixes #2969
Fixes #15004
  • Loading branch information
AndriySvyryd committed Oct 3, 2019
1 parent 1bd4e85 commit a0f1676
Show file tree
Hide file tree
Showing 20 changed files with 881 additions and 168 deletions.
189 changes: 189 additions & 0 deletions src/EFCore.SqlServer/Extensions/SqlServerModelBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,195 @@ public static bool CanSetValueGenerationStrategy(
SqlServerAnnotationNames.ValueGenerationStrategy, valueGenerationStrategy, fromDataAnnotation);
}

/// <summary>
/// <para>
/// Configures the maximum size for Azure SQL Database.
/// </para>
/// <para>
/// Units must be included, e.g. "100 MB". See Azure SQL Database documentation for all supported values.
/// </para>
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="maxSize"> The maximum size of the database. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static ModelBuilder HasDatabaseMaxSize([NotNull] this ModelBuilder modelBuilder, [NotNull] string maxSize)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotNull(maxSize, nameof(maxSize));

modelBuilder.Model.SetDatabaseMaxSize(maxSize);

return modelBuilder;
}

/// <summary>
/// <para>
/// Attempts to configure the maximum size for Azure SQL Database.
/// </para>
/// <para>
/// Units must be included, e.g. "100 MB". See Azure SQL Database documentation for all supported values.
/// </para>
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="maxSize"> The maximum size of the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <c>null</c> otherwise.
/// </returns>
public static IConventionModelBuilder HasDatabaseMaxSize(
[NotNull] this IConventionModelBuilder modelBuilder, [CanBeNull] string maxSize, bool fromDataAnnotation = false)
{
if (modelBuilder.CanSetDatabaseMaxSize(maxSize, fromDataAnnotation))
{
modelBuilder.Metadata.SetDatabaseMaxSize(maxSize, fromDataAnnotation);
return modelBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the given value can be set as the maximum size of the database.
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="maxSize"> The maximum size of the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <c>true</c> if the given value can be set as the maximum size of the database. </returns>
public static bool CanSetDatabaseMaxSize(
[NotNull] this IConventionModelBuilder modelBuilder, [CanBeNull] string maxSize, bool fromDataAnnotation = false)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));

return modelBuilder.CanSetAnnotation(SqlServerAnnotationNames.MaxDatabaseSize, maxSize, fromDataAnnotation);
}

/// <summary>
/// <para>
/// Configures the service tier (EDITION) for Azure SQL Database.
/// </para>
/// <para>
/// Literals should be delimited with ''. See Azure SQL Database documentation for supported values.
/// </para>
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="serviceTier"> The service tier of the database. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static ModelBuilder HasServiceTier([NotNull] this ModelBuilder modelBuilder, [NotNull] string serviceTier)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotNull(serviceTier, nameof(serviceTier));

modelBuilder.Model.SetServiceTier(serviceTier);

return modelBuilder;
}

/// <summary>
/// <para>
/// Attempts to configure the service tier (EDITION) for Azure SQL Database.
/// </para>
/// <para>
/// Literals should be delimited with ''. See Azure SQL Database documentation for supported values.
/// </para>
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="serviceTier"> The service tier of the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <c>null</c> otherwise.
/// </returns>
public static IConventionModelBuilder HasServiceTier(
[NotNull] this IConventionModelBuilder modelBuilder, [CanBeNull] string serviceTier, bool fromDataAnnotation = false)
{
if (modelBuilder.CanSetServiceTier(serviceTier, fromDataAnnotation))
{
modelBuilder.Metadata.SetServiceTier(serviceTier, fromDataAnnotation);
return modelBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the given value can be set as the service tier of the database.
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="serviceTier"> The service tier of the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <c>true</c> if the given value can be set as the service tier of the database. </returns>
public static bool CanSetServiceTier(
[NotNull] this IConventionModelBuilder modelBuilder, [CanBeNull] string serviceTier, bool fromDataAnnotation = false)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));

return modelBuilder.CanSetAnnotation(SqlServerAnnotationNames.ServiceTier, serviceTier, fromDataAnnotation);
}

/// <summary>
/// <para>
/// Configures the performance level (SERVICE_OBJECTIVE) for Azure SQL Database.
/// </para>
/// <para>
/// Literals should be delimited with ''. See Azure SQL Database documentation for supported values.
/// </para>
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="performanceLevel"> The performance level of the database. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
public static ModelBuilder HasPerformanceLevel([NotNull] this ModelBuilder modelBuilder, [NotNull] string performanceLevel)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));
Check.NotNull(performanceLevel, nameof(performanceLevel));

modelBuilder.Model.SetPerformanceLevel(performanceLevel);

return modelBuilder;
}

/// <summary>
/// <para>
/// Attempts to configure the performance level (SERVICE_OBJECTIVE) for Azure SQL Database.
/// </para>
/// <para>
/// Literals should be delimited with ''. See Azure SQL Database documentation for supported values.
/// </para>
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="performanceLevel"> The performance level of the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <c>null</c> otherwise.
/// </returns>
public static IConventionModelBuilder HasPerformanceLevel(
[NotNull] this IConventionModelBuilder modelBuilder, [CanBeNull] string performanceLevel, bool fromDataAnnotation = false)
{
if (modelBuilder.CanSetPerformanceLevel(performanceLevel, fromDataAnnotation))
{
modelBuilder.Metadata.SetPerformanceLevel(performanceLevel, fromDataAnnotation);
return modelBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the given value can be set as the performance level of the database.
/// </summary>
/// <param name="modelBuilder"> The model builder. </param>
/// <param name="performanceLevel"> The performance level of the database. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> <c>true</c> if the given value can be set as the performance level of the database. </returns>
public static bool CanSetPerformanceLevel(
[NotNull] this IConventionModelBuilder modelBuilder, [CanBeNull] string performanceLevel, bool fromDataAnnotation = false)
{
Check.NotNull(modelBuilder, nameof(modelBuilder));

return modelBuilder.CanSetAnnotation(SqlServerAnnotationNames.PerformanceLevel, performanceLevel, fromDataAnnotation);
}

/// <summary>
/// Configures the model to use a sequence-based hi-lo pattern to generate values for key properties
/// marked as <see cref="ValueGenerated.OnAdd" />, when targeting SQL Server.
Expand Down
106 changes: 104 additions & 2 deletions src/EFCore.SqlServer/Extensions/SqlServerModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public static void SetIdentityIncrement(
=> (SqlServerValueGenerationStrategy?)model[SqlServerAnnotationNames.ValueGenerationStrategy];

/// <summary>
/// Attempts to set the <see cref="SqlServerValueGenerationStrategy" /> to use for properties
/// Sets the <see cref="SqlServerValueGenerationStrategy" /> to use for properties
/// of keys in the model that don't have a strategy explicitly set.
/// </summary>
/// <param name="model"> The model. </param>
Expand All @@ -201,7 +201,7 @@ public static void SetValueGenerationStrategy([NotNull] this IMutableModel model
=> model.SetOrRemoveAnnotation(SqlServerAnnotationNames.ValueGenerationStrategy, value);

/// <summary>
/// Attempts to set the <see cref="SqlServerValueGenerationStrategy" /> to use for properties
/// Sets the <see cref="SqlServerValueGenerationStrategy" /> to use for properties
/// of keys in the model that don't have a strategy explicitly set.
/// </summary>
/// <param name="model"> The model. </param>
Expand All @@ -218,5 +218,107 @@ public static void SetValueGenerationStrategy(
/// <returns> The <see cref="ConfigurationSource" /> for the default <see cref="SqlServerValueGenerationStrategy" />. </returns>
public static ConfigurationSource? GetValueGenerationStrategyConfigurationSource([NotNull] this IConventionModel model)
=> model.FindAnnotation(SqlServerAnnotationNames.ValueGenerationStrategy)?.GetConfigurationSource();

/// <summary>
/// Returns the maximum size of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The maximum size of the database. </returns>
public static string GetDatabaseMaxSize([NotNull] this IModel model)
=> (string)model[SqlServerAnnotationNames.MaxDatabaseSize];

/// <summary>
/// Sets the maximum size of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
public static void SetDatabaseMaxSize([NotNull] this IMutableModel model, [CanBeNull] string value)
=> model.SetOrRemoveAnnotation(SqlServerAnnotationNames.MaxDatabaseSize, value);

/// <summary>
/// Sets the maximum size of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
public static void SetDatabaseMaxSize(
[NotNull] this IConventionModel model, [CanBeNull] string value, bool fromDataAnnotation = false)
=> model.SetOrRemoveAnnotation(SqlServerAnnotationNames.MaxDatabaseSize, value, fromDataAnnotation);

/// <summary>
/// Returns the <see cref="ConfigurationSource" /> for the maximum size of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The <see cref="ConfigurationSource" /> for the maximum size of the database. </returns>
public static ConfigurationSource? GetMaxSizeConfigurationSource([NotNull] this IConventionModel model)
=> model.FindAnnotation(SqlServerAnnotationNames.MaxDatabaseSize)?.GetConfigurationSource();

/// <summary>
/// Returns the service tier of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The service tier of the database. </returns>
public static string GetServiceTier([NotNull] this IModel model)
=> (string)model[SqlServerAnnotationNames.ServiceTier];

/// <summary>
/// Sets the service tier of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
public static void SetServiceTier([NotNull] this IMutableModel model, [CanBeNull] string value)
=> model.SetOrRemoveAnnotation(SqlServerAnnotationNames.ServiceTier, value);

/// <summary>
/// Sets the service tier of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
public static void SetServiceTier(
[NotNull] this IConventionModel model, [CanBeNull] string value, bool fromDataAnnotation = false)
=> model.SetOrRemoveAnnotation(SqlServerAnnotationNames.ServiceTier, value, fromDataAnnotation);

/// <summary>
/// Returns the <see cref="ConfigurationSource" /> for the service tier of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The <see cref="ConfigurationSource" /> for the service tier of the database. </returns>
public static ConfigurationSource? GetServiceTierConfigurationSource([NotNull] this IConventionModel model)
=> model.FindAnnotation(SqlServerAnnotationNames.ServiceTier)?.GetConfigurationSource();

/// <summary>
/// Returns the performance level of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The performance level of the database. </returns>
public static string GetPerformanceLevel([NotNull] this IModel model)
=> (string)model[SqlServerAnnotationNames.PerformanceLevel];

/// <summary>
/// Sets the performance level of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
public static void SetPerformanceLevel([NotNull] this IMutableModel model, [CanBeNull] string value)
=> model.SetOrRemoveAnnotation(SqlServerAnnotationNames.PerformanceLevel, value);

/// <summary>
/// Sets the performance level of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <param name="value"> The value to set. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
public static void SetPerformanceLevel(
[NotNull] this IConventionModel model, [CanBeNull] string value, bool fromDataAnnotation = false)
=> model.SetOrRemoveAnnotation(SqlServerAnnotationNames.PerformanceLevel, value, fromDataAnnotation);

/// <summary>
/// Returns the <see cref="ConfigurationSource" /> for the performance level of the database.
/// </summary>
/// <param name="model"> The model. </param>
/// <returns> The <see cref="ConfigurationSource" /> for the performance level of the database. </returns>
public static ConfigurationSource? GetPerformanceLevelConfigurationSource([NotNull] this IConventionModel model)
=> model.FindAnnotation(SqlServerAnnotationNames.PerformanceLevel)?.GetConfigurationSource();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static IConventionSequenceBuilder HasHiLoSequence(
[CanBeNull] string schema,
bool fromDataAnnotation = false)
{
if (!propertyBuilder.CanSetHiLoSequence(name, schema))
if (!propertyBuilder.CanSetHiLoSequence(name, schema, fromDataAnnotation))
{
return null;
}
Expand Down
Loading

0 comments on commit a0f1676

Please sign in to comment.