public MigrationState GetMigrationState()
        {
            var state = new MigrationState();

            try
            {
                using (var connection = new SqlConnection(_connectionString))
                {
                    var context   = new RunnerContext(_announcer);
                    var generator = GetMigrationGenerator(_syntax);
                    var processor = new SqlServerProcessor(connection, generator, _announcer, _options);
                    var runner    = new FluentMigrator.Runner.MigrationRunner(_assembly, context, processor);

                    state.Total = runner.MigrationLoader.Migrations.Keys.Count;

                    foreach (var mig in runner.MigrationLoader.Migrations.Keys)
                    {
                        if (runner.VersionLoader.VersionInfo.HasAppliedMigration(mig))
                        {
                            state.Completed++;
                        }
                        else
                        {
                            state.Pending++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                state.Exception = ex;
            }

            return(state);
        }
        public void Create(SqlServerProcessor processor, IEnumerable <string> columnDefinitions)
        {
            if (!string.IsNullOrEmpty(_schemaName) && !processor.SchemaExists(_schemaName))
            {
                using (var command = new SqlCommand(string.Format("CREATE SCHEMA {0}", _quoter.QuoteSchemaName(_schemaName)), Connection, Transaction))
                    command.ExecuteNonQuery();
            }

            var quotedObjectName = _quoter.QuoteTableName(Name, _schemaName);

            var sb = new StringBuilder();

            sb.AppendFormat("CREATE TABLE ");
            sb.Append(quotedObjectName);

            sb.Append("(");
            foreach (string definition in columnDefinitions)
            {
                sb.Append(definition);
                sb.Append(", ");
            }

            sb.Remove(sb.Length - 2, 2);
            sb.Append(")");

            using (var command = new SqlCommand(sb.ToString(), Connection, Transaction))
                command.ExecuteNonQuery();
        }
 public void SetUp()
 {
     Connection = new SqlConnection(IntegrationTestOptions.SqlServer2012.ConnectionString);
     Processor  = new SqlServerProcessor(Connection, new SqlServer2012Generator(), new TextWriterAnnouncer(System.Console.Out), new ProcessorOptions(), new SqlServerDbFactory());
     Connection.Open();
     Processor.BeginTransaction();
 }
        public void ObsoleteGetAutoScriptDownName()
        {
            var querySchema = new SqlServerProcessor(
                new[] { "SqlServer2016", "SqlServer" },
                null,
                null,
                null,
                new ProcessorOptions(),
                null);
            var assemblyCollection = new Mock <IAssemblyCollection>();

            assemblyCollection.SetupGet(c => c.Assemblies).Returns(new Assembly[0]);
            var context = new MigrationContext(querySchema, assemblyCollection.Object, null, null);
            var expr    = new ObsoleteAutoScriptMigrationFake();

            expr.GetDownExpressions(context);

            var expression = context.Expressions.Single();
            var processed  = (IAutoNameExpression)expression.Apply(ConventionSets.NoSchemaName);

            processed.AutoNames.ShouldNotBeNull();
            CollectionAssert.AreEqual(
                new[]
            {
                "Scripts.Down.20130508175300_ObsoleteAutoScriptMigrationFake_SqlServer2016.sql",
                "Scripts.Down.20130508175300_ObsoleteAutoScriptMigrationFake_SqlServer.sql",
                "Scripts.Down.20130508175300_ObsoleteAutoScriptMigrationFake_Generic.sql",
            },
                processed.AutoNames);
        }
Beispiel #5
0
 public void Setup()
 {
     Connection   = new SqlConnection(IntegrationTestOptions.SqlServer2008.ConnectionString);
     Processor    = new SqlServerProcessor(Connection, new SqlServer2008Generator(), new TextWriterAnnouncer(System.Console.Out), new ProcessorOptions(), new SqlServerDbFactory());
     SchemaDumper = new SqlServerSchemaDumper(Processor, new TextWriterAnnouncer(System.Console.Out));
     Connection.Open();
 }
Beispiel #6
0
        public SqlServerTestTable(SqlServerProcessor processor, string schemaName, params string[] columnDefinitions)
        {
            _schemaName = schemaName;
            Connection  = processor.Connection;
            Transaction = processor.Transaction;

            Name = "Table" + Guid.NewGuid().ToString("N");
            Create(columnDefinitions);
        }
Beispiel #7
0
        public SqlServerTestSequence(SqlServerProcessor processor, string schemaName, string sequenceName)
        {
            _schemaName = schemaName;
            Name        = sequenceName;

            Connection  = (SqlConnection)processor.Connection;
            Transaction = (SqlTransaction)processor.Transaction;
            Create();
        }
Beispiel #8
0
        public void VersionInfoCreationScriptsOnlyGeneratedOnceInPreviewMode()
        {
            if (!IntegrationTestOptions.SqlServer2008.IsEnabled)
            {
                return;
            }

            var connection       = new SqlConnection(IntegrationTestOptions.SqlServer2008.ConnectionString);
            var processorOptions = new ProcessorOptions {
                PreviewOnly = true
            };

            var outputSql = new StringWriter();
            var announcer = new TextWriterAnnouncer(outputSql)
            {
                ShowSql = true
            };

            var processor = new SqlServerProcessor(connection, new SqlServer2008Generator(), announcer, processorOptions, new SqlServerDbFactory());

            try
            {
                var asm           = typeof(MigrationRunnerTests).Assembly;
                var runnerContext = new RunnerContext(announcer)
                {
                    Namespace   = "FluentMigrator.Tests.Integration.Migrations",
                    PreviewOnly = true
                };

                var runner = new MigrationRunner(asm, runnerContext, processor);
                runner.MigrateUp(1, false);

                processor.CommitTransaction();

                string schemaName         = new TestVersionTableMetaData().SchemaName;
                var    schemaAndTableName = string.Format("\\[{0}\\]\\.\\[{1}\\]", schemaName, TestVersionTableMetaData.TABLENAME);

                var outputSqlString = outputSql.ToString();

                var createSchemaMatches = new Regex(string.Format("CREATE SCHEMA \\[{0}\\]", schemaName)).Matches(outputSqlString).Count;
                var createTableMatches  = new Regex("CREATE TABLE " + schemaAndTableName).Matches(outputSqlString).Count;
                var createIndexMatches  = new Regex("CREATE UNIQUE CLUSTERED INDEX \\[" + TestVersionTableMetaData.UNIQUEINDEXNAME + "\\] ON " + schemaAndTableName).Matches(outputSqlString).Count;
                var alterTableMatches   = new Regex("ALTER TABLE " + schemaAndTableName).Matches(outputSqlString).Count;

                System.Console.WriteLine(outputSqlString);

                createSchemaMatches.ShouldBe(1);
                createTableMatches.ShouldBe(1);
                alterTableMatches.ShouldBe(1);
                createIndexMatches.ShouldBe(1);
            }
            finally
            {
                CleanupTestSqlServerDatabase(connection, processor);
            }
        }
        public SqlServerTestTable(SqlServerProcessor processor, string schemaName, params string[] columnDefinitions)
        {
            _schemaName = schemaName;
            Connection  = (SqlConnection)processor.Connection;
            Transaction = (SqlTransaction)processor.Transaction;
            _quoter     = processor.Quoter;

            Name = "TestTable";
            Create(processor, columnDefinitions);
        }
        public SqlServerTestTable(string table, SqlServerProcessor processor, string schemaName, params string[] columnDefinitions)
        {
            this.schemaName = schemaName;
            Connection      = (SqlConnection)processor.Connection;
            Transaction     = (SqlTransaction)processor.Transaction;

            Name = table;

            Create(columnDefinitions);
        }
 public void SetUp()
 {
     if (!IntegrationTestOptions.SqlServer2012.IsEnabled)
     {
         Assert.Ignore();
     }
     Connection = new SqlConnection(IntegrationTestOptions.SqlServer2012.ConnectionString);
     Processor  = new SqlServerProcessor(new[] { "SqlServer2012" }, Connection, new SqlServer2012Generator(), new TextWriterAnnouncer(TestContext.Out), new ProcessorOptions(), new SqlServerDbFactory());
     Connection.Open();
     Processor.BeginTransaction();
 }
        public void Run()
        {
            using (var connection = new SqlConnection(_connectionString))
            {
                var context   = new RunnerContext(_announcer);
                var generator = GetMigrationGenerator(_syntax);
                var processor = new SqlServerProcessor(connection, generator, _announcer, _options);
                var runner    = new FluentMigrator.Runner.MigrationRunner(_assembly, context, processor);

                runner.MigrateUp();
            }
        }
Beispiel #13
0
        private static void ExecuteWithSqlServer(IntegrationTestOptions.DatabaseServerOptions serverOptions, TextWriterAnnouncer announcer, SqlServer2005Generator generator, Action <IMigrationProcessor> test, bool tryRollback)
        {
            using (var connection = new SqlConnection(serverOptions.ConnectionString))
            {
                var processor = new SqlServerProcessor(connection, generator, announcer, new ProcessorOptions(), new SqlServerDbFactory());
                test(processor);

                if (tryRollback && !processor.WasCommitted)
                {
                    processor.RollbackTransaction();
                }
            }
        }
Beispiel #14
0
        public void CallingProcessWithPerformDBOperationExpressionWhenInPreviewOnlyModeWillNotMakeDbChanges()
        {
            var output = new StringWriter();

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

            var processor = new SqlServerProcessor(
                connection,
                new SqlServer2008Generator(),
                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();

            output.ToString().ShouldBe(
                @"/* Beginning Transaction */
/* Performing DB Operation */
/* Rolling back transaction */
");
        }
Beispiel #15
0
        private static void CleanupTestSqlServerDatabase(SqlConnection connection, SqlServerProcessor origProcessor)
        {
            if (origProcessor.WasCommitted)
            {
                connection.Close();

                var             cleanupProcessor = new SqlServerProcessor(connection, new SqlServer2008Generator(), new TextWriterAnnouncer(System.Console.Out), new ProcessorOptions(), new SqlServerDbFactory());
                MigrationRunner cleanupRunner    = SetupMigrationRunner(cleanupProcessor);
                cleanupRunner.RollbackToVersion(0);
            }
            else
            {
                origProcessor.RollbackTransaction();
            }
        }
        MigrationRunner getRunner()
        {
            var connectionString = ConfigurationManager.ConnectionStrings["dahliaSQL"].ConnectionString;
            var connection       = new SqlConnection(connectionString);
            var assembly         = typeof(SqlMigrationService).Assembly;
            var ns        = typeof(SqlMigrationService).Namespace;
            var generator = new SqlServer2005Generator();
            var processor = new SqlServerProcessor(connection, generator, new NullAnnouncer(), new ProcessorOptions());

            return(new MigrationRunner(assembly, new RunnerContext(new NullAnnouncer())
            {
                Namespace = ns
            },
                                       processor));
        }
Beispiel #17
0
        public void GetAutoScriptDownName()
        {
            var querySchema = new SqlServerProcessor(new[] { "SqlServer2016", "SqlServer" }, null, null, null, null, null);
            var context     = new MigrationContext(querySchema, null, null, null);
            var expr        = new AutoScriptMigrationFake();

            expr.GetDownExpressions(context);

            var expression = context.Expressions.Single();
            var processed  = (IAutoNameExpression)expression.Apply(ConventionSets.NoSchemaName);

            processed.AutoNames.ShouldNotBeNull();
            CollectionAssert.AreEqual(
                new[]
            {
                "Scripts.Down.20130508175300_AutoScriptMigrationFake_SqlServer2016.sql",
                "Scripts.Down.20130508175300_AutoScriptMigrationFake_SqlServer.sql",
                "Scripts.Down.20130508175300_AutoScriptMigrationFake_Generic.sql",
            },
                processed.AutoNames);
        }
        protected static void ExecuteWithSqlServer(Action <IMigrationProcessor> test, IntegrationTestOptions.DatabaseServerOptions serverOptions, Boolean tryRollback)
        {
            if (!serverOptions.IsEnabled)
            {
                return;
            }

            var announcer = new TextWriterAnnouncer(System.Console.Out);

            announcer.Heading("Testing Migration against MS SQL Server");

            using (var connection = new SqlConnection(serverOptions.ConnectionString))
            {
                var processor = new SqlServerProcessor(connection, new SqlServer2000Generator(), announcer, new ProcessorOptions());
                test(processor);

                if (tryRollback && !processor.WasCommitted)
                {
                    processor.RollbackTransaction();
                }
            }
        }
Beispiel #19
0
        public void MigrateUpSpecificVersionWithSqlServerProcessorShouldCommitItsTransaction()
        {
            if (!IntegrationTestOptions.SqlServer2008.IsEnabled)
            {
                return;
            }

            var connection = new SqlConnection(IntegrationTestOptions.SqlServer2008.ConnectionString);
            var processor  = new SqlServerProcessor(connection, new SqlServer2008Generator(), new TextWriterAnnouncer(System.Console.Out), new ProcessorOptions(), new SqlServerDbFactory());

            MigrationRunner runner = SetupMigrationRunner(processor);

            runner.MigrateUp(1);

            try
            {
                processor.WasCommitted.ShouldBeTrue();
            }
            finally
            {
                CleanupTestSqlServerDatabase(connection, processor);
            }
        }
 public SqlServerSchemaDumper(SqlServerProcessor processor, IAnnouncer announcer)
 {
     Announcer = announcer;
     Processor = processor;
 }
 public SchemaDumpTests()
 {
     Connection   = new SqlConnection(IntegrationTestOptions.SqlServer.ConnectionString);
     Processor    = new SqlServerProcessor(Connection, new SqlServer2000Generator(), new TextWriterAnnouncer(System.Console.Out), new ProcessorOptions());
     SchemaDumper = new SqlServerSchemaDumper(Processor, new TextWriterAnnouncer(System.Console.Out));
 }
Beispiel #22
0
 public void SetUp()
 {
     Connection = new SqlConnection(IntegrationTestOptions.SqlServer.ConnectionString);
     Processor  = new SqlServerProcessor(Connection, new SqlServer2000Generator(), new TextWriterAnnouncer(System.Console.Out), new ProcessorOptions());
 }