示例#1
0
        public void ValidateSchemaCompareOptionsDefaultAgainstDacFx()
        {
            DeploymentOptions deployOptions = new DeploymentOptions();
            DacDeployOptions  dacOptions    = new DacDeployOptions();

            System.Reflection.PropertyInfo[] deploymentOptionsProperties = deployOptions.GetType().GetProperties();
            System.Reflection.PropertyInfo[] ddProperties = dacOptions.GetType().GetProperties();

            // Note that DatabaseSpecification and sql cmd variables list is not present in Sqltools service - its not settable and is not used by ADS options.
            // TODO : update this test if the above options are added later
            Assert.True(deploymentOptionsProperties.Length == ddProperties.Length - 2, $"Number of properties is not same Deployment options : {deploymentOptionsProperties.Length} DacFx options : {ddProperties.Length}");

            foreach (var deployOptionsProp in deploymentOptionsProperties)
            {
                var dacProp = dacOptions.GetType().GetProperty(deployOptionsProp.Name);
                Assert.True(dacProp != null, $"DacDeploy property not present for {deployOptionsProp.Name}");

                var deployOptionsValue = deployOptionsProp.GetValue(deployOptions);
                var dacValue           = dacProp.GetValue(dacOptions);

                if (deployOptionsProp.Name != "ExcludeObjectTypes") // do not compare for ExcludeObjectTypes because it will be different
                {
                    Assert.True((deployOptionsValue == null && dacValue == null) || deployOptionsValue.Equals(dacValue), $"DacFx DacDeploy property not equal to Tools Service DeploymentOptions for { deployOptionsProp.Name}, SchemaCompareOptions value: {deployOptionsValue} and DacDeployOptions value: {dacValue} ");
                }
            }
        }
示例#2
0
        public DeploymentOptions()
        {
            DacDeployOptions options = new DacDeployOptions();

            // Adding these defaults to ensure behavior similarity with other tools. Dacfx and SSMS import/export wizards use these defaults.
            // Tracking the full fix : https://github.com/microsoft/azuredatastudio/issues/5599
            options.AllowDropBlockingAssemblies      = true;
            options.AllowIncompatiblePlatform        = true;
            options.DropObjectsNotInSource           = true;
            options.DropPermissionsNotInSource       = true;
            options.DropRoleMembersNotInSource       = true;
            options.IgnoreKeywordCasing              = false;
            options.IgnoreSemicolonBetweenStatements = false;
            options.IgnoreWhitespace = false;

            System.Reflection.PropertyInfo[] deploymentOptionsProperties = this.GetType().GetProperties();

            foreach (var deployOptionsProp in deploymentOptionsProperties)
            {
                var prop = options.GetType().GetProperty(deployOptionsProp.Name);

                // Note that we set excluded object types here since dacfx has this value as null;
                if (prop != null && deployOptionsProp.Name != "ExcludeObjectTypes")
                {
                    deployOptionsProp.SetValue(this, prop.GetValue(options));
                }
            }
        }
