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

Update Cosmos SDK #16530

Merged
merged 1 commit into from
Jul 9, 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
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PropertyGroup Label="Dependencies from nuget.org">
<SystemThreadingTasksExtensionsVersion>4.5.2</SystemThreadingTasksExtensionsVersion>
<CastleCorePackageVersion>4.2.1</CastleCorePackageVersion>
<MicrosoftAzureCosmosPackageVersion>3.0.0.18-preview</MicrosoftAzureCosmosPackageVersion>
<MicrosoftAzureCosmosPackageVersion>3.0.0.19-preview</MicrosoftAzureCosmosPackageVersion>
<MicrosoftCodeAnalysisCSharpPackageVersion>2.8.0</MicrosoftCodeAnalysisCSharpPackageVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>2.8.0</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
<mod_spatialitePackageVersion>4.3.0.1</mod_spatialitePackageVersion>
Expand Down
42 changes: 13 additions & 29 deletions src/EFCore.Cosmos/Infrastructure/CosmosModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,6 @@ protected virtual void ValidateSharedContainerCompatibility(
[NotNull] string containerName,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)
{
if (mappedTypes.Count == 1)
{
var entityType = mappedTypes[0];
var partitionKeyPropertyName = entityType.GetCosmosPartitionKeyPropertyName();
if (partitionKeyPropertyName != null)
{
var nextPartitionKeyProperty = entityType.FindProperty(partitionKeyPropertyName);
if (nextPartitionKeyProperty == null)
{
throw new InvalidOperationException(
CosmosStrings.PartitionKeyMissingProperty(entityType.DisplayName(), partitionKeyPropertyName));
}
}
return;
}

var discriminatorValues = new Dictionary<object, IEntityType>();
IProperty partitionKey = null;
IEntityType firstEntityType = null;
Expand All @@ -104,6 +88,14 @@ protected virtual void ValidateSharedContainerCompatibility(
CosmosStrings.PartitionKeyMissingProperty(entityType.DisplayName(), partitionKeyPropertyName));
}

var keyType = nextPartitionKeyProperty.GetTypeMapping().Converter?.ProviderClrType
?? nextPartitionKeyProperty.ClrType;
if (keyType != typeof(string))
{
throw new InvalidOperationException(CosmosStrings.PartitionKeyNonStringStoreType(
partitionKeyPropertyName, entityType.DisplayName(), keyType.ShortDisplayName()));
}

if (partitionKey == null)
{
if (firstEntityType != null)
Expand All @@ -119,25 +111,17 @@ protected virtual void ValidateSharedContainerCompatibility(
partitionKey.Name, firstEntityType.DisplayName(), partitionKey.GetCosmosPropertyName(),
nextPartitionKeyProperty.Name, entityType.DisplayName(), nextPartitionKeyProperty.GetCosmosPropertyName()));
}
else if ((partitionKey.GetTypeMapping().Converter?.ProviderClrType ?? partitionKey.ClrType)
!= (nextPartitionKeyProperty.GetTypeMapping().Converter?.ProviderClrType ?? nextPartitionKeyProperty.ClrType))
{
throw new InvalidOperationException(
CosmosStrings.PartitionKeyStoreTypeMismatch(
partitionKey.Name,
firstEntityType.DisplayName(),
(partitionKey.GetTypeMapping().Converter?.ProviderClrType ?? partitionKey.ClrType).ShortDisplayName(),
nextPartitionKeyProperty.Name,
entityType.DisplayName(),
(nextPartitionKeyProperty.GetTypeMapping().Converter?.ProviderClrType ?? nextPartitionKeyProperty.ClrType)
.ShortDisplayName()));
}
}
else if (partitionKey != null)
{
throw new InvalidOperationException(CosmosStrings.NoPartitionKey(entityType.DisplayName(), containerName));
}

if (mappedTypes.Count == 1)
{
break;
}

