Example #1
0
        private async Task UpdateContainers(IEnumerable <BigBangContainer> containersToUpdate, Database cloudDatabase,
                                            string directoryFullPath)
        {
            foreach (var container in containersToUpdate)
            {
                var containerToUpdate = cloudDatabase.GetContainer(container.Id);

                if (container.Throughput.HasValue)
                {
                    await containerToUpdate.ReplaceThroughputAsync(container.Throughput.Value);
                }

                var cosmosContainerSettings = new ContainerProperties(containerToUpdate.Id, container.PartitionKey)
                {
                    DefaultTimeToLive = Convert.ToInt32(TimeSpan.FromSeconds(container.DefaultTimeToLive).TotalSeconds),
                    UniqueKeyPolicy   = container.UniqueKeyPolicy ?? new UniqueKeyPolicy(),
                    IndexingPolicy    = container.IndexingPolicy ??
                                        IndexingPolicyExtentions.CreateDefaultIndexingPolicy()
                };

                _logger.LogInformation($"Updating container {container.Id}");

                await containerToUpdate.ReplaceContainerAsync(cosmosContainerSettings);

                _logger.LogInformation("Checking which stored procedures currently exist");

                var currentStoredProcedures = (await containerToUpdate.Scripts
                                               .GetStoredProcedureQueryIterator <dynamic>()
                                               .GetAll())
                                              .Select(c => c.Id)
                                              .ToList();

                //foreach (var file in container.StoredProcedures)
                //{
                //    var id = file.Replace(".js", "");
                //    if (currentStoredProcedures.Contains(id))
                //    {
                //        _logger.LogInformation($"Replacing stored procedure {file}");

                //        var storedProc = containerToUpdate.StoredProcedures[id];
                //        await storedProc.ReplaceAsync(await File.ReadAllTextAsync(Path.Combine(directoryFullPath, file)));
                //    }
                //    else
                //    {
                //        _logger.LogInformation($"Creating stored procedure {file}");

                //        await containerToUpdate.StoredProcedures.CreateStoredProcedureAsync(id,
                //            await File.ReadAllTextAsync(Path.Combine(directoryFullPath, file)));
                //    }
                //}

                //foreach (var id in currentStoredProcedures.Except(container.StoredProcedures))
                //{
                //    _logger.LogInformation($"Deleting stored procedure {id}");
                //    await containerToUpdate.StoredProcedures[id].DeleteAsync();
                //}
            }
        }
Example #2
0
        private async Task UpdateContainers(IEnumerable <BigBangContainer> containersToUpdate,
                                            Database cloudDatabase,
                                            bool updateThroughput,
                                            string directoryFullPath)
        {
            foreach (var container in containersToUpdate)
            {
                var containerToUpdate = cloudDatabase.GetContainer(container.Id);

                if (updateThroughput && container.Throughput.HasValue)
                {
                    await containerToUpdate.ReplaceThroughputAsync(container.Throughput.Value);
                }

                var cosmosContainerSettings = new ContainerProperties(containerToUpdate.Id, container.PartitionKey)
                {
                    DefaultTimeToLive = Convert.ToInt32(TimeSpan.FromSeconds(container.DefaultTimeToLive).TotalSeconds),
                    UniqueKeyPolicy   = container.UniqueKeyPolicy ?? new UniqueKeyPolicy(),
                    IndexingPolicy    = container.IndexingPolicy ??
                                        IndexingPolicyExtentions.CreateDefaultIndexingPolicy()
                };

                _logger.LogInformation($"Updating container {container.Id}");

                await containerToUpdate.ReplaceContainerAsync(cosmosContainerSettings);

                _logger.LogInformation("Checking which stored procedures currently exist");

                var currentStoredProcedures = (await containerToUpdate.Scripts
                                               .GetStoredProcedureQueryIterator <dynamic>()
                                               .GetAll())
                                              .Select(c => c.Id)
                                              .ToList();

                foreach (var file in container.StoredProcedures)
                {
                    var id = file.Replace(".js", "");
                    if (currentStoredProcedures.Contains(id))
                    {
                        _logger.LogInformation($"Replacing stored procedure {file}");
                        await containerToUpdate.Scripts.ReplaceStoredProcedureAsync(await GetStoredProcedurePropertiesFromPath(directoryFullPath, file));
                    }
                    else
                    {
                        _logger.LogInformation($"Creating stored procedure {file}");
                        await containerToUpdate.Scripts.CreateStoredProcedureAsync(await GetStoredProcedurePropertiesFromPath(directoryFullPath, file));
                    }
                }

                foreach (var id in currentStoredProcedures.Except(container.StoredProcedures))
                {
                    _logger.LogInformation($"Deleting stored procedure {id}");
                    await containerToUpdate.Scripts.DeleteStoredProcedureAsync(id);
                }

                var currentUserDefinedFuncs = (await containerToUpdate.Scripts
                                               .GetUserDefinedFunctionQueryIterator <dynamic>()
                                               .GetAll())
                                              .Select(c => c.Id)
                                              .ToList();

                foreach (var file in container.UserDefinedFunctions)
                {
                    var id = file.Replace(".js", "");
                    if (currentUserDefinedFuncs.Contains(id))
                    {
                        _logger.LogInformation($"Replacing user defined function {file}");
                        await containerToUpdate.Scripts.ReplaceUserDefinedFunctionAsync(await GetUserDefinedFunctionPropertiesFromPath(directoryFullPath, file));
                    }
                    else
                    {
                        _logger.LogInformation($"Creating user defined function  {file}");
                        await containerToUpdate.Scripts.CreateUserDefinedFunctionAsync(await GetUserDefinedFunctionPropertiesFromPath(directoryFullPath, file));
                    }
                }

                foreach (var id in currentUserDefinedFuncs.Except(container.UserDefinedFunctions))
                {
                    _logger.LogInformation($"Deleting user defined function {id}");
                    await containerToUpdate.Scripts.DeleteUserDefinedFunctionAsync(id);
                }
            }
        }