Primary controller for executing DbDeploy.
 private void GenerateUndoChangeScripts(ChangeScriptRepository repository, int lastChangeToApply, List<int> appliedChanges)
 {
     ChangeScriptExecuter undoScriptExecuter = new ChangeScriptExecuter(undoOutputPrintStream, dbmsSyntax, useTransaction);
     Controller undoController = new Controller(schemaManager, repository, undoScriptExecuter, infoPrintStream);
     undoController.ProcessUndoChangeScripts(lastChangeToApply, appliedChanges);
     undoOutputPrintStream.Flush();
 }
        public void Setup()
        {
            // Setup default available scripts.
            this.availableChangeScriptsProvider = new Mock<IAvailableChangeScriptsProvider>();
            this.availableChangeScriptsProvider.Setup(p => p.GetAvailableChangeScripts())
                .Returns(new List<ChangeScript>
                        {
                            new ChangeScript("1.0", 1),
                            new ChangeScript("1.0", 2),
                            new ChangeScript("1.0", 3),
                            new ChangeScript("1.0", 4),
                            new ChangeScript("1.1", 1),
                            new ChangeScript("1.1", 2),
                        });

            // Capture changes that would be run.
            this.runScripts = new List<ChangeScript>();
            this.doApplier = new Mock<IChangeScriptApplier>();
            this.doApplier
                .Setup(a => a.Apply(It.IsAny<IEnumerable<ChangeScript>>(), false))
                .Callback<IEnumerable<ChangeScript>, bool>((l,b) => this.runScripts = l.ToList());

            this.appliedChangesProvider = new Mock<IAppliedChangesProvider>();
            var undoApplier = new Mock<IChangeScriptApplier>();
            this.output = new StringBuilder();
            this.controller = new Controller(this.availableChangeScriptsProvider.Object, this.appliedChangesProvider.Object, this.doApplier.Object, undoApplier.Object, false, new StringWriter(this.output));
        }
        /// <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, config.DllPathConnector);

            var dbmsSyntax = factory.CreateDbmsSyntax();

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

            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, SupportedDbms.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 changeScriptRepositoryFactory = new ChangeScriptRepositoryFactory(config, infoWriter);
                var changeScriptRepository = changeScriptRepositoryFactory.Obter();
                var repositorioScripts = new RepositorioScripts(databaseSchemaVersionManager, changeScriptRepository);

                var controller = new Controller(
                                        repositorioScripts,
                                        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 #4
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();
            }
        }