public void SqlBatchBuilder_correctly_groups_multiple_statements_into_one_batch()
        {
            var batchBuilder = new SqlBatchBuilder();
            batchBuilder.AppendLine("Statement1");
            batchBuilder.AppendLine("Statement2");
            batchBuilder.AppendLine("Statement3");
            batchBuilder.EndBatch();

            Assert.Equal(1, batchBuilder.SqlBatches.Count);
            Assert.Equal(
                @"Statement1
Statement2
Statement3
", batchBuilder.SqlBatches[0].Sql);
        }
        public void SqlBatchBuilder_correctly_produces_multiple_batches()
        {
            var batchBuilder = new SqlBatchBuilder();
            batchBuilder.AppendLine("Statement1");
            batchBuilder.EndBatch();
            batchBuilder.AppendLine("Statement2");
            batchBuilder.AppendLine("Statement3");
            batchBuilder.EndBatch();
            batchBuilder.AppendLine("Statement4");
            batchBuilder.AppendLine("Statement5");
            batchBuilder.AppendLine("Statement6");
            batchBuilder.EndBatch();

            Assert.Equal(3, batchBuilder.SqlBatches.Count);

            Assert.Equal(
                @"Statement1
", batchBuilder.SqlBatches[0].Sql);

            Assert.Equal(
                @"Statement2
Statement3
", batchBuilder.SqlBatches[1].Sql);

            Assert.Equal(
                @"Statement4
Statement5
Statement6
", batchBuilder.SqlBatches[2].Sql);
        }
        public void SqlBatchBuilder_ignores_empty_batches()
        {
            var batchBuilder = new SqlBatchBuilder();
            batchBuilder.AppendLine("Statement1");
            batchBuilder.EndBatch();
            batchBuilder.EndBatch();
            batchBuilder.EndBatch();
            batchBuilder.AppendLine("Statement2");
            batchBuilder.AppendLine("Statement3");
            batchBuilder.EndBatch();
            batchBuilder.EndBatch();

            Assert.Equal(2, batchBuilder.SqlBatches.Count);

            Assert.Equal(
                @"Statement1
", batchBuilder.SqlBatches[0].Sql);

            Assert.Equal(
                @"Statement2
Statement3
", batchBuilder.SqlBatches[1].Sql);
        }
        public virtual IReadOnlyList<SqlBatch> Generate(
            IReadOnlyList<MigrationOperation> operations,
            IModel model = null)
        {
            Check.NotNull(operations, nameof(operations));

            var builder = new SqlBatchBuilder();
            foreach (var operation in operations)
            {
                Generate(operation, model, builder);
                builder.AppendLine(Sql.BatchCommandSeparator);
            }

            builder.EndBatch();

            return builder.SqlBatches;
        }
        protected override void Generate(RenameSequenceOperation operation, IModel model, SqlBatchBuilder builder)
        {
            Check.NotNull(operation, nameof(operation));
            Check.NotNull(builder, nameof(builder));

            var separate = false;
            var name = operation.Name;
            if (operation.NewName != null)
            {
                var qualifiedName = new StringBuilder();
                if (operation.Schema != null)
                {
                    qualifiedName
                        .Append(operation.Schema)
                        .Append(".");
                }
                qualifiedName.Append(operation.Name);

                Rename(qualifiedName.ToString(), operation.NewName, builder);

                separate = true;
                name = operation.NewName;
            }

            if (operation.NewSchema != null)
            {
                if (separate)
                {
                    builder.AppendLine(Sql.BatchCommandSeparator);
                }

                Transfer(operation.NewSchema, operation.Schema, name, builder);
            }
        }
        public void SqlBatchBuilder_correctly_splits_statements_to_multiple_batches_when_transaction_is_suppressed()
        {
            var batchBuilder = new SqlBatchBuilder();
            batchBuilder.AppendLine("Statement1");
            batchBuilder.AppendLine("Statement2");
            batchBuilder.AppendLine("Statement3", suppressTransaction: true);
            batchBuilder.AppendLine("Statement4");
            batchBuilder.AppendLine("Statement5");
            batchBuilder.AppendLine("Statement6", suppressTransaction: true);
            batchBuilder.AppendLine("Statement7", suppressTransaction: true);
            batchBuilder.EndBatch();

            Assert.Equal(2, batchBuilder.SqlBatches.Count);
            Assert.False(batchBuilder.SqlBatches[0].SuppressTransaction);
            Assert.Equal(
                @"Statement1
Statement2
", batchBuilder.SqlBatches[0].Sql);

            Assert.True(batchBuilder.SqlBatches[1].SuppressTransaction);
            Assert.Equal(
                @"Statement3
Statement4
Statement5
Statement6
Statement7
", batchBuilder.SqlBatches[1].Sql);
        }