protected override void ProcessRecord()
        {
            base.ProcessRecord();

            var infoTextWriter = new LambdaTextWriter(WriteVerbose);

            List<ChangeScript> allChangeScripts =
                new DirectoryScanner(infoTextWriter).GetChangeScriptsForDirectory(new DirectoryInfo(_deltasDirectory));
            
            var repository = new ChangeScriptRepository(allChangeScripts);
            var changeScripts = repository.GetOrderedListOfDoChangeScripts();

            var appliedChangeNumbers = _databaseSchemaVersion.GetAppliedChangeNumbers();
            var notAppliedChangeScripts = changeScripts.Where(c => !appliedChangeNumbers.Contains(c.GetId()));

            var descriptionPrettyPrinter = new DescriptionPrettyPrinter();

            var objects =
                notAppliedChangeScripts
                    .Select(script => new
                                          {
                                              Id = script.GetId(),
                                              Description = descriptionPrettyPrinter.Format(script.GetDescription()),
                                              File = script.GetFile()
                                          });

            WriteObject(objects, true);
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            var infoTextWriter = new LambdaTextWriter(WriteVerbose);

            List<ChangeScript> allChangeScripts = new DirectoryScanner(infoTextWriter, Encoding.UTF8, new DirectoryInfo(this.deltasDirectory))
                .GetChangeScripts();
            
            var repository = new ChangeScriptRepository(allChangeScripts);
            var changeScripts = repository.GetAvailableChangeScripts();

            DbmsFactory factory = new DbmsFactory(this.DatabaseType, this.ConnectionString);
            var queryExecuter = new QueryExecuter(factory);

            var schemaManager = new DatabaseSchemaVersionManager(queryExecuter, factory.CreateDbmsSyntax(), this.TableName);

            var appliedChanges = schemaManager.GetAppliedChanges();
            var notAppliedChangeScripts = changeScripts.Where(c => appliedChanges.All(a => a.ScriptNumber != c.ScriptNumber));

            var descriptionPrettyPrinter = new DescriptionPrettyPrinter();

            var objects = notAppliedChangeScripts
                .Select(script => new
                    {
                        Id = script.ScriptNumber,
                        Description = descriptionPrettyPrinter.Format(script.ScriptName),
                        File = script.FileInfo
                    });

            this.WriteObject(objects, true);
        }
Beispiel #3
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            var infoTextWriter = new LambdaTextWriter(WriteVerbose);
                
            var updateText = new StringBuilder();
            var stringWriterForUpdateScript = new StringWriter(updateText);

            List<ChangeScript> changeScripts = new DirectoryScanner(infoTextWriter).GetChangeScriptsForDirectory(new DirectoryInfo(_deltasDirectory));
            new PowershellPrintStreamDeployer(_databaseSchemaVersion, new ChangeScriptRepository(changeScripts),
                                              stringWriterForUpdateScript,
                                              _dbmsFactory.CreateDbmsSyntax(), UseTransaction, null,
                                              infoTextWriter)
                .DoDeploy(Int32.MaxValue, infoTextWriter);

            using (var connection = _dbmsFactory.CreateConnection())
            {
                connection.Open();

                var command = connection.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = updateText.ToString();
                var result = command.ExecuteNonQuery();
                WriteObject(new {RowsChanged = result});
            }
        }
        public void CanSearchForFilesFromNotExistDirectory()
        {
            var writer = new StringWriter();
            var directoryScanner = new DirectoryScanner(writer, Encoding.UTF8, new DirectoryInfo(@"Mocks\NotExistDirectory"));

            List<ChangeScript> changeScripts = directoryScanner.GetChangeScripts();

            Assert.IsNotNull(changeScripts, "Change scripts should not be null.");
            Assert.AreEqual(changeScripts.Count, 0, "Needs no change scripts where found.");
        }
