public void Dispose_should_disable_tracing()
        {
            var mockCommandInterceptor = new Mock<IDbCommandInterceptor>();

            var commandTracer = new CommandTracer(mockCommandInterceptor.Object);

            commandTracer.Dispose();

            mockCommandInterceptor.VerifySet(i => i.IsEnabled = false);
        }
        public void Dispose_should_disable_tracing()
        {
            var mockCommandInterceptor = new Mock <IDbCommandInterceptor>();

            var commandTracer = new CommandTracer(mockCommandInterceptor.Object);

            commandTracer.Dispose();

            mockCommandInterceptor.VerifySet(i => i.IsEnabled = false);
        }
        public void Can_enable_tracing_and_return_commands()
        {
            var mockCommandInterceptor = new Mock<IDbCommandInterceptor>();

            var commandTracer = new CommandTracer(mockCommandInterceptor.Object);

            mockCommandInterceptor.VerifySet(i => i.IsEnabled = true);

            var interceptedCommands = new List<InterceptedCommand>();
            mockCommandInterceptor.SetupGet(i => i.Commands).Returns(interceptedCommands);

            Assert.Same(interceptedCommands, commandTracer.Commands);
        }
        public void Can_enable_tracing_and_return_commands()
        {
            var mockCommandInterceptor = new Mock <IDbCommandInterceptor>();

            var commandTracer = new CommandTracer(mockCommandInterceptor.Object);

            mockCommandInterceptor.VerifySet(i => i.IsEnabled = true);

            var interceptedCommands = new List <InterceptedCommand>();

            mockCommandInterceptor.SetupGet(i => i.Commands).Returns(interceptedCommands);

            Assert.Same(interceptedCommands, commandTracer.Commands);
        }
        public void Can_trace_command_trees()
        {
            using (var context = new TracerContext())
            {
                context.TracerEntities.Add(
                    new TracerEntity
                    {
                        Id = 1,
                        Name = "Pineapple Lumps"
                    });
                
                using (var commandTracer = new CommandTracer(context))
                {
                    context.SaveChanges();

                    var interceptedCommandTree = commandTracer.CommandTrees.Single();

                    Assert.IsType<DbInsertCommandTree>(interceptedCommandTree);
                }
            }
        }
Exemple #6
0
        public void Can_trace_command_trees()
        {
            using (var context = new TracerContext())
            {
                context.TracerEntities.Add(
                    new TracerEntity
                {
                    Id   = 1,
                    Name = "Pineapple Lumps"
                });

                using (var commandTracer = new CommandTracer(context))
                {
                    context.SaveChanges();

                    var interceptedCommandTree = commandTracer.CommandTrees.Single();

                    Assert.IsType <DbInsertCommandTree>(interceptedCommandTree);
                }
            }
        }
Exemple #7
0
        public void Can_trace_command_trees()
        {
            using (var context = new ChangeTrackingProxyTests.YummyContext())
            {
                context.Products.Add(
                    new ChangeTrackingProxyTests.YummyProduct
                    {
                        Id = 1,
                        Name = "Pineapple Lumps"
                    });
                
                using (var commandTracer = new CommandTracer(context))
                {
                    context.SaveChanges();

                    var interceptedCommandTree = commandTracer.CommandTrees.Single();

                    Assert.IsType<DbInsertCommandTree>(interceptedCommandTree);
                }
            }
        }
Exemple #8
0
        public void Can_trace_command_trees()
        {
            using (var context = new ChangeTrackingProxyTests.YummyContext())
            {
                context.Products.Add(
                    new ChangeTrackingProxyTests.YummyProduct
                {
                    Id   = 1,
                    Name = "Pineapple Lumps"
                });

                using (var commandTracer = new CommandTracer(context))
                {
                    context.SaveChanges();

                    var interceptedCommandTree = commandTracer.CommandTrees.Single();

                    Assert.IsType <DbInsertCommandTree>(interceptedCommandTree);
                }
            }
        }
Exemple #9
0
        public void Can_trace_commands_and_execution_cancelled()
        {
            using (var context = new ChangeTrackingProxyTests.YummyContext())
            {
                context.Products.Add(
                    new ChangeTrackingProxyTests.YummyProduct
                        {
                            Id = 1,
                            Name = "Pineapple Lumps"
                        });

                CommandTracer commandTracer;

                using (commandTracer = new CommandTracer(context))
                {
                    context.SaveChanges();

                    var interceptedCommand = commandTracer.DbCommands.Single();

                    Assert.Equal(
                        "INSERT [dbo].[YummyProducts]([Id], [Name])\r\nVALUES (@0, @1)\r\n",
                        interceptedCommand.CommandText);

                    Assert.Equal(2, interceptedCommand.Parameters.Count);
                }

                context.Products.Add(
                    new ChangeTrackingProxyTests.YummyProduct
                        {
                            Id = 2,
                            Name = "Orange squash gums"
                        });

                context.SaveChanges();

                Assert.Equal(1, commandTracer.DbCommands.Count());
                Assert.Null(context.Products.SingleOrDefault(p => p.Id == 1));
                Assert.NotNull(context.Products.SingleOrDefault(p => p.Id == 2));
            }
        }
