Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a public abstraction for managing seed data #15288

Merged
merged 1 commit into from
Apr 17, 2019
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
45 changes: 25 additions & 20 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,52 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Update;

namespace Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal
{
public class CosmosDatabaseCreator : IDatabaseCreator
{
private readonly CosmosClientWrapper _cosmosClient;
private readonly StateManagerDependencies _stateManagerDependencies;
private readonly IModel _model;
private readonly IUpdateAdapterFactory _updateAdapterFactory;
private readonly IDatabase _database;

public CosmosDatabaseCreator(
CosmosClientWrapper cosmosClient,
StateManagerDependencies stateManagerDependencies)
IModel model,
IUpdateAdapterFactory updateAdapterFactory,
IDatabase database)
{
_cosmosClient = cosmosClient;
_stateManagerDependencies = stateManagerDependencies;
_model = model;
_updateAdapterFactory = updateAdapterFactory;
_database = database;
}

public bool EnsureCreated()
{
var created = _cosmosClient.CreateDatabaseIfNotExists();
foreach (var entityType in _stateManagerDependencies.Model.GetEntityTypes())
foreach (var entityType in _model.GetEntityTypes())
{
created |= _cosmosClient.CreateContainerIfNotExists(entityType.Cosmos().ContainerName, "__partitionKey");
}

if (created)
{
var stateManager = new StateManager(_stateManagerDependencies);
foreach (var entityType in _stateManagerDependencies.Model.GetEntityTypes())
var updateAdapter = _updateAdapterFactory.Create();
foreach (var entityType in _model.GetEntityTypes())
{
foreach (var targetSeed in entityType.GetData())
foreach (var targetSeed in entityType.GetSeedData())
{
var entry = stateManager.CreateEntry(targetSeed, entityType);
entry.SetEntityState(EntityState.Added);
var entry = updateAdapter.CreateEntry(targetSeed, entityType);
entry.EntityState = EntityState.Added;
}
}

stateManager.SaveChanges(acceptAllChangesOnSuccess: false);
_database.SaveChanges(updateAdapter.GetEntriesToSave());
}

return created;
Expand All @@ -53,24 +58,24 @@ public bool EnsureCreated()
public async Task<bool> EnsureCreatedAsync(CancellationToken cancellationToken = default)
{
var created = await _cosmosClient.CreateDatabaseIfNotExistsAsync(cancellationToken);
foreach (var entityType in _stateManagerDependencies.Model.GetEntityTypes())
foreach (var entityType in _model.GetEntityTypes())
{
created |= await _cosmosClient.CreateContainerIfNotExistsAsync(entityType.Cosmos().ContainerName, "__partitionKey", cancellationToken);
}

if (created)
{
var stateManager = new StateManager(_stateManagerDependencies);
foreach (var entityType in _stateManagerDependencies.Model.GetEntityTypes())
var updateAdapter = _updateAdapterFactory.Create();
foreach (var entityType in _model.GetEntityTypes())
{
foreach (var targetSeed in entityType.GetData())
foreach (var targetSeed in entityType.GetSeedData())
{
var entry = stateManager.CreateEntry(targetSeed, entityType);
entry.SetEntityState(EntityState.Added);
var entry = updateAdapter.CreateEntry(targetSeed, entityType);
entry.EntityState = EntityState.Added;
}
}

await stateManager.SaveChangesAsync(acceptAllChangesOnSuccess: false);
await _database.SaveChangesAsync(updateAdapter.GetEntriesToSave(), cancellationToken);
}

return created;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private IUpdateEntry GetRootDocument(InternalEntityEntry entry)
{
var stateManager = entry.StateManager;
var ownership = entry.EntityType.FindOwnership();
var principal = stateManager.GetPrincipal(entry, ownership);
var principal = stateManager.FindPrincipal(entry, ownership);
if (principal == null)
{
if (_sensitiveLoggingEnabled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ protected virtual void GenerateEntityType(
GenerateRelationships(builderName, entityType, stringBuilder);
}

GenerateData(builderName, entityType.GetProperties(), entityType.GetData(providerValues: true), stringBuilder);
GenerateData(builderName, entityType.GetProperties(), entityType.GetSeedData(providerValues: true), stringBuilder);
}

stringBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.EntityFrameworkCore.InMemory.Storage.Internal;
using Microsoft.EntityFrameworkCore.InMemory.ValueGeneration.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors;
using Microsoft.EntityFrameworkCore.Storage;
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.InMemory/Query/Internal/InMemoryQueryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class InMemoryQueryContext : QueryContext
/// </summary>
public InMemoryQueryContext(
[NotNull] QueryContextDependencies dependencies,
// Internal code: see #15096
[NotNull] Func<IQueryBuffer> queryBufferFactory,
[NotNull] IInMemoryStore store)
: base(dependencies, queryBufferFactory)
Expand Down
4 changes: 1 addition & 3 deletions src/EFCore.InMemory/Storage/Internal/IInMemoryDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -38,6 +36,6 @@ public interface IInMemoryDatabase : IDatabase
/// 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>
bool EnsureDatabaseCreated([NotNull] StateManagerDependencies stateManagerDependencies);
bool EnsureDatabaseCreated();
}
}
2 changes: 1 addition & 1 deletion src/EFCore.InMemory/Storage/Internal/IInMemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface IInMemoryStore
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
bool EnsureCreated(
[NotNull] StateManagerDependencies stateManagerDependencies,
[NotNull] IUpdateAdapterFactory updateAdapterFactory,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Update> updateLogger);

/// <summary>
Expand Down
9 changes: 6 additions & 3 deletions src/EFCore.InMemory/Storage/Internal/InMemoryDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Query;
Expand Down Expand Up @@ -36,6 +35,7 @@ namespace Microsoft.EntityFrameworkCore.InMemory.Storage.Internal
public class InMemoryDatabase : Database, IInMemoryDatabase
{
private readonly IInMemoryStore _store;
private readonly IUpdateAdapterFactory _updateAdapterFactory;
private readonly IDiagnosticsLogger<DbLoggerCategory.Update> _updateLogger;

/// <summary>
Expand All @@ -48,14 +48,17 @@ public InMemoryDatabase(
[NotNull] DatabaseDependencies dependencies,
[NotNull] IInMemoryStoreCache storeCache,
[NotNull] IDbContextOptions options,
[NotNull] IUpdateAdapterFactory updateAdapterFactory,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Update> updateLogger)
: base(dependencies)
{
Check.NotNull(storeCache, nameof(storeCache));
Check.NotNull(options, nameof(options));
Check.NotNull(updateAdapterFactory, nameof(updateAdapterFactory));
Check.NotNull(updateLogger, nameof(updateLogger));

_store = storeCache.GetStore(options);
_updateAdapterFactory = updateAdapterFactory;
_updateLogger = updateLogger;
}

Expand Down Expand Up @@ -93,8 +96,8 @@ public override Task<int> SaveChangesAsync(
/// 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 bool EnsureDatabaseCreated(StateManagerDependencies stateManagerDependencies)
=> _store.EnsureCreated(Check.NotNull(stateManagerDependencies, nameof(stateManagerDependencies)), _updateLogger);
public virtual bool EnsureDatabaseCreated()
=> _store.EnsureCreated(_updateAdapterFactory, _updateLogger);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
15 changes: 7 additions & 8 deletions src/EFCore.InMemory/Storage/Internal/InMemoryDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -27,19 +26,19 @@ namespace Microsoft.EntityFrameworkCore.InMemory.Storage.Internal
/// </summary>
public class InMemoryDatabaseCreator : IDatabaseCreator
{
private readonly StateManagerDependencies _stateManagerDependencies;
private readonly IDatabase _database;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// 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 InMemoryDatabaseCreator([NotNull] StateManagerDependencies stateManagerDependencies)
public InMemoryDatabaseCreator([NotNull] IDatabase database)
{
Check.NotNull(stateManagerDependencies, nameof(stateManagerDependencies));
Check.NotNull(database, nameof(database));

_stateManagerDependencies = stateManagerDependencies;
_database = database;
}

/// <summary>
Expand All @@ -48,7 +47,7 @@ public InMemoryDatabaseCreator([NotNull] StateManagerDependencies stateManagerDe
/// 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>
protected virtual IInMemoryDatabase Database => (IInMemoryDatabase)_stateManagerDependencies.Database;
protected virtual IInMemoryDatabase Database => (IInMemoryDatabase)_database;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -74,7 +73,7 @@ public virtual Task<bool> EnsureDeletedAsync(CancellationToken cancellationToken
/// 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 bool EnsureCreated() => Database.EnsureDatabaseCreated(_stateManagerDependencies);
public virtual bool EnsureCreated() => Database.EnsureDatabaseCreated();

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -83,7 +82,7 @@ public virtual Task<bool> EnsureDeletedAsync(CancellationToken cancellationToken
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Task<bool> EnsureCreatedAsync(CancellationToken cancellationToken = default)
=> Task.FromResult(Database.EnsureDatabaseCreated(_stateManagerDependencies));
=> Task.FromResult(Database.EnsureDatabaseCreated());

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
16 changes: 6 additions & 10 deletions src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.InMemory.Internal;
using Microsoft.EntityFrameworkCore.InMemory.ValueGeneration.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Update;

namespace Microsoft.EntityFrameworkCore.InMemory.Storage.Internal
Expand Down Expand Up @@ -82,7 +78,7 @@ public virtual InMemoryIntegerValueGenerator<TProperty> GetIntegerValueGenerator
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual bool EnsureCreated(
StateManagerDependencies stateManagerDependencies,
IUpdateAdapterFactory updateAdapterFactory,
IDiagnosticsLogger<DbLoggerCategory.Update> updateLogger)
{
lock (_lock)
Expand All @@ -93,14 +89,14 @@ public virtual bool EnsureCreated(
// ReSharper disable once AssignmentIsFullyDiscarded
_tables = CreateTables();

var stateManager = new StateManager(stateManagerDependencies);
var updateAdapter = updateAdapterFactory.Create();
var entries = new List<IUpdateEntry>();
foreach (var entityType in stateManagerDependencies.Model.GetEntityTypes())
foreach (var entityType in updateAdapter.Model.GetEntityTypes())
{
foreach (var targetSeed in entityType.GetData())
foreach (var targetSeed in entityType.GetSeedData())
{
var entry = stateManager.CreateEntry(targetSeed, entityType);
entry.SetEntityState(EntityState.Added);
var entry = updateAdapter.CreateEntry(targetSeed, entityType);
entry.EntityState = EntityState.Added;
entries.Add(entry);
}
}
Expand Down
16 changes: 10 additions & 6 deletions src/EFCore.InMemory/Storage/Internal/InMemoryTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.InMemory.Internal;
using Microsoft.EntityFrameworkCore.InMemory.ValueGeneration.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Update;
using Microsoft.EntityFrameworkCore.Utilities;

Expand All @@ -26,7 +24,8 @@ namespace Microsoft.EntityFrameworkCore.InMemory.Storage.Internal
/// </summary>
public class InMemoryTable<TKey> : IInMemoryTable
{
private readonly IPrincipalKeyValueFactory<TKey> _keyValueFactory;
// WARNING: The in-memory provider is using EF internal code here. This should not be copied by other providers. See #15096
private readonly ChangeTracking.Internal.IPrincipalKeyValueFactory<TKey> _keyValueFactory;
private readonly bool _sensitiveLoggingEnabled;
private readonly Dictionary<TKey, object[]> _rows;

Expand All @@ -38,7 +37,10 @@ public class InMemoryTable<TKey> : IInMemoryTable
/// 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 InMemoryTable([NotNull] IPrincipalKeyValueFactory<TKey> keyValueFactory, bool sensitiveLoggingEnabled)
public InMemoryTable(
// WARNING: The in-memory provider is using EF internal code here. This should not be copied by other providers. See #15096
[NotNull] ChangeTracking.Internal.IPrincipalKeyValueFactory<TKey> keyValueFactory,
bool sensitiveLoggingEnabled)
{
_keyValueFactory = keyValueFactory;
_sensitiveLoggingEnabled = sensitiveLoggingEnabled;
Expand All @@ -58,7 +60,8 @@ public virtual InMemoryIntegerValueGenerator<TProperty> GetIntegerValueGenerator
_integerGenerators = new Dictionary<int, IInMemoryIntegerValueGenerator>();
}

var propertyIndex = property.GetIndex();
// WARNING: The in-memory provider is using EF internal code here. This should not be copied by other providers. See #15096
var propertyIndex = EntityFrameworkCore.Metadata.Internal.PropertyBaseExtensions.GetIndex(property);
if (!_integerGenerators.TryGetValue(propertyIndex, out var generator))
{
generator = new InMemoryIntegerValueGenerator<TProperty>(propertyIndex);
Expand Down Expand Up @@ -212,8 +215,9 @@ private void BumpValueGenerators(object[] row)
}
}

// WARNING: The in-memory provider is using EF internal code here. This should not be copied by other providers. See #15096
private TKey CreateKey(IUpdateEntry entry)
=> _keyValueFactory.CreateFromCurrentValues((InternalEntityEntry)entry);
=> _keyValueFactory.CreateFromCurrentValues((ChangeTracking.Internal.InternalEntityEntry)entry);

private static object SnapshotValue(IProperty property, ValueComparer comparer, IUpdateEntry entry)
=> SnapshotValue(comparer, entry.GetCurrentValue(property));
Expand Down
Loading