if (firstEntityType == null)
{
firstEntityType = entityType;
Expand Down
16 changes: 8 additions & 8 deletions src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/EFCore.Cosmos/Properties/CosmosStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@
<data name="PartitionKeyMissingProperty" xml:space="preserve">
<value>The partition key for entity type '{entityType}' is set to '{property}', but there is no property with that name.</value>
</data>
<data name="PartitionKeyNonStringStoreType" xml:space="preserve">
<value>The type of the partition key property '{property}' on '{entityType}' is '{propertyType}'. All partition key properties need to be strings or have a string converter.</value>
</data>
<data name="PartitionKeyStoreNameMismatch" xml:space="preserve">
<value>The partition key property '{property1}' on '{entityType1}' is mapped as '{storeName1}', but the partition key property '{property2}' on '{entityType2}' is mapped as '{storeName2}'. All partition key properties need to be mapped to the same store property.</value>
</data>
<data name="PartitionKeyStoreTypeMismatch" xml:space="preserve">
<value>The type of the partition key property '{property1}' on '{entityType1}' is '{propertyType1}', but the type of the partition key property '{property2}' on '{entityType2}' is '{propertyType2}'. All partition key properties need to have matching types.</value>
</data>
</root>
32 changes: 16 additions & 16 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,26 @@ private async Task<bool> CreateContainerIfNotExistsOnceAsync(
public bool CreateItem(
string containerId,
JToken document,
object partitionKey)
string partitionKey)
=> _executionStrategyFactory.Create().Execute(
(containerId, document, partitionKey), CreateItemOnce, null);

private bool CreateItemOnce(
DbContext context,
(string ContainerId, JToken Document, object PartitionKey) parameters)
(string ContainerId, JToken Document, string PartitionKey) parameters)
=> CreateItemOnceAsync(context, parameters).GetAwaiter().GetResult();

public Task<bool> CreateItemAsync(
string containerId,
JToken document,
object partitionKey,
string partitionKey,
CancellationToken cancellationToken = default)
=> _executionStrategyFactory.Create().ExecuteAsync(
(containerId, document, partitionKey), CreateItemOnceAsync, null, cancellationToken);

private async Task<bool> CreateItemOnceAsync(
DbContext _,
(string ContainerId, JToken Document, object PartitionKey) parameters,
(string ContainerId, JToken Document, string PartitionKey) parameters,
CancellationToken cancellationToken = default)
{
using (var stream = new MemoryStream())
Expand All @@ -193,27 +193,27 @@ public bool ReplaceItem(
string collectionId,
string documentId,
JObject document,
object partitionKey)
string partitionKey)
=> _executionStrategyFactory.Create().Execute(
(collectionId, documentId, document, partitionKey), ReplaceItemOnce, null);

private bool ReplaceItemOnce(
DbContext context,
(string ContainerId, string ItemId, JObject Document, object PartitionKey) parameters)
(string ContainerId, string ItemId, JObject Document, string PartitionKey) parameters)
=> ReplaceItemOnceAsync(context, parameters).GetAwaiter().GetResult();

public Task<bool> ReplaceItemAsync(
string collectionId,
string documentId,
JObject document,
object partitionKey,
string partitionKey,
CancellationToken cancellationToken = default)
=> _executionStrategyFactory.Create().ExecuteAsync(
(collectionId, documentId, document, partitionKey), ReplaceItemOnceAsync, null, cancellationToken);

private async Task<bool> ReplaceItemOnceAsync(
DbContext _,
(string ContainerId, string ItemId, JObject Document, object PartitionKey) parameters,
(string ContainerId, string ItemId, JObject Document, string PartitionKey) parameters,
CancellationToken cancellationToken = default)
{
using var stream = new MemoryStream();
Expand All @@ -236,26 +236,26 @@ private async Task<bool> ReplaceItemOnceAsync(
public bool DeleteItem(
string containerId,
string documentId,
object partitionKey)
string partitionKey)
=> _executionStrategyFactory.Create().Execute(
(containerId, documentId, partitionKey), DeleteItemOnce, null);

public bool DeleteItemOnce(
DbContext context,
(string ContainerId, string DocumentId, object PartitionKey) parameters)
(string ContainerId, string DocumentId, string PartitionKey) parameters)
=> DeleteItemOnceAsync(context, parameters).GetAwaiter().GetResult();

public Task<bool> DeleteItemAsync(
string containerId,
string documentId,
object partitionKey,
string partitionKey,
CancellationToken cancellationToken = default)
=> _executionStrategyFactory.Create().ExecuteAsync(
(containerId, documentId, partitionKey), DeleteItemOnceAsync, null, cancellationToken);

public async Task<bool> DeleteItemOnceAsync(
DbContext _,
(string ContainerId, string DocumentId, object PartitionKey) parameters,
(string ContainerId, string DocumentId, string PartitionKey) parameters,
CancellationToken cancellationToken = default)
{
var items = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
Expand All @@ -266,10 +266,10 @@ public async Task<bool> DeleteItemOnceAsync(
}
}

private PartitionKey CreatePartitionKey(object partitionKey)
private PartitionKey CreatePartitionKey(string partitionKey)
=> partitionKey == null
? PartitionKey.NonePartitionKeyValue
: new PartitionKey(partitionKey);
? PartitionKey.None
: new PartitionKey(partitionKey);

