Skip to content

Commit

Permalink
[automated] Merge branch 'release/9.0' => 'main'
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd authored Oct 10, 2024
2 parents a0d6550 + 2311edc commit babb8b9
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 24 deletions.
11 changes: 11 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ extends:
displayName: Build
- task: AzureCLI@2
displayName: Run Cosmos tests
condition: notin(variables['Build.Reason'], 'PullRequest', 'Schedule')
inputs:
azureSubscription: EFCosmosTesting
scriptType: bash
Expand Down Expand Up @@ -231,6 +232,16 @@ extends:
arguments: 'locals all -clear'
- template: /eng/common/templates-official/steps/enable-internal-sources.yml
- template: /eng/common/templates-official/steps/enable-internal-runtimes.yml
- ${{ if ne(variables['System.TeamProject'], 'public') }}:
- template: /eng/common/core-templates/steps/get-delegation-sas.yml
parameters:
federatedServiceConnection: 'dotnetbuilds-internal-read'
outputVariableName: 'dotnetbuilds-internal-container-read-token'
expiryInHours: 1
base64Encode: false
storageAccount: dotnetbuilds
container: internal
permissions: rl
- script: restore.cmd -ci /p:configuration=$(_BuildConfig) $(_InternalRuntimeDownloadArgs)
displayName: Restore packages
- script: .dotnet\dotnet build eng\helix.proj /restore /t:Test /p:configuration=$(_BuildConfig) /bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/SendToHelix.binlog $(_InternalRuntimeDownloadArgs)
Expand Down
11 changes: 10 additions & 1 deletion eng/common/core-templates/steps/get-delegation-sas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ steps:
# Calculate the expiration of the SAS token and convert to UTC
$expiry = (Get-Date).AddHours(${{ parameters.expiryInHours }}).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ssZ")
$sas = az storage container generate-sas --account-name ${{ parameters.storageAccount }} --name ${{ parameters.container }} --permissions ${{ parameters.permissions }} --expiry $expiry --auth-mode login --as-user -o tsv
# Temporarily work around a helix issue where SAS tokens with / in them will cause incorrect downloads
# of correlation payloads.
$sas = ""
do {
$sas = az storage container generate-sas --account-name ${{ parameters.storageAccount }} --name ${{ parameters.container }} --permissions ${{ parameters.permissions }} --expiry $expiry --auth-mode login --as-user -o tsv
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to generate SAS token."
exit 1
}
} while($sas.IndexOf('/') -ne -1)
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to generate SAS token."
Expand Down
6 changes: 3 additions & 3 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public virtual DbContext CreateContext(string? contextType)
public virtual IEnumerable<DbContext> CreateAllContexts()
{
EF.IsDesignTime = true;
var types = FindContextTypes();
var types = FindContextTypes(useServiceProvider: false);
foreach (var contextPair in types)
{
yield return CreateContext(null, contextPair);
Expand Down Expand Up @@ -499,7 +499,7 @@ public virtual IEnumerable<Type> GetContextTypes()
public virtual Type GetContextType(string? name)
=> FindContextType(name).Key;

private IDictionary<Type, Func<DbContext>> FindContextTypes(string? name = null)
private IDictionary<Type, Func<DbContext>> FindContextTypes(string? name = null, bool useServiceProvider = true)
{
_reporter.WriteVerbose(DesignStrings.FindingContexts);

Expand Down Expand Up @@ -576,7 +576,7 @@ where i.IsGenericType
}

if (contexts.Values.All(f => f != null)
&& (string.IsNullOrEmpty(name) || contexts.Count == 1))
&& (!useServiceProvider || contexts.Count == 1))
{
return contexts!;
}
Expand Down
18 changes: 12 additions & 6 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,19 @@ private void EnsureMigrationsAssembly(IServiceProvider services)
var assemblyName = _assembly.GetName();
var options = services.GetRequiredService<IDbContextOptions>();
var contextType = services.GetRequiredService<ICurrentDbContext>().Context.GetType();
var migrationsAssemblyName = RelationalOptionsExtension.Extract(options).MigrationsAssembly
?? contextType.Assembly.GetName().Name;
if (assemblyName.Name != migrationsAssemblyName
&& assemblyName.FullName != migrationsAssemblyName)
var optionsExtension = RelationalOptionsExtension.Extract(options);
if (optionsExtension.MigrationsAssemblyObject == null
|| optionsExtension.MigrationsAssemblyObject != _assembly)
{
throw new OperationException(
DesignStrings.MigrationsAssemblyMismatch(assemblyName.Name, migrationsAssemblyName));
var migrationsAssemblyName = optionsExtension.MigrationsAssembly
?? optionsExtension.MigrationsAssemblyObject?.GetName().Name
?? contextType.Assembly.GetName().Name;
if (assemblyName.Name != migrationsAssemblyName
&& assemblyName.FullName != migrationsAssemblyName)
{
throw new OperationException(
DesignStrings.MigrationsAssemblyMismatch(assemblyName.Name, migrationsAssemblyName));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,7 @@ public static CoreOptionsExtension WithDefaultWarningConfiguration(CoreOptionsEx
.TryWithExplicit(RelationalEventId.IndexPropertiesMappedToNonOverlappingTables, WarningBehavior.Throw)
.TryWithExplicit(RelationalEventId.ForeignKeyPropertiesMappedToUnrelatedTables, WarningBehavior.Throw)
.TryWithExplicit(RelationalEventId.StoredProcedureConcurrencyTokenNotMapped, WarningBehavior.Throw)
.TryWithExplicit(RelationalEventId.PendingModelChangesWarning, WarningBehavior.Throw)
.TryWithExplicit(RelationalEventId.NonTransactionalMigrationOperationWarning, WarningBehavior.Throw));
.TryWithExplicit(RelationalEventId.PendingModelChangesWarning, WarningBehavior.Throw));

/// <summary>
/// Information/metadata for a <see cref="RelationalOptionsExtension" />.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public MigrationsAssembly(
{
_contextType = currentContext.Context.GetType();

var assemblyName = RelationalOptionsExtension.Extract(options).MigrationsAssembly;
var assemblyObject = RelationalOptionsExtension.Extract(options).MigrationsAssemblyObject;
var optionsExtension = RelationalOptionsExtension.Extract(options);
var assemblyName = optionsExtension.MigrationsAssembly;
var assemblyObject = optionsExtension.MigrationsAssemblyObject;

Assembly = assemblyName == null
? assemblyObject ?? _contextType.Assembly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.WithMany(e => e.Users)
.UsingEntity<CustomUserRoleString>(
j => j.HasOne(e => e.Role).WithMany(e => e.UserRoles).HasForeignKey(e => e.RoleId),
j => j.HasOne(e => e.User).WithMany(e => e.UserRoles).HasForeignKey(e => e.RoleId));
j => j.HasOne(e => e.User).WithMany(e => e.UserRoles).HasForeignKey(e => e.UserId));

b.HasMany(e => e.Claims).WithOne(e => e.User).HasForeignKey(uc => uc.UserId).IsRequired();
b.HasMany(e => e.Logins).WithOne(e => e.User).HasForeignKey(ul => ul.UserId).IsRequired();
b.HasMany(e => e.Tokens).WithOne(e => e.User).HasForeignKey(ut => ut.UserId).IsRequired();
b.HasMany(e => e.UserRoles).WithOne(e => e.User).HasForeignKey(ur => ur.UserId).IsRequired();
b.ToTable("MyUsers");
b.Property(u => u.UserName).HasMaxLength(128);
b.Property(u => u.NormalizedUserName).HasMaxLength(128);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,23 @@ public class DbContextOperationsTest
{
[ConditionalFact]
public void CreateContext_gets_service()
=> CreateOperations(typeof(TestProgram)).CreateContext(typeof(TestContext).FullName.ToLower());
=> CreateOperations(typeof(TestProgram), includeContext: false).CreateContext(typeof(TestContext).FullName.ToLower());

[ConditionalFact]
public void CreateContext_gets_service_without_name()
=> CreateOperations(typeof(TestProgram), includeContext: false).CreateContext(null);

[ConditionalFact]
public void CreateContext_gets_service_without_AddDbContext()
=> CreateOperations(typeof(TestProgramWithoutAddDbContext)).CreateContext(typeof(TestContext).FullName);

[ConditionalFact]
public void CreateContext_gets_service_when_context_factory_used()
=> CreateOperations(typeof(TestProgramWithContextFactory)).CreateContext(typeof(TestContextFromFactory).FullName);
=> CreateOperations(typeof(TestProgramWithContextFactory), includeContext: false).CreateContext(typeof(TestContextFromFactory).FullName);

[ConditionalFact]
public void CreateContext_gets_service_when_context_factory_used_without_name()
=> CreateOperations(typeof(TestProgramWithContextFactory), includeContext: false).CreateContext(null);

[ConditionalFact]
public void CreateContext_throws_if_context_type_not_found()
Expand Down Expand Up @@ -230,10 +238,9 @@ public void Optimize_throws_when_no_contexts()

Assert.Equal(
DesignStrings.NoContextsToOptimize,
Assert.Throws<OperationException>(
() =>
operations.Optimize(
null, null, contextTypeName: "*", null, scaffoldModel: true, precompileQueries: false, nativeAot: false)).Message);
Assert.Throws<OperationException>(() =>
operations.Optimize(
null, null, contextTypeName: "*", null, scaffoldModel: true, precompileQueries: false, nativeAot: false)).Message);

Assert.DoesNotContain(reporter.Messages, m => m.Level == LogLevel.Critical);
Assert.DoesNotContain(reporter.Messages, m => m.Level == LogLevel.Error);
Expand Down Expand Up @@ -364,9 +371,14 @@ private static TestWebHost BuildWebHost(string[] args)
=> CreateWebHost(b => b.UseSqlServer(@"Cake=None"));
}

private static TestDbContextOperations CreateOperations(Type testProgramType)
private static TestDbContextOperations CreateOperations(Type testProgramType, bool includeContext = true)
{
var assembly = MockAssembly.Create(testProgramType, typeof(TestContext));
List<Type> types = [testProgramType];
if (includeContext)
{
types.Add(typeof(TestContext));
}
var assembly = MockAssembly.Create([.. types]);
var reporter = new TestOperationReporter();
var operations = new TestDbContextOperations(
reporter,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,34 @@ public void Can_pass_null_args()
args: null);
}

[ConditionalFact]
public void Can_use_migrations_assembly()
{
// Even though newer versions of the tools will pass an empty array
// older versions of the tools can pass null args.
var assembly = MockAssembly.Create(typeof(AssemblyTestContext));
var migrationsAssembly = MockAssembly.Create();
AssemblyTestContext.MigrationsAssembly = migrationsAssembly;
var testOperations = new TestMigrationsOperations(
new TestOperationReporter(),
assembly,
assembly,
"projectDir",
"RootNamespace",
"C#",
nullable: false,
args: null);

testOperations.AddMigration("Test", null, null, null, dryRun: true);
}

private class TestContext : DbContext;

private class AssemblyTestContext : DbContext
{
public static Assembly MigrationsAssembly { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlServer(o => o.MigrationsAssembly(MigrationsAssembly));
}
}

0 comments on commit babb8b9

Please # to comment.