Exemple #10
0
        public void Can_trace_commands_and_execution_cancelled()
        {
            using (var context = new ChangeTrackingProxyTests.YummyContext())
            {
                context.Products.Add(
                    new ChangeTrackingProxyTests.YummyProduct
                {
                    Id   = 1,
                    Name = "Pineapple Lumps"
                });

                CommandTracer commandTracer;

                using (commandTracer = new CommandTracer(context))
                {
                    context.SaveChanges();

                    var interceptedCommand = commandTracer.DbCommands.Single();

                    Assert.Equal(
                        "INSERT [dbo].[YummyProducts]([Id], [Name])\r\nVALUES (@0, @1)\r\n",
                        interceptedCommand.CommandText);

                    Assert.Equal(2, interceptedCommand.Parameters.Count);
                }

                context.Products.Add(
                    new ChangeTrackingProxyTests.YummyProduct
                {
                    Id   = 2,
                    Name = "Orange squash gums"
                });

                context.SaveChanges();

                Assert.Equal(1, commandTracer.DbCommands.Count());
                Assert.Null(context.Products.SingleOrDefault(p => p.Id == 1));
                Assert.NotNull(context.Products.SingleOrDefault(p => p.Id == 2));
            }
        }
        public void Can_trace_commands()
        {
            using (var context = new YummyContext())
            {
                context.Products.Add(
                    new YummyProduct
                {
                    Name = "Pineapple Lumps"
                });

                using (var commandTracer = new CommandTracer())
                {
                    context.SaveChanges();

                    var interceptedCommand = commandTracer.Commands.Single();

                    Assert.Equal(
                        "insert [dbo].[YummyProducts]([Id], [Name])\r\nvalues (@0, @1)\r\n",
                        interceptedCommand.CommandText);
                    Assert.Equal(2, interceptedCommand.Parameters.Count());
                }
            }
        }
        public void Can_trace_commands()
        {
            using (var context = new YummyContext())
            {
                context.Products.Add(
                    new YummyProduct
                        {
                            Name = "Pineapple Lumps"
                        });

                using (var commandTracer = new CommandTracer())
                {
                    context.SaveChanges();

                    var interceptedCommand = commandTracer.Commands.Single();

                    Assert.Equal(
                        "insert [dbo].[YummyProducts]([Id], [Name])\r\nvalues (@0, @1)\r\n",
                        interceptedCommand.CommandText);
                    Assert.Equal(2, interceptedCommand.Parameters.Count());
                }
            }
        }
Exemple #13
0
        public void Generate_can_output_delete_history_statement()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            using (var historyContext = new HistoryContext())
            {
                var historyRow
                    = new HistoryRow
                          {
                              MigrationId = "House Lannister",
                              ContextKey = "The pointy end"
                          };

                historyContext.History.Attach(historyRow);
                historyContext.History.Remove(historyRow);

                using (var commandTracer = new CommandTracer(historyContext))
                {
                    historyContext.SaveChanges();

                    var deleteHistoryOperation
                        = new HistoryOperation(commandTracer.CommandTrees.OfType<DbModificationCommandTree>().ToList());

                    var sql
                        = migrationSqlGenerator
                            .Generate(new[] { deleteHistoryOperation }, "4.0")
                            .Single();

                    Assert.Equal(@"DELETE [__MigrationHistory]
WHERE (([MigrationId] = N'House Lannister') AND ([ContextKey] = N'The pointy end'))", sql.Sql.Trim());
                }
            }
        }