public IEnumerable<JObject> ExecuteSqlQuery(
string containerId,
Expand Down Expand Up @@ -297,7 +297,7 @@ private FeedIterator CreateQuery(
var queryDefinition = new QueryDefinition(query.Query);
foreach (var parameter in query.Parameters)
{
queryDefinition.UseParameter(parameter.Name, parameter.Value);
queryDefinition = queryDefinition.WithParameter(parameter.Name, parameter.Value);
}

return container.GetItemQueryStreamIterator(queryDefinition);
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ private IUpdateEntry GetRootDocument(InternalEntityEntry entry)
return principal.EntityType.IsDocumentRoot() ? principal : GetRootDocument(principal);
}

private static object GetPartitionKey(IUpdateEntry entry)
private static string GetPartitionKey(IUpdateEntry entry)
{
object partitionKey = null;
var partitionKeyPropertyName = entry.EntityType.GetCosmosPartitionKeyPropertyName();
Expand All @@ -309,7 +309,7 @@ private static object GetPartitionKey(IUpdateEntry entry)
}
}

return partitionKey;
return (string)partitionKey;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task Cosmos_client_instance_is_shared_between_contexts()
Assert.Same(client, context.Database.GetCosmosClient());
}

await using (var testDatabase2 = CosmosTestStore.CreateInitialized(DatabaseName, o => o.Region(CosmosRegions.AustraliaCentral)))
await using (var testDatabase2 = CosmosTestStore.CreateInitialized(DatabaseName, o => o.Region(Regions.AustraliaCentral)))
{
options = CreateOptions(testDatabase2);

Expand All @@ -54,7 +54,7 @@ public async Task Cosmos_client_instance_is_shared_between_contexts()
[ConditionalFact]
public async Task Should_not_throw_if_specified_region_is_right()
{
var regionName = CosmosRegions.AustraliaCentral;
var regionName = Regions.AustraliaCentral;

await using (var testDatabase = CosmosTestStore.CreateInitialized(DatabaseName, o => o.Region(regionName)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class CosmosDbContextOptionsExtensionsTests
[ConditionalFact]
public void Can_create_options_with_specified_region()
{
var regionName = CosmosRegions.EastAsia;
var regionName = Regions.EastAsia;
var options = new DbContextOptionsBuilder().UseCosmos(
"serviceEndPoint",
"authKeyOrResourceToken",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public virtual void Passes_on_valid_partition_keys()
{
var modelBuilder = CreateConventionalModelBuilder();
modelBuilder.Entity<Customer>().ForCosmosToContainer("Orders").ForCosmosHasPartitionKey(c => c.PartitionId);
modelBuilder.Entity<Order>().ForCosmosToContainer("Orders").ForCosmosHasPartitionKey(c => c.PartitionId);
modelBuilder.Entity<Order>().ForCosmosToContainer("Orders").ForCosmosHasPartitionKey(o => o.PartitionId)
.Property(o => o.PartitionId).HasConversion<string>();

var model = modelBuilder.Model;
Validate(model);
Expand Down Expand Up @@ -83,12 +84,12 @@ public virtual void Detects_partition_key_of_different_type()
{
var modelBuilder = CreateConventionalModelBuilder();
modelBuilder.Entity<Customer>().ForCosmosToContainer("Orders").ForCosmosHasPartitionKey(c => c.PartitionId);
modelBuilder.Entity<Order>().ForCosmosToContainer("Orders").ForCosmosHasPartitionKey(c => c.PartitionId)
.Property(c => c.PartitionId).HasConversion<string>();
modelBuilder.Entity<Order>().ForCosmosToContainer("Orders").ForCosmosHasPartitionKey(o => o.PartitionId)
.Property(c => c.PartitionId).HasConversion<int>();

var model = modelBuilder.Model;
VerifyError(CosmosStrings.PartitionKeyStoreTypeMismatch(
nameof(Customer.PartitionId), typeof(Customer).Name, "int", nameof(Order.PartitionId), typeof(Order).Name, "string"), model);
VerifyError(CosmosStrings.PartitionKeyNonStringStoreType(
nameof(Customer.PartitionId), typeof(Order).Name, "int"), model);
}

[ConditionalFact]
Expand Down Expand Up @@ -130,14 +131,14 @@ private class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int PartitionId { get; set; }
public string PartitionId { get; set; }
public ICollection<Order> Orders { get; set; }
}

private class Order
{
public int Id { get; set; }
public int PartitionId { get; set; }
public string PartitionId { get; set; }
public Customer Customer { get; set; }
}
}
Expand Down