Esempio n. 1
0
        /// <summary>
        /// Deploys the specified database to the specified target server and database ID, using the specified options.
        /// Returns a list of DAX errors (if any) on objects inside the database, in case the deployment was successful.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="targetConnectionString"></param>
        /// <param name="targetDatabaseName"></param>
        /// <param name="options"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public DeploymentResult Deploy(TOM.Database db, string targetConnectionString, string targetDatabaseName, DeploymentOptions options, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(targetConnectionString))
            {
                throw new ArgumentNullException("targetConnectionString");
            }
            var destinationServer = new TOM.Server();

            destinationServer.Connect(targetConnectionString);

            if (!destinationServer.SupportedCompatibilityLevels.Contains(db.CompatibilityLevel.ToString()))
            {
                throw new DeploymentException($"The specified server does not support Compatibility Level {db.CompatibilityLevel}");
            }

            var tmsl = GetTMSL(db, destinationServer, targetDatabaseName, options, true);

            if (cancellationToken.IsCancellationRequested)
            {
                throw new DeploymentException("Deployment cancelled.");
            }
            cancellationToken.Register(() =>
                                       destinationServer.CancelCommand()
                                       );
            LastExecutedTmsl = tmsl;
            var result = destinationServer.Execute(tmsl);

            if (result.ContainsErrors)
            {
                throw new DeploymentException(string.Join("\n", result.Cast <XmlaResult>().SelectMany(r => r.Messages.Cast <XmlaMessage>().Select(m => m.Description)).ToArray()));
            }

            // Refresh the server object to make sure we get an updated list of databases, in case a new database was made:
            destinationServer.Refresh();

            // Fully refresh the deployed database object, to make sure we get updated error messages for the full object tree:
            var deployedDB = destinationServer.Databases.GetByName(targetDatabaseName);

            deployedDB.Refresh(true);
            return(GetLastDeploymentResults(deployedDB));
        }