Esempio n. 1
0
 public void SaveCompare(string filename, bool overWrite = true)
 {
     if (SchemaComparison != null)
     {
         SchemaComparison.SaveToFile(filename, overWrite);
     }
 }
        public void ScmFileBasedCompare(string srcConString, string targetConString)
        {
            var src = new SqlConnectionStringBuilder(srcConString);

            var bacPacFile = GetDacFileName(srcConString) + ".dacpac";
            var dtVersion  = DateTime.Now.ToString("yyyy.MM.dd.HHmmss");

            //if Bac file not exists create new
            var dacService = new Microsoft.SqlServer.Dac.DacServices(srcConString);

            dacService.Extract(bacPacFile, src.InitialCatalog, "Test Application", Version.Parse(dtVersion));

            var sourceDacpac = new SchemaCompareDacpacEndpoint(bacPacFile);

            var target         = new SqlConnectionStringBuilder(targetConString);
            var targetDatabase = new SchemaCompareDatabaseEndpoint(targetConString);

            var comparison = new SchemaComparison(sourceDacpac, targetDatabase);

            // Persist comparison file to disk in Schema Compare (.scmp) format
            comparison.SaveToFile(@"C:\temp\mycomparison.scmp");

            // Load comparison from Schema Compare (.scmp) file
            comparison = new SchemaComparison(@"C:\temp\mycomparison.scmp");
            SchemaComparisonResult comparisonResult = comparison.Compare();

            foreach (var d in comparisonResult.Differences)
            {
                Console.WriteLine(d.SourceObject.GetScript());
            }



            // Find the change to table1 and exclude it.
            //foreach (SchemaDifference difference in comparisonResult.Differences)
            //{
            //    if (difference.TargetObject.Name != null &&
            //        difference.TargetObject.Name.HasName &&
            //        difference.TargetObject.Name.Parts[1] == "DbConnections")
            //    {
            //        comparisonResult.Exclude(difference);
            //        break;
            //    }
            //}


            // Publish the changes to the target database
            //SchemaComparePublishResult publishResult = comparisonResult.PublishChangesToTarget();
            var publishResult = comparisonResult.GenerateScript(".");


            Console.WriteLine(publishResult.MasterScript);
            Console.WriteLine(publishResult.Script);
            Console.WriteLine(publishResult.Success ? "Publish succeeded." : "Publish failed.");
        }
Esempio n. 3
0
        public void Execute(TaskExecutionMode mode = TaskExecutionMode.Execute)
        {
            if (this.CancellationToken.IsCancellationRequested)
            {
                throw new OperationCanceledException(this.CancellationToken);
            }

            try
            {
                SchemaCompareEndpoint sourceEndpoint = SchemaCompareUtils.CreateSchemaCompareEndpoint(this.Parameters.SourceEndpointInfo, this.SourceConnectionString);
                SchemaCompareEndpoint targetEndpoint = SchemaCompareUtils.CreateSchemaCompareEndpoint(this.Parameters.TargetEndpointInfo, this.TargetConnectionString);

                SchemaComparison comparison = new SchemaComparison(sourceEndpoint, targetEndpoint);

                if (Parameters.ExcludedSourceObjects != null)
                {
                    foreach (var sourceObj in this.Parameters.ExcludedSourceObjects)
                    {
                        SchemaComparisonExcludedObjectId excludedObjId = SchemaCompareUtils.CreateExcludedObject(sourceObj);
                        if (excludedObjId != null)
                        {
                            comparison.ExcludedSourceObjects.Add(excludedObjId);
                        }
                    }
                }

                if (Parameters.ExcludedTargetObjects != null)
                {
                    foreach (var targetObj in this.Parameters.ExcludedTargetObjects)
                    {
                        SchemaComparisonExcludedObjectId excludedObjId = SchemaCompareUtils.CreateExcludedObject(targetObj);
                        if (excludedObjId != null)
                        {
                            comparison.ExcludedTargetObjects.Add(excludedObjId);
                        }
                    }
                }

                if (this.Parameters.DeploymentOptions != null)
                {
                    comparison.Options = SchemaCompareUtils.CreateSchemaCompareOptions(this.Parameters.DeploymentOptions);
                }

                comparison.SaveToFile(this.Parameters.ScmpFilePath, true);
            }
            catch (Exception e)
            {
                ErrorMessage = e.Message;
                Logger.Write(TraceEventType.Error, string.Format("Schema compare save settings operation {0} failed with exception {1}", this.OperationId, e));
                throw;
            }
        }
        //#r "C:\Program Files\Microsoft SQL Server\140\DAC\bin\Microsoft.SqlServer.Dac.Extensions.dll"
        public void Compare()
        {
            var csb = new SqlConnectionStringBuilder
            {
                DataSource         = ".",
                InitialCatalog     = "ICareMVCMaster",
                IntegratedSecurity = true
            };
            var bacPacFile = @"C:\temp\icaremvcmaster.dacpac";
            var s          = new Microsoft.SqlServer.Dac.DacServices(csb.ConnectionString);

            s.Extract(bacPacFile, "IcareMVCMaster", "Test Application", new Version("1.1.1.1"));
            var sourceDacpac = new SchemaCompareDacpacEndpoint(bacPacFile);

            var csb2 = new SqlConnectionStringBuilder
            {
                DataSource         = ".",
                InitialCatalog     = "New_ICareMVCMaster",
                IntegratedSecurity = true
            };
            var targetDatabase = new SchemaCompareDatabaseEndpoint(csb2.ToString());

            var comparison = new SchemaComparison(sourceDacpac, targetDatabase);

            // Persist comparison file to disk in Schema Compare (.scmp) format
            comparison.SaveToFile(@"C:\temp\mycomparison.scmp");

            // Load comparison from Schema Compare (.scmp) file
            comparison = new SchemaComparison(@"C:\temp\mycomparison.scmp");
            SchemaComparisonResult comparisonResult = comparison.Compare();

            // Find the change to table1 and exclude it.
            //foreach (SchemaDifference difference in comparisonResult.Differences)
            //{
            //    if (difference.TargetObject.Name != null &&
            //        difference.TargetObject.Name.HasName &&
            //        difference.TargetObject.Name.Parts[1] == "DbConnections")
            //    {
            //        comparisonResult.Exclude(difference);
            //        break;
            //    }
            //}


            // Publish the changes to the target database
            //SchemaComparePublishResult publishResult = comparisonResult.PublishChangesToTarget();
            var publishResult = comparisonResult.GenerateScript(".");

            Console.WriteLine(publishResult.MasterScript);
            Console.WriteLine(publishResult.Script);
            Console.WriteLine(publishResult.Success ? "Publish succeeded." : "Publish failed.");
        }
        public SchemaComparisonResult Compare(string compareFileLocation = null)
        {
            var compare = new SchemaComparison(_sourceDacPac, _enpointDb);

            compare.Options.BlockOnPossibleDataLoss = false;

            if (compareFileLocation != null)
            {
                compare.SaveToFile(compareFileLocation);
            }

            compare = new SchemaComparison(compareFileLocation);

            _compareResult = compare.Compare();

            return(_compareResult);
        }