示例#3
0
        public DeploymentOptions(DacDeployOptions options)
        {
            System.Reflection.PropertyInfo[] deploymentOptionsProperties = this.GetType().GetProperties();

            foreach (var deployOptionsProp in deploymentOptionsProperties)
            {
                var prop = options.GetType().GetProperty(deployOptionsProp.Name);

                if (prop != null)
                {
                    deployOptionsProp.SetValue(this, prop.GetValue(options));
                }
            }
        }
        public void ValidateSchemaCompareOptionsDefaultAgainstDacFx()
        {
            DeploymentOptions deployOptions = new DeploymentOptions();
            DacDeployOptions  dacOptions    = new DacDeployOptions();

            // Changes to match new defaults
            dacOptions.AllowDropBlockingAssemblies      = true;
            dacOptions.AllowIncompatiblePlatform        = true;
            dacOptions.DropObjectsNotInSource           = true;
            dacOptions.DropPermissionsNotInSource       = true;
            dacOptions.DropRoleMembersNotInSource       = true;
            dacOptions.IgnoreKeywordCasing              = false;
            dacOptions.IgnoreSemicolonBetweenStatements = false;
            dacOptions.IgnoreWhitespace = false;

            System.Reflection.PropertyInfo[] deploymentOptionsProperties = deployOptions.GetType().GetProperties();
            System.Reflection.PropertyInfo[] ddProperties = dacOptions.GetType().GetProperties();

            // Note that DatabaseSpecification and sql cmd variables list is not present in Sqltools service - its not settable and is not used by ADS options.
            // TODO : update this test if the above options are added later
            Assert.True(deploymentOptionsProperties.Length == ddProperties.Length - 2, $"Number of properties is not same Deployment options : {deploymentOptionsProperties.Length} DacFx options : {ddProperties.Length}");

            foreach (var deployOptionsProp in deploymentOptionsProperties)
            {
                var dacProp = dacOptions.GetType().GetProperty(deployOptionsProp.Name);
                Assert.True(dacProp != null, $"DacDeploy property not present for {deployOptionsProp.Name}");

                var deployOptionsValue = deployOptionsProp.GetValue(deployOptions);
                var dacValue           = dacProp.GetValue(dacOptions);

                if (deployOptionsProp.Name != "ExcludeObjectTypes") // do not compare for ExcludeObjectTypes because it will be different
                {
                    Assert.True((deployOptionsValue == null && dacValue == null) || deployOptionsValue.Equals(dacValue), $"DacFx DacDeploy property not equal to Tools Service DeploymentOptions for { deployOptionsProp.Name}, SchemaCompareOptions value: {deployOptionsValue} and DacDeployOptions value: {dacValue} ");
                }
            }
        }
        private DacDeployOptions CreateSchemaCompareOptions(DeploymentOptions deploymentOptions)
        {
            System.Reflection.PropertyInfo[] deploymentOptionsProperties = deploymentOptions.GetType().GetProperties();

            DacDeployOptions dacOptions = new DacDeployOptions();

            foreach (var deployOptionsProp in deploymentOptionsProperties)
            {
                var prop = dacOptions.GetType().GetProperty(deployOptionsProp.Name);
                if (prop != null)
                {
                    prop.SetValue(dacOptions, deployOptionsProp.GetValue(deploymentOptions));
                }
            }
            return(dacOptions);
        }
示例#6
0
        public DeploymentOptions()
        {
            DacDeployOptions options = new DacDeployOptions();

            System.Reflection.PropertyInfo[] deploymentOptionsProperties = this.GetType().GetProperties();

            foreach (var deployOptionsProp in deploymentOptionsProperties)
            {
                var prop = options.GetType().GetProperty(deployOptionsProp.Name);

                // Note that we set excluded object types here since dacfx has this value as null;
                if (prop != null && deployOptionsProp.Name != "ExcludeObjectTypes")
                {
                    deployOptionsProp.SetValue(this, prop.GetValue(options));
                }
            }
        }
