Ejemplo n.º 1
0
        private void GenerateScript(object state)
        {
            try
            {
                var project = state as Project;

                var filename = GetSelectedSolutionExplorerFileName();

                if (String.IsNullOrEmpty(filename))
                {
                    OutputWindowMessage.WriteMessage("Couldn't GetConfig filename");
                    return;
                }

                if (!filename.EndsWith(".sql"))
                {
                    OutputWindowMessage.WriteMessage("This only works with .sql files - boo hoo hoo");
                    return;
                }

                var procname = ScriptProperties.GetScriptDetail(File.ReadAllText(filename)).Name;
                if (string.IsNullOrEmpty(procname))
                {
                    OutputWindowMessage.WriteMessage("Couldn't GetConfig proc name - boo hoo hoo");
                    return;
                }
                var settings = Config.Configuration.GetSettings(project);

                WriteDeployFile(project.FullName, procname, filename, settings.DeploymentFolder);
            }
            catch (Exception e)
            {
                OutputWindowMessage.WriteMessage("Unable to generate script: " + e.Message);
            }
        }
Ejemplo n.º 2
0
        private void DeploySingleFile(object state)
        {
            try
            {
                var project = state as Project;

                var variables = new SsdtVariableProvider().GetVariables(project.FullName);

                var settings = Config.Configuration.GetSettings(project);

                var filename = GetSelectedSolutionExplorerFileName();

                if (String.IsNullOrEmpty(filename))
                {
                    OutputWindowMessage.WriteMessage("Couldn't GetConfig filename");
                    return;
                }

                if (!filename.EndsWith(".sql"))
                {
                    OutputWindowMessage.WriteMessage("Single file deploy only works with .sql files - boo hoo hoo");
                    return;
                }

                var procname = ScriptProperties.GetScriptDetail(File.ReadAllText(filename)).Name;

                if (string.IsNullOrEmpty(procname))
                {
                    OutputWindowMessage.WriteMessage("Couldn't GetConfig proc name - boo hoo hoo");
                    return;
                }

                var fileContents = GetFileContents(filename);

                foreach (SqlCmdVariable v in variables)
                {
                    fileContents = fileContents.Replace(v.Name, v.Value);
                }


                if (!DtcAvailable())
                {
                    OutputWindowMessage.WriteMessage("Unable to deploy file, deploy uses msdtc to protect changes. Please ensure the service is enabled and running");
                    return;
                }

                using (var scope = new TransactionScope())
                {
                    try
                    {
                        var script  = DeploymentScriptGenerator.BuildDeploy(fileContents);
                        var batches = script.Split(new string[] { "\r\nGO\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var batch in batches)
                        {
                            new SqlGateway(settings.ConnectionString).Execute(batch);
                        }

                        scope.Complete();


                        OutputWindowMessage.WriteMessage(string.Format("Deployed File: {0}\r\n", filename));
                    }
                    catch (NullReferenceException)
                    {
                        OutputWindowMessage.WriteMessage(string.Format("Unable to deploy file {0}\r\n", filename));
                    }
                    catch (Exception ex)
                    {
                        OutputWindowMessage.WriteMessage(string.Format("Unable to deploy file {0} error : {1}\r\n",
                                                                       filename, ex.Message));
                    }
                }
            }
            catch (Exception e)
            {
                OutputWindowMessage.WriteMessage("Deploying file Failed: " + e.Message);
            }
        }