Skip to content

Commit

Permalink
Migrations: Annotate NRTs
Browse files Browse the repository at this point in the history
Fixes #24328, part of #19007
  • Loading branch information
bricelam committed Mar 5, 2021
1 parent 0f61aac commit 573410f
Show file tree
Hide file tree
Showing 84 changed files with 786 additions and 563 deletions.
20 changes: 11 additions & 9 deletions src/EFCore.Relational/Migrations/HistoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand All @@ -42,9 +44,9 @@ public abstract class HistoryRepository : IHistoryRepository
/// </summary>
public const string DefaultTableName = "__EFMigrationsHistory";

private IModel _model;
private string _migrationIdColumnName;
private string _productVersionColumnName;
private IModel? _model;
private string? _migrationIdColumnName;
private string? _productVersionColumnName;

/// <summary>
/// Initializes a new instance of this class.
Expand Down Expand Up @@ -80,15 +82,15 @@ protected virtual ISqlGenerationHelper SqlGenerationHelper
/// <summary>
/// The schema that contains the history table, or <see langword="null" /> if the default schema should be used.
/// </summary>
protected virtual string TableSchema { get; }
protected virtual string? TableSchema { get; }

/// <summary>
/// The name of the column that holds the Migration identifier.
/// </summary>
protected virtual string MigrationIdColumnName
=> _migrationIdColumnName ??= EnsureModel()
.FindEntityType(typeof(HistoryRow))
.FindProperty(nameof(HistoryRow.MigrationId))
.FindEntityType(typeof(HistoryRow))!
.FindProperty(nameof(HistoryRow.MigrationId))!
.GetColumnBaseName();

private IModel EnsureModel()
Expand Down Expand Up @@ -120,8 +122,8 @@ private IModel EnsureModel()
/// </summary>
protected virtual string ProductVersionColumnName
=> _productVersionColumnName ??= EnsureModel()
.FindEntityType(typeof(HistoryRow))
.FindProperty(nameof(HistoryRow.ProductVersion))
.FindEntityType(typeof(HistoryRow))!
.FindProperty(nameof(HistoryRow.ProductVersion))!
.GetColumnBaseName();

