public void CanApplyIndexConvention()
        {
            var connection = new SqlConnection(sqlServerConnectionString);
             connection.Open();
             var processor = new SqlServerProcessor(connection, new SqlServerGenerator());

             var conventions = new MigrationConventions();
             var runner = new MigrationRunner(conventions, processor);

             runner.Up(new TestIndexNamingConvention());
             processor.TableExists("Users").ShouldBeTrue();

             runner.Down(new TestIndexNamingConvention());
             processor.TableExists("Users").ShouldBeFalse();
        }
        public void CanApplyForeignKeyConvention()
        {
            var connection = new SqlConnection(sqlServerConnectionString);
            connection.Open();
            var processor = new SqlServerProcessor(connection, new SqlServerGenerator());

             var conventions = new MigrationConventions();
             var runner = new MigrationRunner(conventions, processor);

             runner.Up(new TestForeignKeyNamingConvention());
             processor.TableExists("Users").ShouldBeTrue();
             processor.ConstraintExists( "Users", "FK_Users_GroupId_Groups_GroupId").ShouldBeTrue();

             runner.Down(new TestForeignKeyNamingConvention());
             processor.TableExists("Users").ShouldBeFalse();
        }
        public void CanReadLongViewDefinition()
        {
            // Arrange
             var create = new CreateTableExpression
             {
            TableName = "Foo",
            Columns =
               new List<ColumnDefinition> { new ColumnDefinition { Name = "Id", Type = DbType.Int32 } }
             };

             IList<ViewDefinition> views;

             var createSql = new StringBuilder();
             createSql.Append("CREATE VIEW FooView As SELECT Id,");
             createSql.Append("'");
             createSql.Append(new string('A', 3000));
             createSql.Append("'");
             createSql.Append(" As LongText1,");
             createSql.Append("'");
             createSql.Append(new string('B', 3000));
             createSql.Append("'");
             createSql.Append(" As LongText2");
             createSql.Append(" FROM Foo");

             // Act
             using (var connection = new SqlConnection(ConnectionString))
             {
            var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions());

            processor.Process(create);

            Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "SqlServer");

            processor.Execute(createSql.ToString());

            var dumper = new SqlServerSchemaDumper(processor, new DebugAnnouncer());
            views = dumper.ReadViewSchema();

            processor.CommitTransaction();
             }

             // Assert

             Assert.AreEqual(1, views.Count);
        }
        /// <summary>
        /// Creates tables in source oracle database and asserts that they exist
        /// </summary>
        /// <param name="expressions">The tables to be created</param>
        private void ExecuteMigrations(params IMigrationExpression[] expressions)
        {
            if (expressions == null)
            return;

             using (var connection = new SqlConnection(_source.ConnectionString))
             {
            var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions());

            foreach (var expression in expressions)
            {
                if (expression is CreateTableExpression)
                {
                    var create = (CreateTableExpression)expression;
                    processor.Process(create);
                    Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "Source " + create.TableName);
                }

                if (expression is CreateForeignKeyExpression)
                {
                    processor.Process((CreateForeignKeyExpression)expression);
                }

                if (expression is InsertDataExpression)
                {
                    processor.Process((InsertDataExpression)expression);
                }
            }

            processor.CommitTransaction();
             }
        }
        public void SqlServerMigrationsAreTransactional()
        {
            var connection = new SqlConnection(sqlServerConnectionString);
            connection.Open();
            var processor = new SqlServerProcessor(connection, new SqlServerGenerator());
            var runner = new MigrationVersionRunner(_conventions, processor, new MigrationLoader(_conventions), typeof(MigrationVersionRunnerTests).Assembly, typeof(InvalidMigration).Namespace);

            try
            {
                runner.MigrateUp();
            }
            catch
            {
            }

            processor.TableExists("Users").ShouldBeFalse();
        }
        /// <summary>
        /// Creates a single column table using the spplied type and retruns its <see cref="ColumnDefinition"/>
        /// </summary>
        /// <param name="type">The Sql Server data type to apply to the column</param>
        /// <returns>The translated <see cref="ColumnDefinition"/></returns>
        private TableDefinition GetTableColumnColumns(string createSql, string name, params IMigrationExpression[] expresions)
        {
            IList<TableDefinition> tables;

              // Act
              using (var connection = new SqlConnection(ConnectionString))
              {
              var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions());

              if (!string.IsNullOrEmpty(createSql))
                  processor.Execute(createSql);

              foreach (var expresion in expresions)
              {
                  if (expresion is CreateTableExpression)
                      processor.Process((CreateTableExpression)expresion);
                  if (expresion is CreateIndexExpression)
                      processor.Process((CreateIndexExpression)expresion);
                  if (expresion is CreateForeignKeyExpression)
                      processor.Process((CreateForeignKeyExpression)expresion);
              }

              Assert.IsTrue(processor.TableExists(string.Empty, name), "SqlServer");

              var dumper = new SqlServerSchemaDumper(processor, new DebugAnnouncer());
              tables = dumper.ReadDbSchema();

              processor.CommitTransaction();
              }

              if (!string.IsNullOrEmpty(createSql))
              tables.Count.ShouldBe(1);

              return tables.Where(t => t.Name == name).FirstOrDefault();
        }
        public void CallingProcessWithPerformDBOperationExpressionWhenInPreviewOnlyModeWillNotMakeDbChanges()
        {
            var output = new StringWriter();

            var connection = new SqlConnection(IntegrationTestOptions.SqlServer2012.ConnectionString);

            var processor = new SqlServerProcessor(
                connection,
                new SqlServer2012Generator(),
                new TextWriterAnnouncer(output),
                new ProcessorOptions { PreviewOnly = true },
                new SqlServerDbFactory());

            bool tableExists;

            try
            {
                var expression =
                    new PerformDBOperationExpression
                    {
                        Operation = (con, trans) =>
                        {
                            var command = con.CreateCommand();
                            command.CommandText = "CREATE TABLE ProcessTestTable (test int NULL) ";
                            command.Transaction = trans;

                            command.ExecuteNonQuery();
                        }
                    };

                processor.Process(expression);

                tableExists = processor.TableExists("", "ProcessTestTable");
            }
            finally
            {
                processor.RollbackTransaction();

            }

            tableExists.ShouldBeFalse();

            string fmOutput = output.ToString();
            Assert.That(fmOutput, Is.StringContaining("/* Beginning Transaction */"));
            Assert.That(fmOutput, Is.StringContaining("/* Performing DB Operation */"));
            Assert.That(fmOutput, Is.StringContaining("/* Rolling back transaction */"));
        }
        /// <summary>
        /// Creates tables in SQL Server and asserts that they exist
        /// </summary>
        /// <param name="createTables">The tables to be created</param>
        private void CreateTables(params CreateTableExpression[] createTables)
        {
            if (createTables == null)
            return;

             using (var connection = new SqlConnection(_sqlContext.ConnectionString))
             {
            var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions());

            foreach (var create in createTables)
            {
               processor.Process(create);
               Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "SqlServer " + create.TableName);
            }

            processor.CommitTransaction();
             }
        }
        /// <summary>
        /// Migrates tables from SQL Server to Oracle and queries the data in the new Oracle table
        /// </summary>
        /// <param name="expressions">The expressions to execute as part of the migration</param>
        /// <param name="expectedMigrations">The number of migrations that should be created</param>
        /// <param name="contextAction">Delegates that alter the context before the migration is executed</param>
        /// <returns>The data in the new Oracle table</returns>
        private DataSet MigrateToOracleWithData(IEnumerable<IMigrationExpression> expressions, int expectedMigrations, params Action<SchemaMigrationContext>[] contextAction)
        {
            var context = GetDefaultContext();
             context.GenerateAlternateMigrationsFor.Add(DatabaseType.Oracle);
             context.Type = context.Type | MigrationType.Data;

             if (contextAction != null)
             {
            foreach (var action in contextAction)
            {
               action(context);
            }
             }

             CreateTableExpression create = null;

             using (var connection = new SqlConnection(_sqlContext.ConnectionString))
             {
            var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions());

            foreach (var action in expressions)
            {
               if (action is CreateTableExpression)
               {
                  create = (CreateTableExpression) action;
                  processor.Process(create);
                  Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "SqlServer " + create.TableName);
                  continue;
               }

               if (action is InsertDataExpression)
               {
                  var insert = (InsertDataExpression)action;
                  processor.Process(insert);
                  continue;
               }

               if (action is CreateIndexExpression)
               {
                  var index = (CreateIndexExpression)action;
                  processor.Process(index);
                  continue;
               }

               if (action is CreateForeignKeyExpression)
               {
                   var index = (CreateForeignKeyExpression)action;
                   processor.Process(index);
                   continue;
               }
            }

            processor.CommitTransaction();
             }

             MigrateTable(context);

             if (create != null)
            AssertOracleTablesExist(create);

             context.MigrationIndex.ShouldBe(expectedMigrations);

             return create != null ? GetOracleTableData(create.TableName) : null;
        }
        public void SqlServerMigrationsAreTransactional()
        {
            var connection = new SqlConnection(IntegrationTestOptions.SqlServer.ConnectionString);
            connection.Open();
            var processor = new SqlServerProcessor(connection, new SqlServer2000Generator(), new TextWriterAnnouncer(System.Console.Out), new ProcessorOptions());
            var runner = new MigrationVersionRunner(_conventions, processor, new MigrationLoader(_conventions), typeof(MigrationVersionRunnerTests).Assembly, typeof(InvalidMigration).Namespace, new TextWriterAnnouncer(System.Console.Out));

            try
            {
                runner.MigrateUp();
            }
            catch
            {
            }

            processor.TableExists("Users").ShouldBeFalse();
        }
        public void CanReadMultipleViews()
        {
            // Arrange
             var create = new CreateTableExpression
             {
            TableName = "Foo",
            Columns =
               new List<ColumnDefinition> { new ColumnDefinition { Name = "Id", Type = DbType.Int32 } }
             };

             IList<ViewDefinition> views;

             // Act
             using (var connection = new SqlConnection(ConnectionString))
             {
            var processor = new SqlServerProcessor(connection, new SqlServer2005Generator(), new DebugAnnouncer(), new ProcessorOptions());

            processor.Process(create);

            Assert.IsTrue(processor.TableExists(string.Empty, create.TableName), "SqlServer");

            processor.Execute("CREATE VIEW FooViewC AS SELECT Id FROM Foo");
            processor.Execute("CREATE VIEW FooViewB AS SELECT Id FROM Foo");
            processor.Execute("CREATE VIEW FooViewA AS SELECT Id FROM Foo");

            var dumper = new SqlServerSchemaDumper(processor, new DebugAnnouncer());
            views = dumper.ReadViewSchema();

            processor.CommitTransaction();
             }

             // Assert

             Assert.AreEqual(3, views.Count);

             Assert.AreEqual("FooViewA", views[0].Name);
             Assert.AreEqual("FooViewB", views[1].Name);
             Assert.AreEqual("FooViewC", views[2].Name);
        }