Exemplo n.º 1
0
        public static DacpacDeltasStatus CreateSbmFromDacPacDifferences(string platinumDacPacFileName, string targetDacPacFileName, bool batchScripts, string buildRevision, int defaultScriptTimeout, bool allowObjectDelete, out string buildPackageName)
        {
            log.LogInformation($"Generating SBM build from dacpac differences: {Path.GetFileName(platinumDacPacFileName)} vs { Path.GetFileName(targetDacPacFileName)}");
            string path = Path.GetDirectoryName(targetDacPacFileName);

            buildPackageName = string.Empty;
            string rawScript = ScriptDacPacDeltas(platinumDacPacFileName, targetDacPacFileName, path, allowObjectDelete, false);

            if (!string.IsNullOrEmpty(rawScript))
            {
                Directory.CreateDirectory(path);

                string cleaned;
                var    cleanStatus = CleanDacPacScript(rawScript, out cleaned);
                switch (cleanStatus)
                {
                case DacpacDeltasStatus.InSync:
                case DacpacDeltasStatus.OnlyPostDeployment:
                    return(cleanStatus);
                }

                string baseFileName = Path.Combine(path, string.Format("{0}_to_{1}", Path.GetFileNameWithoutExtension(targetDacPacFileName), Path.GetFileNameWithoutExtension(platinumDacPacFileName)));

                List <string> files = new List <string>();
                if (batchScripts)
                {
                    files = BatchAndSaveScripts(cleaned, path);
                }
                else
                {
                    File.WriteAllText(baseFileName + ".sql", cleaned);
                    files.Add(baseFileName + ".sql");
                }

                if (!string.IsNullOrWhiteSpace(buildRevision))
                {
                    var    versionIns = Properties.Resources.VersionsInsert.Replace("{{BuildRevision}}", buildRevision);
                    string verName    = Path.Combine(path, "Versions Insert.sql");
                    File.WriteAllText(verName, versionIns);
                    files.Add(verName);
                }

                buildPackageName = baseFileName + ".sbm";
                if (SqlBuildFileHelper.SaveSqlFilesToNewBuildFile(buildPackageName, files, "client", true, defaultScriptTimeout))
                {
                    //Clean up generated scripts files
                    files.ForEach(f =>
                    {
                        try
                        {
                            File.Delete(f);
                        }
                        catch (Exception exe)
                        {
                            log.LogError($"Unable to delete file {f}. {exe.ToString()}");
                        }
                    });
                    return(DacpacDeltasStatus.Success);
                }
            }
            return(DacpacDeltasStatus.Failure);
        }