Beispiel #5
0
        public void CanSearchForFilesFromNotExistDirectory()
        {
            var writer           = new StringWriter();
            var directoryScanner = new DirectoryScanner(writer, Encoding.UTF8, new DirectoryInfo(@"Mocks\NotExistDirectory"));

            List <ChangeScript> changeScripts = directoryScanner.GetChangeScripts();

            Assert.IsNotNull(changeScripts, "Change scripts should not be null.");
            Assert.AreEqual(changeScripts.Count, 0, "Needs no change scripts where found.");
        }
        public void ShouldApplySqlCmdModeScripts()
        {
            var directoryScanner = new DirectoryScanner(System.Console.Out, Encoding.UTF8, new DirectoryInfo(@"Mocks\Versioned\v2.0.10.0"));

            var changeScripts = directoryScanner.GetChangeScripts();
            this.sqlCmdApplier.Apply(changeScripts, true);

            this.AssertTableExists(ChangeLogTableName);
            this.AssertTableExists("Customer");

            var changeCount = this.ExecuteScalar<int>("SELECT COUNT(*) FROM {0}", ChangeLogTableName);
            Assert.AreEqual(changeScripts.Count, changeCount, "Not all change scripts where applied.");
        }
        public void DoDeploy(int lastChangeToApply)
        {
            Console.Out.WriteLine("dbdeploy v2.12");

            List<ChangeScript> changeScripts = new DirectoryScanner(Console.Out).GetChangeScriptsForDirectory(dir);
            ChangeScriptRepository repository = new ChangeScriptRepository(changeScripts);
            List<int> appliedChanges = schemaManager.GetAppliedChangeNumbers();

            GenerateChangeScripts(repository, lastChangeToApply, appliedChanges);
            if (undoOutputPrintStream != null)
            {
                GenerateUndoChangeScripts(repository, lastChangeToApply, appliedChanges);
            }
        }
        public void CanReadFilesFromSubDirectories()
        {
            var writer = new StringWriter();
            var directoryScanner = new DirectoryScanner(writer, Encoding.UTF8, new DirectoryInfo(@"Mocks\Versioned"));
            
            List<ChangeScript> changeScripts = directoryScanner.GetChangeScripts();
            
            Assert.IsNotNull(changeScripts, "Change scripts should not be null.");
            Assert.Greater(changeScripts.Count, 0, "No change scripts where found.");

            VerifyChangeScript(changeScripts, "2.0.0.0", 8, "8.Create Product Table.sql");
            VerifyChangeScript(changeScripts, "2.0.0.0", 9, "09.Add Product Data.sql");
            VerifyChangeScript(changeScripts, "2.0.0.0", 10, "10.Add Sold Column.sql");
            VerifyChangeScript(changeScripts, "v2.0.10.0", 1, "1.SQLCMD Add Customer Table.sql");
            VerifyChangeScript(changeScripts, "v2.0.10.0", 2, "2.SQLCMD Add Email Column Table.sql");
            VerifyChangeScript(changeScripts, "v2.0.10.0", 3, "3.SQLCMD Add Customer Data.sql");
        }
Beispiel #9
0
        public void CanReadFilesFromSubDirectories()
        {
            var writer           = new StringWriter();
            var directoryScanner = new DirectoryScanner(writer, Encoding.UTF8, new DirectoryInfo(@"Mocks\Versioned"));

            List <ChangeScript> changeScripts = directoryScanner.GetChangeScripts();

            Assert.IsNotNull(changeScripts, "Change scripts should not be null.");
            Assert.Greater(changeScripts.Count, 0, "No change scripts where found.");

            VerifyChangeScript(changeScripts, "2.0.0.0", 8, "8.Create Product Table.sql");
            VerifyChangeScript(changeScripts, "2.0.0.0", 9, "09.Add Product Data.sql");
            VerifyChangeScript(changeScripts, "2.0.0.0", 10, "10.Add Sold Column.sql");
            VerifyChangeScript(changeScripts, "v2.0.10.0", 1, "1.SQLCMD Add Customer Table.sql");
            VerifyChangeScript(changeScripts, "v2.0.10.0", 2, "2.SQLCMD Add Email Column Table.sql");
            VerifyChangeScript(changeScripts, "v2.0.10.0", 3, "3.SQLCMD Add Customer Data.sql");
        }
        public void ShouldThrowExceptionOnScriptFailure()
        {
            var directoryScanner = new DirectoryScanner(System.Console.Out, Encoding.UTF8,
                                                        new DirectoryInfo(@"Mocks\Failures"));

            // Duplicate the first script to cause a failure.
            var changeScripts = directoryScanner.GetChangeScripts();

            try
            {
                this.sqlCmdApplier.Apply(changeScripts, true);
                Assert.Fail("Apply did not thrown and error.");
            }
            catch (DbDeployException)
            {
            }

            var changeCount = this.ExecuteScalar<int>("SELECT COUNT(*) FROM {0}", ChangeLogTableName);
            Assert.AreEqual(2, changeCount, "Only two scripts should have run.");
        }
