Exemplo n.º 1
0
        /// <summary>
        /// This method should only really be used with a command line, unattended execution.
        /// </summary>
        /// <param name="connData"></param>
        /// <param name="objectUpdates"></param>
        /// <param name="dontUpdate"></param>
        /// <param name="manualScriptsCanNotUpdate"></param>
        /// <param name="sourceBuildZipFileName"></param>
        /// <param name="sourceServer"></param>
        /// <param name="sourceDb"></param>
        /// <returns></returns>
        public static string CreateDefaultBackoutPackage(ConnectionData connData, string sourceBuildZipFileName, string sourceServer, string sourceDb)
        {
            /*How to create a backout package:
             *
             * 1. Extract the package and load the BuildData
             * 2. Get the list of sriptable objects and manually created scripts
             * 3. Targeting your "old" source, and see what scriptable objects are not there (i.e. are "new" in the package)
             *
             */
            List <string>        manualScriptsCanNotUpdate;
            List <ObjectUpdates> initialCanUpdateList;
            string           workingDirectory = string.Empty;
            string           projectFilePath  = string.Empty;
            string           projectFileName  = string.Empty;
            string           result;
            SqlSyncBuildData buildData;
            BackgroundWorker bg = new BackgroundWorker();

            bg.WorkerReportsProgress = true;

            //Extract and load the build data...
            log.LogDebug($"Extracting SBM zip file for {sourceBuildZipFileName}");
            bool success = SqlBuildFileHelper.ExtractSqlBuildZipFile(sourceBuildZipFileName, ref workingDirectory, ref projectFilePath, ref projectFileName, out result);

            if (success)
            {
                log.LogDebug($"Loading SqlSyncBuldData object from {projectFileName}");
                success = SqlBuildFileHelper.LoadSqlBuildProjectFile(out buildData, projectFileName, false);
                if (!success)
                {
                    return(string.Empty);
                }
            }
            else
            {
                return(string.Empty);
            }

            //Get the scriptable objects
            log.LogDebug($"Getting the scriptable objects from {sourceBuildZipFileName}");
            SqlBuildFileHelper.GetFileDataForObjectUpdates(ref buildData, projectFileName, out initialCanUpdateList, out manualScriptsCanNotUpdate);

            //Get object that are also on the target (ie are "existing") -- only these will be updated
            log.LogDebug($"Getting list of objects can be rolled back from {sourceServer}:{sourceDb}");
            List <ObjectUpdates> canUpdate = GetObjectThatCanBeUpdated(initialCanUpdateList, connData, sourceServer, sourceDb);

            SetBackoutSourceDatabaseAndServer(ref canUpdate, sourceServer, sourceDb);

            //Get the scriptable objects that are not found on the target (i.e. are "new") -- these will be dropped
            log.LogDebug($"Getting list of objects can not be rolled back from {sourceServer}:{sourceDb}");
            List <ObjectUpdates> notPresentOnTarget = GetObjectsNotPresentTargetDatabase(initialCanUpdateList, connData, sourceServer, sourceDb);

            //Get the name of the new package
            string backoutPackageName = GetDefaultPackageName(sourceBuildZipFileName);

            //Create the package!!
            log.LogDebug($"Creating backout package {backoutPackageName} from source package {sourceBuildZipFileName}");
            success = CreateBackoutPackage(connData, canUpdate, notPresentOnTarget, manualScriptsCanNotUpdate,
                                           sourceBuildZipFileName, backoutPackageName,
                                           sourceServer, sourceDb, true, true, true, ref bg);

            //Cleanup all the temp files created
            SqlBuildFileHelper.CleanUpAndDeleteWorkingDirectory(workingDirectory);

            if (!success)
            {
                log.LogError("Unable to create backout package!");
                return(string.Empty);
            }

            return(backoutPackageName);
        }