private static Comparison CreateComparisonInitialized()

in BismNormalizer/BismNormalizer/TabularCompare/ComparisonFactory.cs [64:202]


        private static Comparison CreateComparisonInitialized(ComparisonInfo comparisonInfo)
        {
            Telemetry.TrackEvent("CreateComparisonInitialized", new Dictionary<string, string> { { "App", comparisonInfo.AppName.Replace(" ", "") } });

            //If composite models not allowed on AS, check DQ/Import at model level matches:
            if (comparisonInfo.AppName == "BISM Normalizer" && comparisonInfo.ConnectionInfoTarget.ServerName != null && !comparisonInfo.ConnectionInfoTarget.ServerName.StartsWith("powerbi://") && !Settings.Default.OptionCompositeModelsOverride && comparisonInfo.SourceDirectQuery != comparisonInfo.TargetDirectQuery)
            {
                throw new ConnectionException($"Mixed DirectQuery settings are not supported for AS skus.\nSource is {(comparisonInfo.SourceDirectQuery ? "On" : "Off")} and target is {(comparisonInfo.TargetDirectQuery ? "On" : "Off")}.");
            }

            #region Data-source versions check

            //Check not working with Desktop and not V3
            if (comparisonInfo.ConnectionInfoSource.ServerName != null && comparisonInfo.ConnectionInfoSource.ServerMode == ServerMode.SharePoint && !_supportedDataSourceVersions.Contains(comparisonInfo.SourceDataSourceVersion))
            {
                string message = $"The source is a Power BI Desktop dataset with default data-source version of {comparisonInfo.SourceDataSourceVersion}, which is not supported for comparison. Please ensure you're using the latest version of Power BI Desktop and if necessary enable the preview feature.";
                throw new ConnectionException(message);
            }
            if (comparisonInfo.ConnectionInfoTarget.ServerName != null && comparisonInfo.ConnectionInfoTarget.ServerMode == ServerMode.SharePoint && !_supportedDataSourceVersions.Contains(comparisonInfo.TargetDataSourceVersion))
            {
                string message = $"The target is a Power BI Desktop dataset with default data-source version of {comparisonInfo.TargetDataSourceVersion}, which is not supported for comparison. Please ensure you're using the latest version of Power BI Desktop and if necessary enable the preview feature.";
                throw new ConnectionException(message);
            }

            //If Power BI, check the default datasource version
            //Source
            bool sourceDataSourceVersionRequiresUpgrade = false;
            if (comparisonInfo.ConnectionInfoSource.ServerName != null && comparisonInfo.ConnectionInfoSource.ServerName.StartsWith("powerbi://") && !_supportedDataSourceVersions.Contains(comparisonInfo.SourceDataSourceVersion))
            {
                sourceDataSourceVersionRequiresUpgrade = true;
            }
            //Target
            bool targetDataSourceVersionRequiresUpgrade = false;
            if (comparisonInfo.ConnectionInfoTarget.ServerName != null && comparisonInfo.ConnectionInfoTarget.ServerName.StartsWith("powerbi://") && !_supportedDataSourceVersions.Contains(comparisonInfo.TargetDataSourceVersion))
            {
                targetDataSourceVersionRequiresUpgrade = true;
            }
            //Check if user willing to upgrade the data-source version(s)
            if (sourceDataSourceVersionRequiresUpgrade && targetDataSourceVersionRequiresUpgrade)
            {
                string message = $"The source and target are Power BI datasets have default data-source versions of {comparisonInfo.SourceDataSourceVersion} and {comparisonInfo.TargetDataSourceVersion} respectively, which are not supported for comparison.";
                if (comparisonInfo.Interactive && System.Windows.Forms.MessageBox.Show(
                    message += $"\nDo you want to upgrade them both to {_supportedDataSourceVersions[0]} and allow the comparison?\n\nNOTE: this is a irreversible operation and you may not be able to download the PBIX file(s) to Power BI Desktop. You should only do this if you have the original PBIX as a backup.", comparisonInfo.AppName, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.Yes)
                {
                    throw new ConnectionException(message);
                }
            }
            else if (sourceDataSourceVersionRequiresUpgrade)
            {
                string message = $"The source is a Power BI dataset with default data-source version of {comparisonInfo.SourceDataSourceVersion}, which is not supported for comparison.";
                if (comparisonInfo.Interactive && System.Windows.Forms.MessageBox.Show(
                    message += $"\nDo you want to upgrade it to {_supportedDataSourceVersions[0]} and allow the comparison?\n\nNOTE: this is a irreversible operation and you may not be able to download the PBIX file(s) to Power BI Desktop. You should only do this if you have the original PBIX as a backup.", comparisonInfo.AppName, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.Yes)
                {
                    throw new ConnectionException(message);
                }
            }
            else if (targetDataSourceVersionRequiresUpgrade)
            {
                string message = $"The target is a Power BI dataset with default data-source version of {comparisonInfo.TargetDataSourceVersion}, which is not supported for comparison.";
                if (comparisonInfo.Interactive && System.Windows.Forms.MessageBox.Show(
                    message += $"\nDo you want to upgrade it to {_supportedDataSourceVersions[0]} and allow the comparison?\n\nNOTE: this is a irreversible operation and you may not be able to download the PBIX file(s) to Power BI Desktop. You should only do this if you have the original PBIX as a backup.", comparisonInfo.AppName, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) != System.Windows.Forms.DialogResult.Yes)
                {
                    throw new ConnectionException(message);
                }
            }

            #endregion

            //Check if one of the supported compat levels:
            if (
                   !(comparisonInfo.SourceCompatibilityLevel >= _minCompatibilityLevel && comparisonInfo.SourceCompatibilityLevel <= _maxCompatibilityLevel &&
                     comparisonInfo.TargetCompatibilityLevel >= _minCompatibilityLevel && comparisonInfo.TargetCompatibilityLevel <= _maxCompatibilityLevel
                    )
               )
            {
                throw new ConnectionException($"This combination of mixed compatibility levels is not supported.\nSource is {Convert.ToString(comparisonInfo.SourceCompatibilityLevel)} and target is {Convert.ToString(comparisonInfo.TargetCompatibilityLevel)}.\nMin supported compatibility level is {Convert.ToString(_minCompatibilityLevel)} and max is {Convert.ToString(_maxCompatibilityLevel)}.");
            }

            //Return the comparison object & offer upgrade of target if appropriate
            Comparison returnComparison = null;

            if (comparisonInfo.SourceCompatibilityLevel >= 1200 && comparisonInfo.TargetCompatibilityLevel >= 1200)
            {
                returnComparison = new TabularMetadata.Comparison(comparisonInfo);
                TabularMetadata.Comparison returnTabularComparison = (TabularMetadata.Comparison)returnComparison;

                //Upgrade default DATA-SOURCE versions if required
                if (sourceDataSourceVersionRequiresUpgrade)
                {
                    returnTabularComparison.SourceTabularModel.Connect();
                    returnTabularComparison.SourceTabularModel.TomDatabase.Model.DefaultPowerBIDataSourceVersion = Microsoft.AnalysisServices.Tabular.PowerBIDataSourceVersion.PowerBI_V3;
                    returnTabularComparison.SourceTabularModel.TomDatabase.Update();
                    returnTabularComparison.Disconnect();
                }
                if (targetDataSourceVersionRequiresUpgrade)
                {
                    returnTabularComparison.TargetTabularModel.Connect();
                    returnTabularComparison.TargetTabularModel.TomDatabase.Model.DefaultPowerBIDataSourceVersion = Microsoft.AnalysisServices.Tabular.PowerBIDataSourceVersion.PowerBI_V3;
                    returnTabularComparison.TargetTabularModel.TomDatabase.Update();
                    returnTabularComparison.Disconnect();
                }

                //Check if source has a higher compat level than the target and offer upgrade if appropriate.
                if (comparisonInfo.SourceCompatibilityLevel > comparisonInfo.TargetCompatibilityLevel)
                {
                    string message = $"Source compatibility level { Convert.ToString(comparisonInfo.SourceCompatibilityLevel) } is higher than the target { Convert.ToString(comparisonInfo.TargetCompatibilityLevel) }, which is not supported for comparison.\n";

                    if (comparisonInfo.Interactive && 
                        !comparisonInfo.ConnectionInfoTarget.UseProject && //Upgrade in SSDT not supported
                        !comparisonInfo.ConnectionInfoTarget.UseDesktop && //Upgrade via port number to Desktop or SSDT not supported
                        !comparisonInfo.ConnectionInfoTarget.UseBimFile && //Upgrade to offline file not (currently) supported
                        System.Windows.Forms.MessageBox.Show(
                    message + $"\nDo you want to upgrade the target to {Convert.ToString(comparisonInfo.SourceCompatibilityLevel)} and allow the comparison?", comparisonInfo.AppName, System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                    {
                        returnTabularComparison.TargetTabularModel.Connect();
                        returnTabularComparison.TargetTabularModel.TomDatabase.CompatibilityLevel = comparisonInfo.SourceCompatibilityLevel;
                        returnTabularComparison.TargetTabularModel.TomDatabase.Update();
                        returnTabularComparison.Disconnect();
                    }
                    else
                    {
                        throw new ConnectionException(message + "\nUpgrade the target compatibility level and retry.");
                    }
                }
            }
            else
            {
                if (comparisonInfo.SourceCompatibilityLevel == comparisonInfo.TargetCompatibilityLevel)
                {
                    returnComparison = new MultidimensionalMetadata.Comparison(comparisonInfo);
                }
                else
                {
                    throw new ConnectionException($"This combination of mixed compatibility levels is not supported.\nSource is {Convert.ToString(comparisonInfo.SourceCompatibilityLevel)} and target is {Convert.ToString(comparisonInfo.TargetCompatibilityLevel)}.");
                }
            }

            return returnComparison;
        }