public static async Task <DocumentCollection> EnsureDatabaseAndCollection(this IDocumentClient client, string dbId, string collectionId, string[] partitionKeys = null) { var db = client.CreateDatabaseQuery().Where(d => d.Id == dbId).AsEnumerable().FirstOrDefault() ?? await client.CreateDatabaseAsync(new Database() { Id = dbId }); var collection = client.CreateDocumentCollectionQuery(db.SelfLink) .Where(c => c.Id == collectionId).AsEnumerable().FirstOrDefault(); if (collection == null) { var collectionDefinition = new DocumentCollection() { Id = collectionId }; if (partitionKeys != null && partitionKeys.Length > 0) { foreach (var partitionKey in partitionKeys) { collectionDefinition.PartitionKey.Paths.Add(partitionKey); } } collection = await client.CreateDocumentCollectionAsync(db.SelfLink, collectionDefinition); } return(collection); }
private async Task CreateDb() { var database = _documentClient.CreateDatabaseQuery() .Where(d => d.Id == _databaseId) .AsEnumerable() .FirstOrDefault(); Assert.NotNull( await _databaseProvider.CreateOrGetDb()); Assert.NotNull( this._documentClient.CreateDatabaseQuery() .Where(d => d.Id == _databaseId) .AsEnumerable() .FirstOrDefault()); }
/// <summary> /// Get or create (when required) database for the given identifier /// </summary> /// <param name="id">The database identifier</param> /// <returns>The instance of DocumentDB database</returns> public Database GetOrCreateDatabase(string id) { var db = _client.CreateDatabaseQuery().AsEnumerable().FirstOrDefault(d => d.Id == id); return(db ?? _client.CreateDatabaseAsync(new Database { Id = id }).Result); }
private async Task <Database> GetOrCreateDatabaseAsync() { var database = _client.CreateDatabaseQuery().Where(db => db.Id == _id).AsEnumerable().FirstOrDefault(); return(database != null ? database : await _client.CreateDatabaseAsync(new Database { Id = _id })); }
private async Task TruncateEnvironment() { var databases = _documentClient.CreateDatabaseQuery().AsEnumerable().ToList(); foreach (var database in databases) { await _documentClient.DeleteDatabaseAsync(database.SelfLink); } }
public void Migrate(IEnumerable <string> documentcollection) { var database = _configuration.GetSection("documentdb").GetSection("source").GetSection("database").Value; var db = _documentClient.CreateDatabaseQuery().ToList().FirstOrDefault(x => x.Id == database); var colls = _documentClient.CreateDocumentCollectionQuery(db.CollectionsLink).ToList(); var migratecolls = colls.Where(z => documentcollection.Contains(z.Id, StringComparer.InvariantCultureIgnoreCase)); Parse(migratecolls); }
/// <summary> /// Gets all databases in the document db account. /// </summary> /// <param name="feedOptions">Feed options</param> /// <returns>All databases in the account.</returns> public async Task <IReadOnlyList <Database> > GetAllDatabasesAsync(FeedOptions feedOptions = null) { IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false); using (IDocumentQuery <Database> query = client.CreateDatabaseQuery().AsDocumentQuery()) { return(await QueryDocumentsAsync(query, feedOptions).ConfigureAwait(false)); } }
private async Task <Database> GetOrCreateDatabaseAsync(bool createOnMissing) { var database = _client.CreateDatabaseQuery().Where(db => db.Id == _id).AsEnumerable().FirstOrDefault(); return(database != null ? database : createOnMissing ? await _client.CreateDatabaseAsync(new Database { Id = _id }) : throw new InvalidOperationException($"Database { _id } does not exist")); }
private static async Task DeleteDatabase(IDocumentClient client) { Console.WriteLine(); Console.WriteLine("#### Deleting Database ####"); Database database = client.CreateDatabaseQuery().AsEnumerable().First(); await client.DeleteDatabaseAsync(database.SelfLink); Console.WriteLine($"Database Id: {database.Id}; Resource Id: {database.ResourceId} deleted"); }
/// <summary> /// Gets a reference to a DocumentDB database, or creates if /// it does not already exist /// </summary> /// <param name="Client">The client.</param> /// <param name="databaseId">The database identifier.</param> /// <returns></returns> public static async Task <Database> GetOrCreateDatabaseAsync(IDocumentClient Client, string databaseId) { Database database = Client.CreateDatabaseQuery().Where(db => db.Id == databaseId).ToArray().FirstOrDefault(); if (database == null) { database = await Client.CreateDatabaseAsync(new Database { Id = databaseId }); } return(database); }
protected override void Handle(PingQuery request) { var value = _documentClient.CreateDatabaseQuery() .Where(d => d.Id == DocumentSettings.DatabaseName) .Select(d => 1) .AsEnumerable() .FirstOrDefault(); if (value == 0) { throw new Exception("Read store database ping failed"); } }
private static void ListDatabases(IDocumentClient client) { Console.WriteLine(">>> List Databases <<<"); List <Database> databases = client.CreateDatabaseQuery().ToList(); foreach (Database database in databases) { PrintDatabase(database); } Console.WriteLine($"Total databases: {databases.Count}"); }
private static async Task CreateDatabase(IDocumentClient client) { Console.WriteLine(); Console.WriteLine("#### Creating Database ####"); Database database = client.CreateDatabaseQuery().Where(db => db.Id == DatabaseId).ToArray() .FirstOrDefault() ?? await client.CreateDatabaseAsync(new Database() { Id = DatabaseId }); Console.WriteLine($"Database Id: {database.Id}; Resource Id: {database.ResourceId}"); }
private async Task <Database> GetOrCreateDatabaseAsync() { Database database = _client.CreateDatabaseQuery() .Where(db => db.Id == _databaseId).ToArray().FirstOrDefault(); if (database == null) { database = await _client.CreateDatabaseAsync( new Database { Id = _databaseId }); } return(database); }
/// <summary> /// Gets the database with specified id. /// </summary> /// <param name="dbId">Database id.</param> /// <param name="feedOptions">Request options</param> /// <returns>The Database.</returns> public async Task <Database> GetDatabaseAsync(string dbId, FeedOptions feedOptions = null) { Code.ExpectsNotNullOrWhiteSpaceArgument(dbId, nameof(dbId), TaggingUtilities.ReserveTag(0x2381b1ca /* tag_961hk */)); IDocumentClient client = await GetDocumentClientAsync().ConfigureAwait(false); using (IDocumentQuery <Database> query = client.CreateDatabaseQuery().Where(db => db.Id == dbId).AsDocumentQuery()) { IReadOnlyList <Database> dbs = await QueryDocumentsAsync(query, feedOptions).ConfigureAwait(false); return(dbs.FirstOrDefault()); } }
private static void GetDatabases(IDocumentClient client) { Console.WriteLine(); Console.WriteLine("#### Get Databases List ####"); var databases = client.CreateDatabaseQuery().ToList(); foreach (var database in databases) { Console.WriteLine($" Database Id: {database.Id}; Rid: {database.ResourceId}"); } Console.WriteLine(); Console.WriteLine($"Total databases: {databases.Count}"); }
public Task <Unit> Handle(PingQuery request, CancellationToken cancellationToken) { var value = _documentClient.CreateDatabaseQuery() .Where(d => d.Id == DocumentSettings.DatabaseName) .Select(d => 1) .AsEnumerable() .FirstOrDefault(); if (value == 0) { throw new Exception("Read store database ping failed"); } return(Task.FromResult(Unit.Value)); }
private async Task SetupDatabaseAsync() { var databaseId = _configuration.DatabaseId; var database = _client.CreateDatabaseQuery() .Where(db => db.Id == databaseId) .AsEnumerable() .FirstOrDefault(); if (database == null) { database = new Database { Id = databaseId }; await _client.CreateDatabaseAsync(database); } }
public async Task <bool> EnsureCreatedAsync(string databaseName) { var database = _documentClient.CreateDatabaseQuery() .Where(db => db.Id == databaseName) .ToArray() .FirstOrDefault(); if (database != null) { return(false); } database = await _documentClient.CreateDatabaseAsync( new Database { Id = databaseName }); return(database != null); }
public static async Task <Database> GetDatabase(string databaseName, IDocumentClient client, CancellationToken token) { while (true) { token.ThrowIfCancellationRequested(); var query = client.CreateDatabaseQuery().Where(db => db.Id == databaseName).AsDocumentQuery(); if (query.HasMoreResults) { var res = await query.ExecuteNextAsync <Database>(); if (res.Any()) { return(res.First()); } } await Task.Delay(1000, token); } }
public async Task ScaleDownAll() { var databases = _documentClient.CreateDatabaseQuery().AsEnumerable().ToList(); foreach (var database in databases) { var offer = await _documentClient.GetOfferFromSelfLinkAsync(database.SelfLink); if (offer != null) { await _documentClient.ScaleAsync(offer, _config.ScaleDownBatch, _config.MinThroughput, _config.MaxThroughput); } else { var collections = _documentClient.CreateDocumentCollectionQuery(database.SelfLink).ToList(); foreach (var collection in collections) { offer = await _documentClient.GetOfferFromSelfLinkAsync(collection.SelfLink); await _documentClient.ScaleAsync(offer, _config.ScaleDownBatch, _config.MinThroughput, _config.MaxThroughput); } } } }
public static Task <Database> GetOrAddDatabase(string databaseName, IDocumentClient client, CancellationToken token) { return(Policy .Handle <DocumentClientException>(e => { return true; }) .WaitAndRetryForeverAsync(attempt => TimeSpan.FromSeconds(1 << Math.Min(attempt, 3))) .ExecuteAsync(async() => { token.ThrowIfCancellationRequested(); var query = client.CreateDatabaseQuery().Where(db => db.Id == databaseName).AsDocumentQuery(); if (query.HasMoreResults) { var res = await query.ExecuteNextAsync <Database>(); if (res.Any()) { return res.First(); } } return (await client.CreateDatabaseAsync(new Database { Id = databaseName }).ConfigureAwait(false)).Resource; })); }