/// <summary>
Expand Down Expand Up @@ -169,7 +171,7 @@ await Dependencies.RawSqlCommandBuilder.Build(ExistsSql).ExecuteScalarAsync(
/// Interprets the result of executing <see cref="ExistsSql" />.
/// </summary>
/// <returns><see langword="true" /> if the table already exists, <see langword="false" /> otherwise.</returns>
protected abstract bool InterpretExistsResult([NotNull] object value);
protected abstract bool InterpretExistsResult([CanBeNull] object? value);

/// <summary>
/// Overridden by a database provider to generate a SQL script that will create the history table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Migrations/HistoryRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Utilities;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Migrations/IHistoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Migrations/IMigrationCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/EFCore.Relational/Migrations/IMigrationsAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand All @@ -32,7 +34,7 @@ public interface IMigrationsAssembly
/// <summary>
/// The snapshot of the <see cref="IModel" /> contained in the assembly.
/// </summary>
ModelSnapshot ModelSnapshot { get; }
ModelSnapshot? ModelSnapshot { get; }

/// <summary>
/// The assembly that contains the migrations, snapshot, etc.
Expand All @@ -45,7 +47,7 @@ public interface IMigrationsAssembly
/// </summary>
/// <param name="nameOrId"> The name or identifier to lookup. </param>
/// <returns> The identifier of the migration, or <see langword="null" /> if none was found. </returns>
string FindMigrationId([NotNull] string nameOrId);
string? FindMigrationId([NotNull] string nameOrId);

/// <summary>
/// Creates an instance of the migration class.
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Relational/Migrations/IMigrationsIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand Down
6 changes: 4 additions & 2 deletions src/EFCore.Relational/Migrations/IMigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand All @@ -32,7 +34,7 @@ public interface IMigrationsModelDiffer
/// <returns>
/// <see langword="true" /> if there are any differences and <see langword="false" /> otherwise.
/// </returns>
bool HasDifferences([CanBeNull] IRelationalModel source, [CanBeNull] IRelationalModel target);
bool HasDifferences([CanBeNull] IRelationalModel? source, [CanBeNull] IRelationalModel? target);

/// <summary>
/// Finds the differences between two models.
Expand All @@ -43,6 +45,6 @@ public interface IMigrationsModelDiffer
/// A list of the operations that need to applied to the database to migrate it
/// from mapping to the source model so that is now mapping to the target model.
/// </returns>
IReadOnlyList<MigrationOperation> GetDifferences([CanBeNull] IRelationalModel source, [CanBeNull] IRelationalModel target);
IReadOnlyList<MigrationOperation> GetDifferences([CanBeNull] IRelationalModel? source, [CanBeNull] IRelationalModel? target);
}
}
4 changes: 3 additions & 1 deletion src/EFCore.Relational/Migrations/IMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand All @@ -32,7 +34,7 @@ public interface IMigrationsSqlGenerator
/// <returns> The list of commands to be executed or scripted. </returns>
IReadOnlyList<MigrationCommand> Generate(
[NotNull] IReadOnlyList<MigrationOperation> operations,
[CanBeNull] IModel model = null,
[CanBeNull] IModel? model = null,
MigrationsSqlGenerationOptions options = MigrationsSqlGenerationOptions.Default);
}
}
10 changes: 6 additions & 4 deletions src/EFCore.Relational/Migrations/IMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations
{
/// <summary>
Expand All @@ -30,7 +32,7 @@ public interface IMigrator
/// <param name="targetMigration">
/// The target migration to migrate the database to, or <see langword="null" /> to migrate to the latest.
/// </param>
void Migrate([CanBeNull] string targetMigration = null);
void Migrate([CanBeNull] string? targetMigration = null);

/// <summary>
/// Migrates the database to either a specified target migration or up to the latest
Expand All @@ -43,7 +45,7 @@ public interface IMigrator
/// <returns> A task that represents the asynchronous operation </returns>
/// <exception cref="OperationCanceledException"> If the <see cref="CancellationToken"/> is canceled. </exception>
Task MigrateAsync(
[CanBeNull] string targetMigration = null,
[CanBeNull] string? targetMigration = null,
CancellationToken cancellationToken = default);

/// <summary>
Expand All @@ -61,8 +63,8 @@ Task MigrateAsync(
/// </param>
/// <returns> The generated script. </returns>
string GenerateScript(
[CanBeNull] string fromMigration = null,
[CanBeNull] string toMigration = null,
[CanBeNull] string? fromMigration = null,
[CanBeNull] string? toMigration = null,
MigrationsSqlGenerationOptions options = MigrationsSqlGenerationOptions.Default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations.Internal
{
/// <summary>
Expand Down Expand Up @@ -45,7 +47,7 @@ public virtual void ExecuteNonQuery(

try
{
IDbContextTransaction transaction = null;
IDbContextTransaction? transaction = null;

try
{
Expand Down Expand Up @@ -103,7 +105,7 @@ public virtual async Task ExecuteNonQueryAsync(

try
{
IDbContextTransaction transaction = null;
IDbContextTransaction? transaction = null;

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Reflection;
using JetBrains.Annotations;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations.Internal
{
/// <summary>
Expand All @@ -21,6 +23,6 @@ public static class MigrationExtensions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string GetId([NotNull] this Migration migration)
=> migration.GetType().GetCustomAttribute<MigrationAttribute>()?.Id;
=> migration.GetType().GetCustomAttribute<MigrationAttribute>()!.Id;
}
}
14 changes: 8 additions & 6 deletions src/EFCore.Relational/Migrations/Internal/MigrationsAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations.Internal
{
/// <summary>
Expand All @@ -31,8 +33,8 @@ public class MigrationsAssembly : IMigrationsAssembly
{
private readonly IMigrationsIdGenerator _idGenerator;
private readonly IDiagnosticsLogger<DbLoggerCategory.Migrations> _logger;
private IReadOnlyDictionary<string, TypeInfo> _migrations;
private ModelSnapshot _modelSnapshot;
private IReadOnlyDictionary<string, TypeInfo>? _migrations;
private ModelSnapshot? _modelSnapshot;
private readonly Type _contextType;

/// <summary>
Expand Down Expand Up @@ -110,12 +112,12 @@ orderby id
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ModelSnapshot ModelSnapshot
public virtual ModelSnapshot? ModelSnapshot
=> _modelSnapshot
??= (from t in Assembly.GetConstructibleTypes()
where t.IsSubclassOf(typeof(ModelSnapshot))
&& t.GetCustomAttribute<DbContextAttribute>()?.ContextType == _contextType
select (ModelSnapshot)Activator.CreateInstance(t.AsType()))
select (ModelSnapshot)Activator.CreateInstance(t.AsType())!)
.FirstOrDefault();

/// <summary>
Expand All @@ -132,7 +134,7 @@ where t.IsSubclassOf(typeof(ModelSnapshot))
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual string FindMigrationId(string nameOrId)
public virtual string? FindMigrationId(string nameOrId)
=> Migrations.Keys
.Where(
_idGenerator.IsValidId(nameOrId)
Expand All @@ -151,7 +153,7 @@ public virtual Migration CreateMigration(TypeInfo migrationClass, string activeP
{
Check.NotNull(activeProvider, nameof(activeProvider));

var migration = (Migration)Activator.CreateInstance(migrationClass.AsType());
var migration = (Migration)Activator.CreateInstance(migrationClass.AsType())!;
migration.ActiveProvider = activeProvider;

return migration;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Text.RegularExpressions;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Migrations.Internal
{
/// <summary>
Expand Down
Loading

0 comments on commit 573410f

Please sign in to comment.