Beispiel #11
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            if (!string.IsNullOrEmpty(UndoOutputFile) && string.IsNullOrEmpty(OutputFile))
            {
                WriteError(new ErrorRecord(new PSInvalidOperationException("Missing a file for output (just picked one for undo output)"),
                                           "NoUndoOutputFile", ErrorCategory.MetadataError, null));
                return;
            }

            var infoTextWriter = new LambdaTextWriter(WriteVerbose);

            TextWriter outputTextWriter;
            var openedFiles = new List<FileStream>();

            try
            {
                if (string.IsNullOrEmpty(OutputFile))
                {
                    outputTextWriter = new LambdaTextWriter(WriteObject);
                }
                else
                {
                    var outputFile = ToAbsolutePath(OutputFile);
                    WriteObject(String.Format("Writing update script to {0}", outputFile));
                    var openedFile = File.OpenWrite(outputFile);
                    openedFiles.Add(openedFile);

                    outputTextWriter = new StreamWriter(openedFile, Encoding.UTF8);
                    infoTextWriter = new LambdaTextWriter(WriteObject);
                }

                TextWriter undoTextWriter = null;
                if (!string.IsNullOrEmpty(UndoOutputFile))
                {
                    var undoOutputFile = ToAbsolutePath(UndoOutputFile);
                    WriteObject(String.Format("Writing undo update script to {0}", undoOutputFile));
                    var openedFile = File.OpenWrite(undoOutputFile);
                    openedFiles.Add(openedFile);
                    undoTextWriter = new StreamWriter(openedFile, Encoding.UTF8);
                }

                infoTextWriter.WriteLine("dbdeploy v2.12");

                List<ChangeScript> changeScripts = new DirectoryScanner(infoTextWriter).GetChangeScriptsForDirectory(new DirectoryInfo(_deltasDirectory));
                new PowershellPrintStreamDeployer(_databaseSchemaVersion, new ChangeScriptRepository(changeScripts),
                                                  outputTextWriter,
                                                  _dbmsFactory.CreateDbmsSyntax(), UseTransaction, undoTextWriter,
                                                  infoTextWriter)
                    .DoDeploy(Int32.MaxValue, infoTextWriter);

            }
            finally
            {
                foreach(var file in openedFiles)
                {
                    file.Close();
                }
            }
        }
Beispiel #12
0
        /// <summary>
        /// Executes the a database deployment with the specified config.
        /// </summary>
        /// <param name="config">The config.</param>
        /// <param name="infoWriter">The info writer.</param>
        /// <exception cref="System.InvalidOperationException">SQLCMD mode can only be applied against an mssql database.</exception>
        public void Execute(DbDeployConfig config, TextWriter infoWriter)
        {
            this.Validate(config, infoWriter);

            infoWriter.WriteLine();
            infoWriter.WriteLine("==========================================================");
            infoWriter.WriteLine(this.GenerateWelcomeString());

            var factory = new DbmsFactory(config.Dbms, config.ConnectionString);
            
            var dbmsSyntax = factory.CreateDbmsSyntax();

            var queryExecuter = new QueryExecuter(factory);

            var databaseSchemaVersionManager = new DatabaseSchemaVersionManager(queryExecuter, dbmsSyntax, config.ChangeLogTableName);

            var scanner = new DirectoryScanner(infoWriter, config.Encoding);

            var changeScriptRepository = new ChangeScriptRepository(scanner.GetChangeScriptsForDirectory(config.ScriptDirectory));

            IChangeScriptApplier doScriptApplier;
            TextWriter doWriter = null;
            QueryExecuter applierExecutor = null;

            if (config.OutputFile != null) 
            {
                doWriter = new StreamWriter(config.OutputFile.OpenWrite(), config.Encoding);

                doScriptApplier = new TemplateBasedApplier(
                    doWriter,
                    dbmsSyntax,
                    config.ChangeLogTableName,
                    config.Delimiter,
                    config.DelimiterType,
                    config.TemplateDirectory);
            }
            else if (config.UseSqlCmd)
            {
                // Verify database is MSSQL.
                if (!string.Equals(config.Dbms, "mssql", StringComparison.InvariantCultureIgnoreCase))
                {
                    throw new InvalidOperationException("SQLCMD mode can only be applied against an mssql database.");
                }

                doScriptApplier = new SqlCmdApplier(
                    config.ConnectionString,
                    databaseSchemaVersionManager,
                    dbmsSyntax,
                    config.ChangeLogTableName,
                    infoWriter);
            }
            else 
            {
                var splitter = new QueryStatementSplitter
                {
                    Delimiter = config.Delimiter,
                    DelimiterType = config.DelimiterType,
                    LineEnding = config.LineEnding,
                };

                // Do not share query executor between schema manager and applier, since a failure in one will effect the other.
                applierExecutor = new QueryExecuter(factory);
                doScriptApplier = new DirectToDbApplier(
                    applierExecutor, 
                    databaseSchemaVersionManager, 
                    splitter, 
                    dbmsSyntax, 
                    config.ChangeLogTableName,
                    infoWriter);
            }

            IChangeScriptApplier undoScriptApplier = null;
            TextWriter undoWriter = null;

            if (config.UndoOutputFile != null) 
            {
                undoWriter = new StreamWriter(config.UndoOutputFile.OpenWrite(), config.Encoding);

                undoScriptApplier = new UndoTemplateBasedApplier(
                    undoWriter,
                    dbmsSyntax,
                    config.ChangeLogTableName,
                    config.Delimiter,
                    config.DelimiterType,
                    config.TemplateDirectory);
            }

            try
            {
                var controller = new Controller(
                    changeScriptRepository, 
                    databaseSchemaVersionManager, 
                    doScriptApplier, 
                    undoScriptApplier, 
                    config.AutoCreateChangeLogTable,
                    infoWriter);

                controller.ProcessChangeScripts(config.LastChangeToApply, config.ForceUpdate);

                queryExecuter.Close();

                if (applierExecutor != null)
                {
                    applierExecutor.Close();
                }
            }
            finally
            {
                if (doWriter != null)
                {
                    doWriter.Dispose();
                }

                if (undoWriter != null)
                {
                    undoWriter.Dispose();
                }
            }
        }