Exemple #14
0
        public void Generate_can_output_insert_history_statement()
        {
            var migrationSqlGenerator = new SqlCeMigrationSqlGenerator();

            using (var historyContext = new HistoryContext())
            {
                historyContext.History.Add(
                    new HistoryRow
                        {
                            MigrationId = "House Lannister",
                            ContextKey = "The pointy end",
                            Model = new byte[0],
                            ProductVersion = "Awesomeness"
                        });

                using (var commandTracer = new CommandTracer(historyContext))
                {
                    historyContext.SaveChanges();

                    var insertHistoryOperation
                        = new HistoryOperation(commandTracer.CommandTrees.OfType<DbModificationCommandTree>().ToList());

                    var sql
                        = migrationSqlGenerator
                            .Generate(new[] { insertHistoryOperation }, "4.0")
                            .Single();

                    Assert.Equal(@"INSERT [__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'House Lannister', N'The pointy end',  0x , N'Awesomeness')", sql.Sql.Trim());
                }
            }
        }
        public virtual IEnumerable<DbQueryCommandTree> CreateDiscoveryQueryTrees()
        {
            DbConnection connection = null;
            try
            {
                connection = CreateConnection();

                foreach (var schema in _schemas)
                {
                    using (var context = CreateContext(connection, schema))
                    {
                        var query
                            = context.History
                                .Where(h => h.ContextKey == _contextKey)
                                .Select(s => s.MigrationId)
                                .OrderByDescending(s => s);

                        var dbQuery = query as DbQuery<string>;

                        if (dbQuery != null)
                        {
                            dbQuery.InternalQuery.ObjectQuery.EnablePlanCaching = false;
                        }

                        using (var commandTracer = new CommandTracer(context))
                        {
                            query.First();

                            var queryTree
                                = commandTracer
                                    .CommandTrees
                                    .OfType<DbQueryCommandTree>()
                                    .Single(t => t.DataSpace == DataSpace.SSpace);

                            yield return
                                new DbQueryCommandTree(
                                    queryTree.MetadataWorkspace,
                                    queryTree.DataSpace,
                                    queryTree.Query.Accept(
                                        new ParameterInliner(
                                            commandTracer.DbCommands.Single().Parameters)));
                        }
                    }
                }
            }
            finally
            {
                DisposeConnection(connection);
            }
        }
        public virtual MigrationOperation CreateDeleteOperation(string migrationId)
        {
            DebugCheck.NotEmpty(migrationId);

            DbConnection connection = null;
            try
            {
                connection = CreateConnection();

                using (var context = CreateContext(connection))
                {
                    var historyRow
                        = new HistoryRow
                            {
                                MigrationId = migrationId.RestrictTo(_migrationIdMaxLength),
                                ContextKey = _contextKey
                            };

                    context.History.Attach(historyRow);
                    context.History.Remove(historyRow);

                    using (var commandTracer = new CommandTracer(context))
                    {
                        context.SaveChanges();

                        return new HistoryOperation(
                            commandTracer.CommandTrees.OfType<DbModificationCommandTree>().ToList());
                    }
                }
            }
            finally
            {
                DisposeConnection(connection);
            }
        }
        public virtual MigrationOperation CreateInsertOperation(string migrationId, VersionedModel versionedModel)
        {
            DebugCheck.NotEmpty(migrationId);
            DebugCheck.NotNull(versionedModel);

            DbConnection connection = null;
            try
            {
                connection = CreateConnection();

                using (var context = CreateContext(connection))
                {
                    context.History.Add(
                        new HistoryRow
                            {
                                MigrationId = migrationId.RestrictTo(_migrationIdMaxLength),
                                ContextKey = _contextKey,
                                Model = new ModelCompressor().Compress(versionedModel.Model),
                                ProductVersion = versionedModel.Version ?? _productVersion
                            });

                    using (var commandTracer = new CommandTracer(context))
                    {
                        context.SaveChanges();

                        return new HistoryOperation(
                            commandTracer.CommandTrees.OfType<DbModificationCommandTree>().ToList());
                    }
                }
            }
            finally
            {
                DisposeConnection(connection);
            }
        }
        public virtual MigrationOperation CreateDeleteOperation(string migrationId)
        {
            DebugCheck.NotEmpty(migrationId);

            using (var connection = CreateConnection())
            {
                using (var context = CreateContext(connection))
                {
                    var historyRow
                        = new HistoryRow
                        {
                            MigrationId = migrationId,
                            ContextKey = _contextKey
                        };

                    context.History.Attach(historyRow);
                    context.History.Remove(historyRow);

                    using (var commandTracer = new CommandTracer(context))
                    {
                        context.SaveChanges();

                        return new HistoryOperation(commandTracer.DbCommands);
                    }
                }
            }
        }
        public virtual MigrationOperation CreateInsertOperation(string migrationId, XDocument model)
        {
            DebugCheck.NotEmpty(migrationId);
            DebugCheck.NotNull(model);

            using (var connection = CreateConnection())
            {
                using (var context = CreateContext(connection))
                {
                    context.History.Add(
                        new HistoryRow
                        {
                            MigrationId = migrationId,
                            ContextKey = _contextKey,
                            Model = new ModelCompressor().Compress(model),
                            ProductVersion = _productVersion
                        });

                    using (var commandTracer = new CommandTracer(context))
                    {
                        context.SaveChanges();

                        return new HistoryOperation(commandTracer.DbCommands);
                    }
                }
            }
        }