public void TestDoesNotRunSecondScriptIfFirstScriptFails()
        {
            this.EnsureTableDoesNotExist("TableWeWillUse");
            this.EnsureTableDoesNotExist(TableName);

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

            var output = new StringBuilder();
            
            var applier = new TemplateBasedApplier(
                new StringWriter(output),
                dbmsSyntax,
                TableName,
                ";",
                DbDeployDefaults.DelimiterType,
                DbDeployDefaults.TemplateDirectory);

            applier.Apply(new ChangeScript[]
            {
                new StubChangeScript(1, "1.test.sql", "INSERT INTO TableWeWillUse VALUES (1);"), 
                new StubChangeScript(2, "2.test.sql", "CREATE TABLE dbo.TableWeWillUse (Id int NULL);"), 
            }, createChangeLogTable: true);

            using (var sqlExecuter = new SqlCmdExecutor(this.ConnectionString))
            {
                var cmdOutput = new StringBuilder();
                sqlExecuter.ExecuteString(output.ToString(), cmdOutput);
            }
            this.AssertTableDoesNotExist("TableWeWillUse");
        }
Exemplo n.º 2
0
        private void RunScript(ChangeScript script, SqlCmdExecutor sqlCmdExecutor)
        {
            this.schemaVersionManager.RecordScriptStatus(script, ScriptStatus.Started);

            this.infoTextWriter.WriteLine(script);
            this.infoTextWriter.WriteLine("----------------------------------------------------------");
            var output = new StringBuilder();

            var success = false;
            try
            {
                success = sqlCmdExecutor.ExecuteFile(script.FileInfo, output);
                if (!success)
                {
                    throw new DbDeployException(string.Format(CultureInfo.InvariantCulture, "Script '{0}' failed.", script));
                }
            }
            finally
            {
                this.infoTextWriter.WriteLine(output);
                this.schemaVersionManager.RecordScriptStatus(script, success ? ScriptStatus.Success : ScriptStatus.Failure,
                    output.ToString());
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Applies the specified change scripts.
        /// </summary>
        /// <param name="changeScripts">The change scripts.</param>
        /// <param name="createChangeLogTable">Whether the change log table script should also be generated at the top</param>
        public void Apply(IEnumerable<ChangeScript> changeScripts, bool createChangeLogTable)
        {
            using (var sqlCmdExecutor = new SqlCmdExecutor(this.connectionString))
            {
                if (createChangeLogTable)
                {
                    CreateChangeLogTable(sqlCmdExecutor);
                }

                this.infoTextWriter.WriteLine(changeScripts.Any() ? "Applying change scripts...\n" : "No changes to apply.\n");

                foreach (var script in changeScripts)
                {
                    RunScript(script, sqlCmdExecutor);
                }
            }
        }
Exemplo n.º 4
0
        private void CreateChangeLogTable(SqlCmdExecutor sqlCmdExecutor)
        {
            this.infoTextWriter.WriteLine("Creating change log table");

            var output = new StringBuilder();

            try
            {
                if (!sqlCmdExecutor.ExecuteString(this.dbmsSyntax.CreateChangeLogTableSqlScript(this.changeLogTableName), output))
                {
                    throw new DbDeployException(string.Format("Create ChangeLog Table '{0}' failed.", this.changeLogTableName));
                }
            }
            finally
            {
                this.infoTextWriter.WriteLine(output);
            }
        }