public void CanUseCustomVersionInfo()
        {
            ExecuteWithSupportedProcessors(processor =>
            {
                var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations.Interleaved.Pass3" }, processor);

                IVersionTableMetaData tableMetaData = new TestVersionTableMetaData();

                //ensure table doesn't exist
                if (processor.TableExists(tableMetaData.SchemaName, tableMetaData.TableName))
                    runner.Down(new VersionMigration(tableMetaData));

                //ensure schema doesn't exist
                if (processor.SchemaExists(tableMetaData.SchemaName))
                    runner.Down(new VersionSchemaMigration(tableMetaData));

                runner.Up(new VersionSchemaMigration(tableMetaData));
                processor.SchemaExists(tableMetaData.SchemaName).ShouldBeTrue();

                runner.Up(new VersionMigration(tableMetaData));
                processor.TableExists(tableMetaData.SchemaName, tableMetaData.TableName).ShouldBeTrue();

                runner.Down(new VersionMigration(tableMetaData));
                processor.TableExists(tableMetaData.SchemaName, tableMetaData.TableName).ShouldBeFalse();

                runner.Down(new VersionSchemaMigration(tableMetaData));
                processor.SchemaExists(tableMetaData.SchemaName).ShouldBeFalse();
            }, true, typeof(SqliteProcessor));
        }
        public void CanApplyForeignKeyConventionWithSchema()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestForeignKeyNamingConventionWithSchema());

                    processor.ConstraintExists("TestSchema", "Users", "FK_Users_GroupId_Groups_GroupId").ShouldBeTrue();
                    runner.Down(new TestForeignKeyNamingConvention());
                }, false, typeof(SqliteProcessor));
        }
        public void CanAlterColumnWithSchema()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateSchema());

                    runner.Up(new TestCreateAndDropTableMigrationWithSchema());
                    processor.ColumnExists("TestSchema", "TestTable2", "Name2").ShouldBeTrue();

                    runner.Up(new TestAlterColumnWithSchema());
                    processor.ColumnExists("TestSchema", "TestTable2", "Name2").ShouldBeTrue();

                    runner.Down(new TestAlterColumnWithSchema());
                    processor.ColumnExists("TestSchema", "TestTable2", "Name2").ShouldBeTrue();

                    runner.Down(new TestCreateAndDropTableMigrationWithSchema());

                    runner.Down(new TestCreateSchema());
                }, true, new[] { typeof(SqliteProcessor), typeof(FirebirdProcessor) });
        }
        public void CanRunMigration()
        {
            ExecuteWithSupportedProcessors(processor =>
                {
                    var conventions = new MigrationConventions();

                    var runner = new MigrationRunner(conventions, processor);

                    runner.Up(new TestCreateAndDropTableMigration());
                    processor.TableExists("TestTable").ShouldBeTrue();

                    runner.Down(new TestCreateAndDropTableMigration());
                    processor.TableExists("TestTable").ShouldBeFalse();
                });
        }
        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 CanRunMigration()
        {
            ExecuteWithSupportedProcessors(processor =>
                {
                    var conventions = new MigrationConventions();

                    var runner = new MigrationRunner(conventions, processor, new TextWriterAnnouncer(System.Console.Out), new StopWatch());

                    runner.Up(new TestCreateAndDropTableMigration());
                    processor.TableExists("TestTable").ShouldBeTrue();

                    runner.Down(new TestCreateAndDropTableMigration());
                    processor.TableExists("TestTable").ShouldBeFalse();
                });
        }
        public void CanApplyIndexConvention()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var conventions = new MigrationConventions();
                    var runner = new MigrationRunner(conventions, processor, new TextWriterAnnouncer(System.Console.Out), new StopWatch());

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

                    runner.Down(new TestIndexNamingConvention());
                    processor.TableExists("Users").ShouldBeFalse();
                });
        }
        public void CanApplyIndexConvention()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    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 CanSilentlyFail()
        {
            var processor = new Mock<IMigrationProcessor>();
            processor.Setup(x => x.Process(It.IsAny<CreateForeignKeyExpression>())).Throws(new Exception("Error"));
            processor.Setup(x => x.Process(It.IsAny<DeleteForeignKeyExpression>())).Throws(new Exception("Error"));

            var conventions = new MigrationConventions();

            var runner = new MigrationRunner(conventions, processor.Object) { SilentlyFail = true };

            runner.Up(new TestForeignKeySilentFailure());
            runner.CaughtExceptions.Count.ShouldBeGreaterThan(0);

            runner.Down(new TestForeignKeySilentFailure());
            runner.CaughtExceptions.Count.ShouldBeGreaterThan(0);
        }
        public void CanApplyIndexConvention()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

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

                    runner.Down(new TestIndexNamingConvention());
                    processor.TableExists("Users").ShouldBeFalse();

                    //processor.CommitTransaction();
                });
        }
        public void CanApplyForeignKeyConvention()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    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 CanApplyForeignKeyConvention()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestForeignKeyNamingConvention());

                    // This is a hack until MigrationVersionRunner and MigrationRunner are refactored and merged together
                    //processor.CommitTransaction();

                    processor.ConstraintExists( "Users", "FK_Users_GroupId_Groups_GroupId" ).ShouldBeTrue();
                    runner.Down( new TestForeignKeyNamingConvention() );
                });
        }
        public void CanUseVersionInfo()
        {
            ExecuteWithSupportedProcessors(processor =>
                {
                    var runner = new MigrationRunner(new MigrationConventions(), processor);

                    //ensure table doesn't exist
                    if (processor.TableExists(VersionInfo.TABLE_NAME))
                        runner.Down(new VersionMigration());

                    runner.Up(new VersionMigration());
                    processor.TableExists(VersionInfo.TABLE_NAME).ShouldBeTrue();

                    runner.Down(new VersionMigration());
                    processor.TableExists(VersionInfo.TABLE_NAME).ShouldBeFalse();
                });
        }
        public void CanRunMigration()
        {
            ExecuteWithSupportedProcessors(processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateAndDropTableMigration());

                    processor.TableExists(null, "TestTable").ShouldBeTrue();

                    // This is a hack until MigrationVersionRunner and MigrationRunner are refactored and merged together
                    //processor.CommitTransaction();

                    runner.Down(new TestCreateAndDropTableMigration());
                    processor.TableExists(null, "TestTable").ShouldBeFalse();
                });
        }
        public void DoesNotCreatesViewOnSqlLite()
        {
            ExecuteWithSqlite(processor =>
             {
            var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations.ScriptMigration" }, processor);

            runner.Up(new SqlServerScriptMigration());

            try
            {
               processor.Exists("SELECT * FROM Foo");
            }
            catch (SQLiteException ex)
            {
               Assert.IsTrue(ex.Message.Contains("no such table: Foo"));
            }

             }, IntegrationTestOptions.SqlLite);
        }
        public void CanUseVersionInfo()
        {
            ExecuteWithSupportedProcessors(processor =>
                {
                    var runner = new MigrationRunner(new MigrationConventions(), processor);

                    IVersionTableMetaData tableMetaData = new DefaultVersionTableMetaData();

                    //ensure table doesn't exist
                    if (processor.TableExists(tableMetaData.TableName))
                        runner.Down(new VersionMigration(tableMetaData));

                    runner.Up(new VersionMigration(tableMetaData));
                    processor.TableExists(tableMetaData.TableName).ShouldBeTrue();

                    runner.Down(new VersionMigration(tableMetaData));
                    processor.TableExists(tableMetaData.TableName).ShouldBeFalse();
                });
        }
        public void CreatesViewOnSqlServer()
        {
            ExecuteWithSqlServer(processor =>
             {
            var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), new RunnerContext(new TextWriterAnnouncer(System.Console.Out)) { Namespace = "FluentMigrator.Tests.Integration.Migrations.SqlServerScriptMigration" }, processor);

            // First check that view does not exist
            try
            {
               processor.ReadTableData(string.Empty, "Foo");
               // .. it does remove it
               runner.Down(new SqlServerScriptMigration());
            }
            catch (SqlException ex){
               // Not found .. proceed with test
               Assert.IsTrue(ex.Message.Contains("Invalid object name 'Foo'"));
            }

            runner.Up(new SqlServerScriptMigration());
            processor.Exists("SELECT * FROM Foo").ShouldBeTrue();

            runner.Down(new SqlServerScriptMigration());
             }, IntegrationTestOptions.SqlServer, true);
        }
        public void CanReverseCreateUniqueConstraintWithSchema()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateSchema());

                    runner.Up(new TestCreateAndDropTableMigrationWithSchema());

                    runner.Up(new TestCreateUniqueConstraintWithSchemaWithReversing());
                    processor.ConstraintExists("TestSchema", "TestTable2", "TestUnique").ShouldBeTrue();

                    runner.Down(new TestCreateUniqueConstraintWithSchemaWithReversing());
                    processor.ConstraintExists("TestSchema", "TestTable2", "TestUnique").ShouldBeFalse();

                    runner.Down(new TestCreateAndDropTableMigrationWithSchema());

                    runner.Down(new TestCreateSchema());

                }, true, new[] { typeof(SqliteProcessor) });
        }
        public void CanRenameTableWithSchema()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateSchema());

                    runner.Up(new TestCreateAndDropTableMigrationWithSchema());
                    processor.TableExists("TestSchema", "TestTable2").ShouldBeTrue();

                    runner.Up(new TestRenameTableMigrationWithSchema());
                    processor.TableExists("TestSchema", "TestTable2").ShouldBeFalse();
                    processor.TableExists("TestSchema", "TestTable'3").ShouldBeTrue();

                    runner.Down(new TestRenameTableMigrationWithSchema());
                    processor.TableExists("TestSchema", "TestTable'3").ShouldBeFalse();
                    processor.TableExists("TestSchema", "TestTable2").ShouldBeTrue();

                    runner.Down(new TestCreateAndDropTableMigrationWithSchema());
                    processor.TableExists("TestSchema", "TestTable2").ShouldBeFalse();

                    runner.Down(new TestCreateSchema());

                    //processor.CommitTransaction();
                });
        }
        public void CanRenameColumn()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateAndDropTableMigration());
                    processor.ColumnExists(null, "TestTable2", "Name").ShouldBeTrue();

                    runner.Up(new TestRenameColumnMigration());
                    processor.ColumnExists(null, "TestTable2", "Name").ShouldBeFalse();
                    processor.ColumnExists(null, "TestTable2", "Name'3").ShouldBeTrue();

                    runner.Down(new TestRenameColumnMigration());
                    processor.ColumnExists(null, "TestTable2", "Name'3").ShouldBeFalse();
                    processor.ColumnExists(null, "TestTable2", "Name").ShouldBeTrue();

                    runner.Down(new TestCreateAndDropTableMigration());
                    processor.ColumnExists(null, "TestTable2", "Name").ShouldBeFalse();
                }, true, typeof(SqliteProcessor));
        }
        public void CanInsertData()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateAndDropTableMigration());
                    DataSet ds = processor.ReadTableData(null, "TestTable");
                    ds.Tables[0].Rows.Count.ShouldBe(1);
                    ds.Tables[0].Rows[0][1].ShouldBe("Test");

                    runner.Down(new TestCreateAndDropTableMigration());

                }, true, new[] { typeof(SqliteProcessor) });
        }
        public void CanExecuteSql()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestExecuteSql());
                    runner.Down(new TestExecuteSql());

                }, true, new[] { typeof(FirebirdProcessor) });
        }
        public void CanCreateSequenceWithSchema()
        {
            Action<IMigrationProcessor> action = processor =>
                                {
                                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                                    runner.Up(new TestCreateSequence());
                                    processor.SequenceExists("TestSchema", "TestSequence");

                                    runner.Down(new TestCreateSequence());
                                    processor.SequenceExists("TestSchema", "TestSequence").ShouldBeFalse();
                                };

            ExecuteWithSqlServer2012(
                action,true);

            ExecuteWithPostgres(action, IntegrationTestOptions.Postgres, true);
        }
        public void CanCreateAndDropIndexWithSchema()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateSchema());

                    runner.Up(new TestCreateAndDropTableMigrationWithSchema());
                    processor.IndexExists("TestSchema", "TestTable", "IX_TestTable_Name").ShouldBeFalse();

                    runner.Up(new TestCreateAndDropIndexMigrationWithSchema());
                    processor.IndexExists("TestSchema", "TestTable", "IX_TestTable_Name").ShouldBeTrue();

                    runner.Down(new TestCreateAndDropIndexMigrationWithSchema());
                    processor.IndexExists("TestSchema", "TestTable", "IX_TestTable_Name").ShouldBeFalse();

                    runner.Down(new TestCreateAndDropTableMigrationWithSchema());
                    processor.IndexExists("TestSchema", "TestTable", "IX_TestTable_Name").ShouldBeFalse();

                    runner.Down(new TestCreateSchema());
                    //processor.CommitTransaction();
                }, false, new[] { typeof(SqliteProcessor), typeof(FirebirdProcessor) });
        }
        public void CanCreateAndDropIndex()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateAndDropTableMigration());
                    processor.IndexExists(null, "TestTable", "IX_TestTable_Name").ShouldBeFalse();

                    runner.Up(new TestCreateAndDropIndexMigration());
                    processor.IndexExists(null, "TestTable", "IX_TestTable_Name").ShouldBeTrue();

                    runner.Down(new TestCreateAndDropIndexMigration());
                    processor.IndexExists(null, "TestTable", "IX_TestTable_Name").ShouldBeFalse();

                    runner.Down(new TestCreateAndDropTableMigration());
                    processor.IndexExists(null, "TestTable", "IX_TestTable_Name").ShouldBeFalse();

                    //processor.CommitTransaction();
                });
        }
        public void CanApplyIndexConventionWithSchema()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestIndexNamingConventionWithSchema());
                    processor.IndexExists("TestSchema", "Users", "IX_Users_GroupId").ShouldBeTrue();
                    processor.TableExists("TestSchema", "Users").ShouldBeTrue();

                    runner.Down(new TestIndexNamingConventionWithSchema());
                    processor.IndexExists("TestSchema", "Users", "IX_Users_GroupId").ShouldBeFalse();
                    processor.TableExists("TestSchema", "Users").ShouldBeFalse();
                });
        }
        public void CanSilentlyFail()
        {
            try
            {
                var processorOptions = new Mock<IMigrationProcessorOptions>();
                processorOptions.SetupGet(x => x.PreviewOnly).Returns(false);

                var processor = new Mock<IMigrationProcessor>();
                processor.Setup(x => x.Process(It.IsAny<CreateForeignKeyExpression>())).Throws(new Exception("Error"));
                processor.Setup(x => x.Process(It.IsAny<DeleteForeignKeyExpression>())).Throws(new Exception("Error"));
                processor.Setup(x => x.Options).Returns(processorOptions.Object);

                var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor.Object) { SilentlyFail = true };

                runner.Up(new TestForeignKeySilentFailure());

                runner.CaughtExceptions.Count.ShouldBeGreaterThan(0);

                runner.Down(new TestForeignKeySilentFailure());
                runner.CaughtExceptions.Count.ShouldBeGreaterThan(0);
            }
            finally
            {
                ExecuteWithSupportedProcessors(processor =>
                {
                    MigrationRunner testRunner = SetupMigrationRunner(processor);
                    testRunner.RollbackToVersion(0);
                }, false);
            }
        }
        public void CanUpdateData()
        {
            ExecuteWithSupportedProcessors(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateSchema());

                    runner.Up(new TestCreateAndDropTableMigrationWithSchema());

                    runner.Up(new TestUpdateData());
                    DataSet upDs = processor.ReadTableData("TestSchema", "TestTable");
                    upDs.Tables[0].Rows.Count.ShouldBe(1);
                    upDs.Tables[0].Rows[0][1].ShouldBe("Updated");

                    runner.Down(new TestUpdateData());
                    DataSet downDs = processor.ReadTableData("TestSchema", "TestTable");
                    downDs.Tables[0].Rows.Count.ShouldBe(1);
                    downDs.Tables[0].Rows[0][1].ShouldBe("Test");

                    runner.Down(new TestCreateAndDropTableMigrationWithSchema());

                    runner.Down(new TestCreateSchema());
                }, true, new[] { typeof(SqliteProcessor), typeof(FirebirdProcessor) });
        }
        public void CanCreateSequence()
        {
            ExecuteWithSqlServer2012(
                processor =>
                {
                    var runner = new MigrationRunner(Assembly.GetExecutingAssembly(), _runnerContext, processor);

                    runner.Up(new TestCreateSequence());
                    processor.SequenceExists(null, "TestSequence");

                    runner.Down(new TestCreateSequence());
                    processor.SequenceExists(null, "TestSequence").ShouldBeFalse();
                }, true);
        }