Esempio n. 1
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            var infoTextWriter = new LambdaTextWriter(WriteVerbose);

            List <ChangeScript> allChangeScripts = new DirectoryScanner(infoTextWriter, Encoding.UTF8)
                                                   .GetChangeScriptsForDirectory(new DirectoryInfo(this.deltasDirectory));

            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, true);

            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);
        }
Esempio n. 2
0
        public void SetUp()
        {
            var dbmsFactory   = new DbmsFactory(Dbms, ConnectionString);
            var queryExecuter = new QueryExecuter(dbmsFactory);
            var dbmsSyntax    = dbmsFactory.CreateDbmsSyntax();

            var schemaVersionManager = new DatabaseSchemaVersionManager(queryExecuter, dbmsSyntax, ChangeLogTableName);

            this.sqlCmdApplier    = new SqlCmdApplier(ConnectionString, schemaVersionManager, dbmsSyntax, ChangeLogTableName, System.Console.Out);
            this.directoryScanner = new DirectoryScanner(System.Console.Out, Encoding.UTF8);

            // Remove any existing changelog and customers test table.
            this.EnsureTableDoesNotExist(ChangeLogTableName);
            this.EnsureTableDoesNotExist("Customer");
        }
Esempio n. 3
0
        public override bool Execute()
        {
            bool result = false;

            try
            {
                LogTaskProperties();

                using (TextWriter outputPrintStream = new StreamWriter(outputfile.FullName))
                {
                    TextWriter undoOutputPrintStream = null;
                    if (undoOutputfile != null)
                    {
                        undoOutputPrintStream = new StreamWriter(undoOutputfile.FullName);
                    }
                    DbmsFactory factory    = new DbmsFactory(dbType, dbConnection);
                    IDbmsSyntax dbmsSyntax = factory.CreateDbmsSyntax(DatabaseSchemaVersionManager.DEFAULT_CHANGE_OWNER);
                    DatabaseSchemaVersionManager databaseSchemaVersion = new DatabaseSchemaVersionManager(factory, deltaSet, null);

                    ToPrintStreamDeployer toPrintSteamDeployer = new ToPrintStreamDeployer(databaseSchemaVersion, dir, outputPrintStream, dbmsSyntax, useTransaction, undoOutputPrintStream);
                    toPrintSteamDeployer.DoDeploy(lastChangeToApply);

                    if (undoOutputPrintStream != null)
                    {
                        undoOutputPrintStream.Close();
                    }
                }
                result = true;
            }
            catch (DbDeployException ex)
            {
                Log.LogErrorFromException(ex, true);
                Console.Error.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex, true);
                Console.Error.WriteLine("Failed to apply changes: " + ex.Message);
                Console.Error.WriteLine("Stack Trace:");
                Console.Error.Write(ex.StackTrace);
            }
            return(result);
        }
Esempio n. 4
0
        protected override void ExecuteTask()
        {
            try
            {
                Encoding encoding = new OutputFileEncoding(outputFileEncoding).AsEncoding();
                using (TextWriter outputPrintStream = new StreamWriter(outputfile.FullName, true, encoding))
                {
                    TextWriter undoOutputPrintStream = null;
                    if (undoOutputfile != null)
                    {
                        undoOutputPrintStream = new StreamWriter(undoOutputfile.FullName, true, encoding);
                    }

                    DbmsFactory factory = new DbmsFactory(dbType, dbConnection);
                    DatabaseSchemaVersionManager databaseSchemaVersion = new DatabaseSchemaVersionManager(factory, deltaSet, GetCurrentDbVersion(), changeLogTable, changeOwner);

                    IDbmsSyntax           dbmsSyntax           = factory.CreateDbmsSyntax(changeOwner);
                    ToPrintStreamDeployer toPrintSteamDeployer = new ToPrintStreamDeployer(databaseSchemaVersion, dir, outputPrintStream, dbmsSyntax, useTransaction, undoOutputPrintStream);
                    toPrintSteamDeployer.DoDeploy(lastChangeToApply);

                    if (undoOutputPrintStream != null)
                    {
                        undoOutputPrintStream.Close();
                    }
                }
            }
            catch (DbDeployException ex)
            {
                Console.Error.WriteLine(ex.Message);
                throw new BuildException(ex.Message);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Failed to apply changes: " + ex);
                Console.Error.WriteLine("Stack Trace:");
                Console.Error.Write(ex.StackTrace);
                throw new BuildException(ex.Message);
            }
        }
Esempio n. 5
0
        public static void Main(string[] args)
        {
            try
            {
                var             commandLineArgumentsParser = new CommandLineArgumentsParser();
                ParsedArguments parsedArguments            = commandLineArgumentsParser.ParseArgs(args);

                var printScreenFactory = new PrintScreenFactory();

                var config  = new CommandLineArgsConfiguration(parsedArguments);
                var factory = new DbmsFactory(config.DbType, config.DbConnectionString);
                var databaseSchemaVersion = new DatabaseSchemaVersionManager(factory, config.DbDeltaSet, config.CurrentDbVersion, config.TableName, config.ChangeOwner);

                var        directoryInfo         = new DirectoryInfo(parsedArguments.GetScriptFilesFolderOrDefaultFolder());
                TextWriter outputPrintStream     = printScreenFactory.GetDoPrintStream(parsedArguments);
                var        dbmsSyntax            = factory.CreateDbmsSyntax(config.ChangeOwner);
                var        useTransaction        = config.UseTransaction;
                TextWriter undoOutputPrintStream = printScreenFactory.GetUndoPrintStream(parsedArguments);

                var toPrintStreamDeployer = new ToPrintStreamDeployer(databaseSchemaVersion, directoryInfo, outputPrintStream, dbmsSyntax, useTransaction, undoOutputPrintStream);

                toPrintStreamDeployer.DoDeploy(LastVersionChangeToApply);

                printScreenFactory.ClosePrintStream(outputPrintStream);
                printScreenFactory.ClosePrintStream(undoOutputPrintStream);
            }
            catch (DbDeployException ex)
            {
                Console.Error.WriteLine(ex.Message);
                Environment.Exit(1);
            }

            catch (Exception ex)
            {
                Console.Error.WriteLine("Failed to apply changes: " + ex);
                Console.Error.WriteLine(ex.StackTrace);
                Environment.Exit(2);
            }
        }
Esempio n. 6
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, config.AutoCreateChangeLogTable);

            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,
                    config.Dbms,
                    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, 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,
                    infoWriter);
            }

            IChangeScriptApplier undoScriptApplier = null;
            TextWriter           undoWriter        = null;

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

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

            try
            {
                var controller = new Controller(
                    changeScriptRepository,
                    databaseSchemaVersionManager,
                    doScriptApplier,
                    undoScriptApplier,
                    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();
                }
            }
        }
Esempio n. 7
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();
                }
            }
        }
Esempio n. 8
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, 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();
                }
            }
        }