Beispiel #13
0
        public void Go()
        {
            this.Validate();

            this.InfoWriter.WriteLine(this.GenerateWelcomeString());
            
            var factory = new DbmsFactory(this.Dbms, this.ConnectionString);
            
            var dbmsSyntax = factory.CreateDbmsSyntax();

            QueryExecuter queryExecuter = new QueryExecuter(factory);

            var databaseSchemaVersionManager =
                    new DatabaseSchemaVersionManager(queryExecuter, dbmsSyntax, this.ChangeLogTableName);

            var scanner = new DirectoryScanner(this.InfoWriter, this.Encoding);

            var changeScriptRepository =
                    new ChangeScriptRepository(scanner.GetChangeScriptsForDirectory(this.ScriptDirectory));

            IChangeScriptApplier doScriptApplier;
            TextWriter doWriter = null;

            if (this.OutputFile != null) 
            {
                doWriter = new StreamWriter(this.OutputFile.OpenWrite(), this.Encoding);

                doScriptApplier = new TemplateBasedApplier(
                    doWriter, 
                    this.Dbms, 
                    this.ChangeLogTableName, 
                    this.Delimiter, 
                    this.DelimiterType, 
                    this.TemplateDir);
            } 
            else 
            {
                QueryStatementSplitter splitter = new QueryStatementSplitter
                {
                    Delimiter = this.Delimiter,
                    DelimiterType = this.DelimiterType,
                    LineEnding = this.LineEnding,
                };

                doScriptApplier = new DirectToDbApplier(
                    queryExecuter, 
                    databaseSchemaVersionManager, 
                    splitter, 
                    this.InfoWriter);
            }

            IChangeScriptApplier undoScriptApplier = null;
            TextWriter undoWriter = null;

            if (this.UndoOutputFile != null) 
            {
                undoWriter = new StreamWriter(this.UndoOutputFile.OpenWrite(), this.Encoding);

                undoScriptApplier = new UndoTemplateBasedApplier(
                    undoWriter, 
                    this.Dbms, 
                    this.ChangeLogTableName, 
                    this.Delimiter, 
                    this.DelimiterType, 
                    this.TemplateDir);
            }

            try
            {
                Controller controller = new Controller(
                    changeScriptRepository, 
                    databaseSchemaVersionManager, 
                    doScriptApplier, 
                    undoScriptApplier, 
                    this.InfoWriter);

                controller.ProcessChangeScripts(this.LastChangeToApply);

                queryExecuter.Close();
            }
            finally
            {
                if (doWriter != null)
                    doWriter.Dispose();

                if (undoWriter != null)
                    undoWriter.Dispose();
            }
        }