Esempio n. 1
0
        static void Main(string[] args)
        {
            Parser.Default.ParseArguments <Options>(args)
            .WithParsed <Options>(o =>
            {
                try
                {
                    Server ssasServer = new Server();
                    ssasServer.Connect(o.ssasServer);
                    if (o.action.Equals("deploy"))
                    {
                        TD.DeploymentOptions dopt = TD.DeploymentOptions.Default;
                        if (o.deployConnection)
                        {
                            dopt.DeployConnections = true;
                        }
                        if (o.deployRoles)
                        {
                            dopt.DeployRoles = true;
                        }
                        if (o.deployMembers)
                        {
                            dopt.DeployRoleMembers = true;
                        }

                        if (o.deployPartition)
                        {
                            dopt.DeployPartitions = true;
                        }

                        if (o.infile.EndsWith("bim", StringComparison.CurrentCultureIgnoreCase))
                        {
                            string contents = File.ReadAllText(o.infile);
                            Console.WriteLine("Deploying:" + o.infile);
                            Database db = TOM.JsonSerializer.DeserializeDatabase(contents);
                            TD.TabularDeployer.Deploy(db, ssasServer, o.nameSSASModel, dopt);
                            Console.WriteLine("Deploying Completed..");
                            if (o.recal)
                            {
                                Console.WriteLine("Recal:" + db.Name);
                                var result = ssasServer.Execute("{\"refresh\": {\"type\": \"calculate\",\"objects\": [{\"database\": \"" + o.nameSSASModel + "\"}]}}");
                                if (result.ContainsErrors)
                                {
                                    throw new Exception(string.Join("\n", result.Cast <Microsoft.AnalysisServices.XmlaResult>().SelectMany(r => r.Messages.Cast <Microsoft.AnalysisServices.XmlaMessage>().Select(m => m.Description)).ToArray()));
                                }
                                ssasServer.Refresh();
                                Console.WriteLine("Recal Done:" + db.Name);
                            }
                        }
                        else
                        {
                            foreach (string line in File.ReadLines(o.infile, Encoding.UTF8))
                            {
                                try
                                {
                                    string[] arg = line.Split(',');
                                    Console.WriteLine("Deploying:" + arg[0]);
                                    string contents = File.ReadAllText(arg[1]);
                                    Database db     = TOM.JsonSerializer.DeserializeDatabase(contents);
                                    TD.TabularDeployer.Deploy(db, ssasServer, arg[0], dopt);
                                    Console.WriteLine("Deploying Completed..");
                                    if (o.recal)
                                    {
                                        Console.WriteLine("Recal:" + db.Name);
                                        var result = ssasServer.Execute("{\"refresh\": {\"type\": \"calculate\",\"objects\": [{\"database\": \"" + arg[0] + "\"}]}}");
                                        if (result.ContainsErrors)
                                        {
                                            throw new Exception(string.Join("\n", result.Cast <Microsoft.AnalysisServices.XmlaResult>().SelectMany(r => r.Messages.Cast <Microsoft.AnalysisServices.XmlaMessage>().Select(m => m.Description)).ToArray()));
                                        }
                                        ssasServer.Refresh();
                                        Console.WriteLine("Recal Done:" + db.Name);
                                    }
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e.Message);
                                }
                            }
                        }
                    }
                    else if (o.action.Equals("export"))
                    {
                        string outdir = o.outDir;
                        if (outdir == null)
                        {
                            outdir = "out";
                        }
                        foreach (string line in File.ReadLines(o.infile, Encoding.UTF8))
                        {
                            try
                            {
                                Database db = ssasServer.Databases[line];
                                if (o.script)
                                {
                                    Console.WriteLine("Exporting script:" + db.Name);
                                    System.IO.FileInfo file = new System.IO.FileInfo(outdir + "\\" + db.Name + ".xmla");
                                    file.Directory.Create();
                                    var rawTmsl = TOM.JsonScripter.ScriptCreateOrReplace(db, false);
                                    System.IO.File.WriteAllText(file.FullName, rawTmsl);
                                    Console.WriteLine("Exporting finished");
                                }
                                else
                                {
                                    Console.WriteLine("Exporting:" + line);
                                    String json             = TOM.JsonSerializer.SerializeDatabase(db);
                                    System.IO.FileInfo file = new System.IO.FileInfo(outdir + "\\" + db.Name + ".bim");
                                    file.Directory.Create();
                                    System.IO.File.WriteAllText(file.FullName, json);
                                    Console.WriteLine("Exporting finished");
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }
                        }
                    }
                    else if (o.action.Equals("list"))
                    {
                        System.Collections.IList dbList = ssasServer.Databases.Cast <Database>().OrderBy(db => db.Name).ToList();
                        foreach (Database item in dbList)
                        {
                            Console.WriteLine(item.Name);
                        }
                    }
                    else if (o.action.Equals("tmsl"))
                    {
                        foreach (string line in File.ReadLines(o.infile, Encoding.UTF8))
                        {
                            try
                            {
                                Console.WriteLine("Executing for:" + line);
                                string tmsl = File.ReadAllText(line, Encoding.UTF8);
                                var result  = ssasServer.Execute(tmsl);
                                if (result.ContainsErrors)
                                {
                                    throw new Exception(string.Join("\n", result.Cast <Microsoft.AnalysisServices.XmlaResult>().SelectMany(r => r.Messages.Cast <Microsoft.AnalysisServices.XmlaMessage>().Select(m => m.Description)).ToArray()));
                                }
                                ssasServer.Refresh();
                                Console.WriteLine("Executed Successfully");
                            }catch (Exception e)
                            {
                                Console.WriteLine(e.Message);
                            }
                        }
                    }
                    else if (o.action.Equals("xmla"))
                    {
                        foreach (string line in File.ReadLines(o.infile, Encoding.UTF8))
                        {
                            Console.WriteLine("Executing for:" + line);
                            var result = ssasServer.Execute(line);
                            if (result.ContainsErrors)
                            {
                                throw new Exception(string.Join("\n", result.Cast <Microsoft.AnalysisServices.XmlaResult>().SelectMany(r => r.Messages.Cast <Microsoft.AnalysisServices.XmlaMessage>().Select(m => m.Description)).ToArray()));
                            }
                            ssasServer.Refresh();
                            Console.WriteLine("Executed Successfully");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            });
        }
Esempio n. 2
0
 public static DeploymentResult Deploy(TabularModelHandler handler, string targetConnectionString, string targetDatabaseName, DeploymentOptions options)
 {
     return(Deploy(handler.Database, targetConnectionString, targetDatabaseName, options));
 }
Esempio n. 3
0
        public static string GetTMSL(TOM.Database db, TOM.Server server, string targetDatabaseID, DeploymentOptions options)
        {
            if (db == null)
            {
                throw new ArgumentNullException("db");
            }
            if (string.IsNullOrWhiteSpace(targetDatabaseID))
            {
                throw new ArgumentNullException("targetDatabaseID");
            }
            if (options.DeployRoleMembers && !options.DeployRoles)
            {
                throw new ArgumentException("Cannot deploy Role Members when Role deployment is disabled.");
            }

            if (server.Databases.Contains(targetDatabaseID) && options.DeployMode == DeploymentMode.CreateDatabase)
            {
                throw new ArgumentException("The specified database already exists.");
            }

            string result;

            if (!server.Databases.Contains(targetDatabaseID))
            {
                result = DeployNewTMSL(db, targetDatabaseID, options);
            }
            else
            {
                result = DeployExistingTMSL(db, server, targetDatabaseID, options);
            }

            // TODO: Check if invalid CalculatedTableColumn perspectives/translations can give us any issues here
            // Should likely be handled similar to what we do in TabularModelHandler.SaveDB()

            return(result);
        }
Esempio n. 4
0
        private static string DeployExistingTMSL(TOM.Database db, TOM.Server server, string dbId, DeploymentOptions options)
        {
            var orgDb = server.Databases[dbId];

            var rawScript = TOM.JsonScripter.ScriptCreateOrReplace(db);
            var jObj      = JObject.Parse(rawScript);

            jObj["createOrReplace"]["object"]["database"] = dbId;
            jObj["createOrReplace"]["database"]["id"]     = dbId;
            jObj["createOrReplace"]["database"]["name"]   = orgDb.Name;

            var model = jObj.SelectToken("createOrReplace.database.model");

            var roles = model["roles"] as JArray;

            if (!options.DeployRoles)
            {
                // Remove roles if present and add original:
                roles          = new JArray();
                model["roles"] = roles;
                foreach (var role in orgDb.Model.Roles)
                {
                    roles.Add(JObject.Parse(TOM.JsonSerializer.SerializeObject(role)));
                }
            }
            else if (roles != null && !options.DeployRoleMembers)
            {
                foreach (var role in roles)
                {
                    var members = new JArray();
                    role["members"] = members;

                    // Remove members if present and add original:
                    var roleName = role["name"].Value <string>();
                    if (orgDb.Model.Roles.Contains(roleName))
                    {
                        foreach (var member in orgDb.Model.Roles[roleName].Members)
                        {
                            members.Add(JObject.Parse(TOM.JsonSerializer.SerializeObject(member)));
                        }
                    }
                }
            }

            if (!options.DeployConnections)
            {
                // Remove dataSources if present
                var dataSources = new JArray();
                model["dataSources"] = dataSources;
                foreach (var ds in orgDb.Model.DataSources)
                {
                    dataSources.Add(JObject.Parse(TOM.JsonSerializer.SerializeObject(ds)));
                }
            }

            if (!options.DeployPartitions)
            {
                var tables = jObj.SelectToken("createOrReplace.database.model.tables") as JArray;
                foreach (var table in tables)
                {
                    var tableName = table["name"].Value <string>();
                    if (orgDb.Model.Tables.Contains(tableName))
                    {
                        var t = orgDb.Model.Tables[tableName];

                        var partitions = new JArray();
                        table["partitions"] = partitions;
                        foreach (var pt in t.Partitions)
                        {
                            partitions.Add(JObject.Parse(TOM.JsonSerializer.SerializeObject(pt)));
                        }
                    }
                }
            }

            return(jObj.ToString());
        }
Esempio n. 5
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 succesful.
        /// </summary>
        /// <param name="db"></param>
        /// <param name="targetConnectionString"></param>
        /// <param name="targetDatabaseID"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        internal static DeploymentResult Deploy(TOM.Database db, string targetConnectionString, string targetDatabaseID, DeploymentOptions options)
        {
            if (string.IsNullOrWhiteSpace(targetConnectionString))
            {
                throw new ArgumentNullException("targetConnectionString");
            }
            var s = new TOM.Server();

            s.Connect(targetConnectionString);

            var tmsl   = GetTMSL(db, s, targetDatabaseID, options);
            var result = s.Execute(tmsl);

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

            s.Refresh();
            var deployedDB = s.Databases[targetDatabaseID];

            return
                (new DeploymentResult(
                     TabularModelHandler.CheckErrors(deployedDB).Select(t => string.Format("Error on {0}: {1}", GetName(t.Item1), t.Item2)),
                     TabularModelHandler.GetObjectsNotReady(deployedDB).Where(t => t.Item2 == TOM.ObjectState.DependencyError || t.Item2 == TOM.ObjectState.EvaluationError || t.Item2 == TOM.ObjectState.SemanticError)
                     .Select(t => string.Format("Warning! Object not in \"Ready\"-state: {0} ({1})", GetName(t.Item1), t.Item2.ToString())),
                     TabularModelHandler.GetObjectsNotReady(deployedDB).Where(t => t.Item2 == TOM.ObjectState.CalculationNeeded || t.Item2 == TOM.ObjectState.NoData)
                     .Select(t => string.Format("Information: Unprocessed object: {0} ({1})", GetName(t.Item1), t.Item2.ToString()))
                     ));
        }