Example #1
0
        public static SqlEtlSimulationResult SimulateSqlEtl(SimulateSqlEtl simulateSqlEtl, DocumentDatabase database, ServerStore serverStore, DocumentsOperationContext context)
        {
            var document = database.DocumentsStorage.Get(context, simulateSqlEtl.DocumentId);

            if (document == null)
            {
                throw new InvalidOperationException($"Document {simulateSqlEtl.DocumentId} does not exist");
            }

            if (serverStore.LoadDatabaseRecord(database.Name, out _).SqlConnectionStrings.TryGetValue(simulateSqlEtl.Configuration.ConnectionStringName, out var connectionString) == false)
            {
                throw new InvalidOperationException($"Connection string named {simulateSqlEtl.Configuration.ConnectionStringName} was not found in the database record");
            }

            simulateSqlEtl.Configuration.Initialize(connectionString);

            if (simulateSqlEtl.Configuration.Validate(out List <string> errors) == false)
            {
                throw new InvalidOperationException($"Invalid ETL configuration for '{simulateSqlEtl.Configuration.Name}'. " +
                                                    $"Reason{(errors.Count > 1 ? "s" : string.Empty)}: {string.Join(";", errors)}.");
            }

            // TODO arek - those constraints can be changed later on

            if (simulateSqlEtl.Configuration.Transforms.Count != 1)
            {
                throw new InvalidOperationException($"Invalid number of transformations. You have provided {simulateSqlEtl.Configuration.Transforms.Count} " +
                                                    "while SQL ETL simulation expects to get exactly 1 transformation script");
            }

            if (simulateSqlEtl.Configuration.Transforms[0].Collections.Count != 1)
            {
                throw new InvalidOperationException($"Invalid number of collections specified in the transformation script. You have provided {simulateSqlEtl.Configuration.Transforms[0].Collections.Count} " +
                                                    "while SQL ETL simulation is supposed to work with exactly 1 collection");
            }

            using (var etl = new SqlEtl(simulateSqlEtl.Configuration.Transforms[0], simulateSqlEtl.Configuration, database, null))
            {
                etl.EnsureThreadAllocationStats();

                var collection  = simulateSqlEtl.Configuration.Transforms[0].Collections[0];
                var transformed = etl.Transform(new[] { new ToSqlItem(document, collection) }, context, new EtlStatsScope(new EtlRunStats()), new EtlProcessState());

                return(etl.Simulate(simulateSqlEtl, context, transformed));
            }
        }
Example #2
0
        public SqlEtlSimulationResult Simulate(SimulateSqlEtl simulateSqlEtl, DocumentsOperationContext context, IEnumerable <SqlTableWithRecords> toWrite)
        {
            var summaries = new List <TableQuerySummary>();

            if (simulateSqlEtl.PerformRolledBackTransaction)
            {
                using (var writer = new RelationalDatabaseWriter(this, Database))
                {
                    foreach (var records in toWrite)
                    {
                        var commands = new List <DbCommand>();

                        writer.Write(records, commands, CancellationToken);

                        summaries.Add(TableQuerySummary.GenerateSummaryFromCommands(records.TableName, commands));
                    }

                    writer.Rollback();
                }
            }
            else
            {
                var simulatedwriter = new RelationalDatabaseWriterSimulator(Configuration);

                foreach (var records in toWrite)
                {
                    var commands = simulatedwriter.SimulateExecuteCommandText(records, CancellationToken).Select(x => new TableQuerySummary.CommandData
                    {
                        CommandText = x
                    }).ToArray();

                    summaries.Add(new TableQuerySummary
                    {
                        TableName = records.TableName,
                        Commands  = commands
                    });
                }
            }

            return(new SqlEtlSimulationResult
            {
                LastAlert = Statistics.LastAlert,
                Summary = summaries
            });
        }