示例#7
0
        public void Deploy(string publishSettingsFile)
        {
            if (!File.Exists(publishSettingsFile))
            {
                throw new FileNotFoundException(string.Format("Provided publish settings '{0}' could not be found!", publishSettingsFile));
            }

            var latest = historyProvider.GetLatest();
            var publishData = new Project(publishSettingsFile);

            var connectionString = publishData.GetPropertyValue("TargetConnectionString");
            var targetDatabaseName = publishData.GetPropertyValue("TargetDatabaseName");

            var currentVersion = versionProvider.GetVersion(connectionString, targetDatabaseName);

            Log("Deployment mode for {0} with version {1}.", targetDatabaseName, currentVersion);

            if (latest.Version == currentVersion)
            {
                Log("Target is latest version: {0}. Skipping deployment.", latest.Version);
                return;
            }

            var dacService = new DacServices(connectionString);

            dacService.Message += (s, e) =>
            {
                Log("DAC Message: {0}", e.Message);
            };

            dacService.ProgressChanged += (s, e) =>
            {
                Log("{0}: {1}", e.Status, e.Message);
            };

            var options = new DacDeployOptions();
            //Load the publish settings
            foreach (var item in publishData.Properties)
            {
                var prop = options.GetType().GetProperty(item.Name);
                if (prop != null)
                {
                    var val = Convert.ChangeType(item.UnevaluatedValue, prop.PropertyType);
                    prop.SetValue(options, val);
                }
            }

            if (currentVersion == null)
            {
                //Deploy latest
                Log("Deploy latest version: {0}.", latest.Version);
                dacService.Deploy(latest, targetDatabaseName, true, options);
                return;
            }

            Log("Upgrading {0} -> {1}.", currentVersion, latest.Version);

            try
            {
                var count = 0;
                foreach (var package in historyProvider.GetHistory(currentVersion).OrderBy(x => x.Version))
                {
                    Log();
                    Log("Applying upgrade #{0}: {1} -> {2}.", ++count, currentVersion, package.Version);
                    Log();

                    if (count > 0)
                    {
                        options.BackupDatabaseBeforeChanges = false;
                    }

                    dacService.Deploy(package, targetDatabaseName, true, options);
                    currentVersion = package.Version;
                }
            }
            catch
            {
                var file = new FileInfo(publishSettingsFile);
                var name = file.Name.Substring(0, file.Name.LastIndexOf(file.Extension));
                File.WriteAllText(Path.Combine(publishData.DirectoryPath, string.Format("{0}v{1}_error.log", name, currentVersion)), logBuilder.ToString());

                throw;
            }
        }
示例#8
0
        private static void UpgradeDatabase(ConsoleAppConfig app)
        {
            var snapshotDir = Path.Combine(app.Project.DirectoryPath, "Snapshots");

            if (!Directory.Exists(snapshotDir))
            {
                throw new IOException("No Snapshots folder found.");
            }

            var snapshots = Directory.GetFiles(snapshotDir).Where(x => x.EndsWith(".dacpac")).ToArray();

            Array.Sort(snapshots);

            //TODO: Make no assumptions to naming conventions
            var latest = snapshots.Last();
            var dac = DacPackage.Load(latest);

            Console.WriteLine(string.Format("Latest DAC version is {0}.", dac.Version));

            //Target
            var connectionString = app.PublishSettings.GetPropertyValue("TargetConnectionString");
            var targetDatabaseName = app.PublishSettings.GetPropertyValue("TargetDatabaseName");

            Console.WriteLine("Connecting to target database to look up current version...");
            Version existing = null;
            using (var connection = new SqlConnection(connectionString))
            {
                var cmd = new SqlCommand(string.Format("select top(1) type_version from msdb.dbo.sysdac_instances_internal where instance_name = '{0}'", targetDatabaseName), connection);

                connection.Open();
                var result = cmd.ExecuteReader();
                if (result.Read())
                {
                    existing = new Version(result["type_version"].ToString());
                }
            }

            if (existing == null || dac.Version > existing)
            {
                if (existing == null)
                {
                    Console.WriteLine("No database found. Deploying...");
                }
                else
                {
                    Console.WriteLine(string.Format("Database found. Running version is {0}. Starting upgrade...", existing));
                }

                var svc = new DacServices(connectionString);
                svc.Message += dacServices_Message;
                svc.ProgressChanged += dacServices_ProgressChanged;
                var options = new DacDeployOptions();

                //Load the publish settings
                foreach (var item in app.PublishSettings.Properties)
                {
                    var prop = options.GetType().GetProperty(item.Name);
                    if (prop != null)
                    {
                        var val = Convert.ChangeType(item.UnevaluatedValue, prop.PropertyType);
                        prop.SetValue(options, val);
                    }
                }
                svc.Deploy(dac, targetDatabaseName, existing != null, options);
            }
            else
            {
                Console.WriteLine("Version is up to date. Skipping deployment.");
            }
        }