Example #1
0
        private async Task CopyDataFromTableToCollectionAsync(string tableSchema, string tableName, bool sampleData = false)
        {
            var collectionName = GetCollectionName(tableSchema, tableName);

            _logger.LogInformation($"Copying data from `{tableSchema}.{tableName}` table to {collectionName} collection...");
            var counter = 0;

            ICouchbaseCollection collection;

            if (_config.UseSchemaForScope)
            {
                var scopeName = GetScopeName(tableSchema);
                var scope     = _bucket.Scope(scopeName);
                collection = scope.Collection(collectionName);
            }
            else
            {
                var scope = _bucket.DefaultScope();
                collection = scope.Collection(collectionName);
            }
            var rows = _sqlConnection.Query($@"
                SELECT {(sampleData ? "TOP 100" : "")} *
                FROM [{tableSchema}].[{tableName}]");

            _logger.LogInformation("Writing row(s)...");
            foreach (var row in rows)
            {
                string documentKey = null;
                try
                {
                    documentKey = await GetDocumentKeyFromPrimaryKeyValuesAsync(row, tableSchema, tableName);

                    await collection.UpsertAsync(documentKey, row);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"Error writing.");
                    _logger.LogError($"Row data: {row}");
                    _logger.LogError($"Document key: {documentKey}");
                    _logger.LogError($"Exception message: {ex.Message}");
                    _logger.LogError($"Exception stack trace: {ex.StackTrace}");
                }
                counter++;
                if ((counter % 1000) == 0)
                {
                    _logger.LogInformation(counter + "...");
                }
            }
            _logger.LogInformation("Done");
        }