public async Task ExecuteAsync(IDocumentClient client, DocumentCollection collection, Uri relativeCollectionUri) { EnsureArg.IsNotNull(client, nameof(client)); EnsureArg.IsNotNull(collection, nameof(collection)); var thisVersion = await GetLatestCollectionVersion(client, collection); if (thisVersion.Version < CollectionSettingsVersion) { _logger.LogDebug("Ensuring indexes are up-to-date {CollectionUri}", _configuration.GetAbsoluteCollectionUri(_collectionConfiguration.CollectionId)); collection.IndexingPolicy = new IndexingPolicy { IncludedPaths = new Collection <IncludedPath> { new IncludedPath { Path = "/*", Indexes = new Collection <Index> { new RangeIndex(DataType.Number, -1), new RangeIndex(DataType.String, -1), }, }, }, ExcludedPaths = new Collection <ExcludedPath> { new ExcludedPath { Path = $"/{KnownResourceWrapperProperties.RawResource}/*", }, }, }; // Setting the DefaultTTL to -1 means that by default all documents in the collection will live forever // but the Cosmos DB service should monitor this collection for documents that have overridden this default. // See: https://docs.microsoft.com/en-us/azure/cosmos-db/time-to-live collection.DefaultTimeToLive = -1; await client.ReplaceDocumentCollectionAsync(collection); thisVersion.Version = CollectionSettingsVersion; await client.UpsertDocumentAsync(relativeCollectionUri, thisVersion); } }
/// <summary> /// Creates a collection if it does not exist. This functionality is defined in DocumentClient, but not IDocumentClient. /// </summary> /// <param name="documentClient">The document client</param> /// <param name="databaseUri">The database URI</param> /// <param name="collectionUri">The collection URI</param> /// <param name="documentCollection">The collection to create</param> /// <param name="options">The request options</param> /// <returns>The result</returns> public static async Task <ResourceResponse <DocumentCollection> > CreateDocumentCollectionIfNotExistsAsync( this IDocumentClient documentClient, Uri databaseUri, Uri collectionUri, DocumentCollection documentCollection, RequestOptions options = null) { return(await CreateIfNotExists( async() => { DocumentCollection existingDocumentCollection = await documentClient.ReadDocumentCollectionAsync(collectionUri, options); existingDocumentCollection.IndexingPolicy = documentCollection.IndexingPolicy; existingDocumentCollection.DefaultTimeToLive = documentCollection.DefaultTimeToLive; return await documentClient.ReplaceDocumentCollectionAsync(existingDocumentCollection, options); }, () => documentClient.CreateDocumentCollectionAsync(databaseUri, documentCollection, options))); }
public async void EnsureConfigCurrentWithChangedSignature() { var dbConfig = CreateDbConfig(); string oldSig = "old_sig"; string newSig = "new_sig"; var configDoc = CreateConfigDoc("Test", oldSig); DocumentStoreConfigBuilder storeABuilder = new DocumentStoreConfigBuilder("StoreA"); storeABuilder.AddDocument("DocA"); DocumentStoreConfigBuilder storeBBuilder = new DocumentStoreConfigBuilder("StoreB"); storeBBuilder.AddDocument("DocA"); storeBBuilder.AddDocument("DocB"); var configSourceA = CreateConfigSource(storeABuilder); var configSourceB = CreateConfigSource(storeBBuilder); _signatureGenerator.CreateSignature(Arg.Any <IList <DocumentStoreConfig> >()).Returns(newSig); _documentClient.ReadDocumentAsync(Arg.Any <Uri>(), Arg.Is <RequestOptions>(x => x.PartitionKey != null)).Returns(WrapResource(configDoc)); // Existing index that should remain. var includeIdx1 = new IncludedPath(); includeIdx1.Path = "/content_Test_StoreA_DocA/*"; includeIdx1.Indexes.Add(new HashIndex(DataType.String, -1)); // Existing index that should be removed. It is no longer present. var includeIdx2 = new IncludedPath(); includeIdx2.Path = "/content_Test_StoreB_DocA/PropA/*"; includeIdx2.Indexes.Add(new RangeIndex(DataType.String)); var col1 = new DocumentCollection(); col1.IndexingPolicy.IncludedPaths.Add(includeIdx1); col1.IndexingPolicy.IncludedPaths.Add(includeIdx2); _documentClient.ReadDocumentCollectionAsync(Arg.Any <Uri>()).Returns(WrapResource(col1)); var manager = new ServiceDbConfigManager("Test", _signatureGenerator); var foo = WrapResource(col1); _documentClient.ReplaceDocumentCollectionAsync(Arg.Any <DocumentCollection>(), Arg.Any <RequestOptions>()).Returns(foo); _documentClient.UpsertDocumentAsync( Arg.Any <Uri>(), Arg.Is <object>(r => ((ServiceDbConfigManager.ServiceConfigRecord)r).Signature == newSig), Arg.Any <RequestOptions>()) .Returns(WrapResource(CreateConfigDoc(configDoc.Id, newSig))); manager.RegisterStoreConfigSource(configSourceA); manager.RegisterStoreConfigSource(configSourceB); manager.EnsureConfigCurrent(_documentClient, dbConfig); await _documentClient.Received(1).UpsertDocumentAsync(Arg.Any <Uri>(), Arg.Any <object>(), Arg.Any <RequestOptions>()); await _documentClient.Received().ReplaceDocumentCollectionAsync( Arg.Is <DocumentCollection>(c => IncludedPathCheck( c.IndexingPolicy.IncludedPaths, "/content_Test_StoreA_DocA/*", "/content_Test_StoreB_DocA/*", "/content_Test_StoreB_DocB/*")), Arg.Any <RequestOptions>()); }