private void ChangeNewDatabaseLocation(DeploymentPlanContributorContext context, string datalocation, string logdatalocation, string filePrefix)
        {
            DeploymentStep nextStep = context.PlanHandle.Head;

            // Loop through all steps in the deployment plan
            bool foundSetVars = false;

            while (nextStep != null && !foundSetVars)
            {
                // Increment the step pointer, saving both the current and next steps
                DeploymentStep currentStep = nextStep;

                // Only interrogate up to BeginPreDeploymentScriptStep - setvars must be done before that
                // We know this based on debugging a new deployment and examining the output script
                if (currentStep is BeginPreDeploymentScriptStep)
                {
                    break;
                }

                DeploymentScriptStep scriptStep = currentStep as DeploymentScriptStep;
                if (scriptStep != null)
                {
                    IList <string> scripts = scriptStep.GenerateTSQL();
                    foreach (string script in scripts)
                    {
                        if (script.Contains("DefaultDataPath"))
                        {
                            // This is the step that sets the default data path and log path.
                            foundSetVars = true;

                            // Override setvars before the deployment begins
                            StringBuilder sb = new StringBuilder();
                            sb.AppendFormat(":setvar DefaultDataPath \"{0}\"", datalocation)
                            .AppendLine()
                            .AppendFormat(":setvar DefaultLogPath \"{0}\"", logdatalocation)
                            .AppendLine()
                            .AppendFormat(":setvar DefaultFilePrefix \"{0}\"", filePrefix)
                            .AppendLine();

                            // Create a new step for the setvar statements, and add it after the existing step.
                            // That ensures that the updated values are used instead of the defaults
                            DeploymentScriptStep setVarsStep = new DeploymentScriptStep(sb.ToString());
                            this.AddAfter(context.PlanHandle, scriptStep, setVarsStep);
                        }
                    }
                }

                nextStep = currentStep.Next;
            }
        }
        /// <summary>
        /// Iterates over the deployment plan to find the definition for
        /// </summary>
        /// <param name="context"></param>
        protected override void OnExecute(DeploymentPlanContributorContext context)
        {
            DeploymentStep next        = context.PlanHandle.Head;
            bool           foundDropDb = false;

            while (next != null)
            {
                DeploymentStep current = next;
                next = current.Next;

                //works!
                if (foundDropDb)
                {
                    base.Remove(context.PlanHandle, current);
                }

                DeploymentScriptStep scriptStep = current as DeploymentScriptStep;
                if (scriptStep != null)
                {
                    IList <string> scripts = scriptStep.GenerateTSQL();
                    foreach (string script in scripts)
                    {
                        if (script.Contains("DROP DATABASE"))
                        {
                            // This is the step that removes the drop database step
                            foundDropDb = true;
                        }
                    }
                }
            }

            // Override setvars before the deployment begins
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("PRINT N'Update complete.';")
            .AppendLine()
            .AppendFormat("GO")
            .AppendLine();

            // Create a new step for the setvar statements, and add it after the existing step.
            // That ensures that the updated values are used instead of the defaults
            DeploymentScriptStep setVarsStep = new DeploymentScriptStep(sb.ToString());

            this.AddAfter(context.PlanHandle, context.PlanHandle.Tail, setVarsStep);
        }
        private void RemoveDropDatabaseStep(DeploymentPlanContributorContext context)
        {
            DeploymentStep nextStep = context.PlanHandle.Head;

            // Loop through all steps in the deployment plan
            bool foundDropDb = false;

            while (nextStep != null && !foundDropDb)
            {
                // Increment the step pointer, saving both the current and next steps
                DeploymentStep currentStep = nextStep;

                // Only interrogate up to BeginPreDeploymentScriptStep - setvars must be done before that
                // We know this based on debugging a new deployment and examining the output script
                if (currentStep is BeginPreDeploymentScriptStep)
                {
                    break;
                }

                DeploymentScriptStep scriptStep = currentStep as DeploymentScriptStep;
                if (scriptStep != null)
                {
                    IList <string> scripts = scriptStep.GenerateTSQL();
                    foreach (string script in scripts)
                    {
                        if (script.Contains("DROP DATABASE"))
                        {
                            // This is the step that removes the drop database step
                            foundDropDb = true;

                            // Remove the current step
                            this.Remove(context.PlanHandle, currentStep);
                        }
                    }
                }

                nextStep = currentStep.Next;
            }
        }
        protected override void OnExecute(DeploymentPlanContributorContext context)
        {
            DeploymentStep nextStep = context.PlanHandle.Head;

            while (nextStep != null)
            {
                DeploymentStep currentStep = nextStep;
                nextStep = currentStep.Next;

                //Debug.WriteLine($"{currentStep.GetType()}");

                if (currentStep is DeploymentScriptStep)
                {
                    DeploymentScriptStep d = currentStep as DeploymentScriptStep;

                    Regex rx = new Regex(@"\[sandbox\]");   //[\n\r]*is being dropped.  Deployment will halt if the table contains data.");
                    if (rx.IsMatch(d.GenerateTSQL()[0]))
                    {
                        base.Remove(context.PlanHandle, currentStep);
                        continue;
                    }
                }

                if (currentStep is CreateElementStep)
                {
                    DeploymentScriptDomStep domStep = currentStep as DeploymentScriptDomStep;

                    TSqlScript    script = domStep.Script as TSqlScript;
                    TSqlStatement t      = script.Batches[0].Statements[0];

                    if (t is CreateTableStatement o)
                    {
                        SchemaObjectName ol  = o.SchemaObjectName;
                        string           ol1 = ol.SchemaIdentifier.Value;

                        if (ol1 == "sandbox" || ol1 == "unittests")
                        {
                            base.Remove(context.PlanHandle, currentStep);
                            continue;
                        }
                    }

                    //  Sql140ScriptGenerator s = new Sql140ScriptGenerator();
                    //  s.GenerateScript(t, out string ts);
                    //  Debug.WriteLine($"{t.GetType()}: {ts}");
                    //  DropChildObjectsStatement
                    //  DropStatisticsStatement
                }

                if (currentStep is DropElementStep)
                {
                    DeploymentScriptDomStep domStep = currentStep as DeploymentScriptDomStep;
                    TSqlScript    script            = domStep.Script as TSqlScript;
                    TSqlStatement t = script.Batches[0].Statements[0];

                    //Debug.WriteLine($"{currentStep.GetType()}: {t.GetType()}");

                    if (t is DropStatisticsStatement)
                    {
                        base.Remove(context.PlanHandle, currentStep);
                        continue;
                    }

                    if (t is DropObjectsStatement)
                    {
                        DropObjectsStatement     o  = (DropObjectsStatement)t;
                        IList <SchemaObjectName> ol = o.Objects;
                        string ol1 = ol[0].SchemaIdentifier.Value;

                        if (ol1 == "sandbox" || ol1 == "unittests")
                        {
                            base.Remove(context.PlanHandle, currentStep);
                            continue;
                        }
                    }

                    //  Sql140ScriptGenerator s = new Sql140ScriptGenerator();
                    //  s.GenerateScript(t, out string ts);
                    //  Debug.WriteLine($"{t.GetType()}: {ts}");
                    //  DropChildObjectsStatement
                    //  DropStatisticsStatement
                }

                //if (currentStep is AlterElementStep)
                //{
                //    DeploymentScriptDomStep domStep = currentStep as DeploymentScriptDomStep;
                //    TSqlScript script = domStep.Script as TSqlScript;
                //    TSqlStatement t = script.Batches[0].Statements[0];

                //    if (t is AlterProcedureStatement)
                //    {
                //        AlterProcedureStatement o = (AlterProcedureStatement)t;
                //        SchemaObjectName ol = o.Options.sch
                //        string ol1 = ol.SchemaIdentifier.Value;

                //        if (ol1 == "sandbox" || ol1 == "unittests")
                //        {
                //            base.Remove(context.PlanHandle, currentStep);
                //            continue;
                //        }
                //    }
                //}
            }
        }