Ejemplo n.º 1
0
        internal static string DeployNewTMSL(TOM.Database db, string targetDatabaseName, DeploymentOptions options, bool includeRestricted, Microsoft.AnalysisServices.CompatibilityMode compatibilityMode)
        {
            var rawTmsl = TOM.JsonScripter.ScriptCreate(db, includeRestricted);

            var jTmsl = JObject.Parse(rawTmsl);

            if (jTmsl["create"]["database"]["compatibilityMode"] != null)
            {
                jTmsl["create"]["database"]["compatibilityMode"] = compatibilityMode.ToString();
            }

            return(jTmsl.TransformCreateTmsl(targetDatabaseName, options).FixCalcGroupMetadata(db).ToString());
        }
Ejemplo n.º 2
0
        internal static string DeployExistingTMSL(TOM.Database db, TOM.Server server, string destinationName, DeploymentOptions options, bool includeRestricted, Microsoft.AnalysisServices.CompatibilityMode compatibilityMode)
        {
            var orgDb = server.Databases.GetByName(destinationName);

            orgDb.Refresh(true);

            var orgTables = orgDb.Model.Tables;
            var newTables = db.Model.Tables;

            var tmslJson = TOM.JsonScripter.ScriptCreateOrReplace(db, includeRestricted);
            var tmsl     = JObject.Parse(tmslJson).TransformCreateOrReplaceTmsl(db, orgDb, options).FixCalcGroupMetadata(db);

            if (tmsl["createOrReplace"]["database"]["compatibilityMode"] != null)
            {
                tmsl["createOrReplace"]["database"]["compatibilityMode"] = compatibilityMode.ToString();
            }
            var orgTmsl = tmsl.DeepClone();

            var  tmslModel = tmsl["createOrReplace"]["database"]["model"] as JObject;
            bool needsTwoStepCreateOrReplace = false;

            // Detect tables/columns that are change from imported to calculated or vice versa:
            foreach (var newTable in newTables)
            {
                if (!orgTables.ContainsName(newTable.Name))
                {
                    continue;
                }
                var orgTable = orgTables[newTable.Name];

                // Remove tables that were changed from calculated to imported or vice versa:
                if (orgTable.GetSourceType() != newTable.GetSourceType())
                {
                    GetNamedObj(tmslModel["tables"], newTable.Name).Remove();

                    // Make sure we remove all metadata that points to this table as well
                    // Note, we should be careful not to remove any objects that can hold
                    // processed data:
                    if (tmslModel["perspectives"] != null)
                    {
                        foreach (JObject perspective in tmslModel["perspectives"])
                        {
                            GetNamedObj(perspective["tables"], newTable.Name)?.Remove();
                        }
                    }
                    if (tmslModel["cultures"] != null)
                    {
                        foreach (JObject culture in tmslModel["cultures"])
                        {
                            GetNamedObj(culture["translations"]["model"]["tables"], newTable.Name)?.Remove();
                        }
                    }
                    if (tmslModel["relationships"] != null)
                    {
                        foreach (JObject relationship in tmslModel["relationships"].Where(r => r.Value <string>("fromTable").EqualsI(newTable.Name) ||
                                                                                          r.Value <string>("toTable").EqualsI(newTable.Name)).ToList())
                        {
                            relationship.Remove();
                        }
                    }
                    if (tmslModel["roles"] != null)
                    {
                        foreach (JObject modelRole in tmslModel["roles"])
                        {
                            GetNamedObj(modelRole["tablePermissions"], newTable.Name)?.Remove();
                        }
                    }
                    // Todo: Variants, Alternates, (other objects that can reference a table?)

                    needsTwoStepCreateOrReplace = true;
                    continue;
                }

                foreach (var newColumn in newTable.Columns)
                {
                    if (newColumn.Type == TOM.ColumnType.RowNumber ||
                        newColumn.Type == TOM.ColumnType.CalculatedTableColumn ||
                        !orgTable.Columns.ContainsName(newColumn.Name))
                    {
                        continue;
                    }
                    var orgColumn = orgTable.Columns[newColumn.Name];

                    // Remove columns that were changed from calculated to data or vice versa:
                    if (orgColumn.Type != newColumn.Type)
                    {
                        var table = GetNamedObj(tmslModel["tables"], newTable.Name);
                        GetNamedObj(table["columns"], newColumn.Name).Remove();

                        // Make sure we remove all references to this column as well:
                        if (tmslModel["perspectives"] != null)
                        {
                            foreach (JObject perspective in tmslModel["perspectives"])
                            {
                                var tablePerspective = GetNamedObj(perspective["tables"], newTable.Name);
                                if (tablePerspective == null)
                                {
                                    continue;
                                }
                                GetNamedObj(tablePerspective["columns"], newColumn.Name)?.Remove();
                            }
                        }
                        if (tmslModel["cultures"] != null)
                        {
                            foreach (JObject culture in tmslModel["cultures"])
                            {
                                var tableTranslation = GetNamedObj(culture["translations"]["model"]["tables"], newTable.Name);
                                if (tableTranslation == null)
                                {
                                    continue;
                                }
                                GetNamedObj(tableTranslation["columns"], newColumn.Name)?.Remove();
                            }
                        }
                        if (table["columns"] != null)
                        {
                            foreach (JObject column in table["columns"].Where(c => c.Value <string>("sortByColumn").EqualsI(newColumn.Name)))
                            {
                                column["sortByColumn"].Remove();
                            }
                        }
                        if (table["hierarchies"] != null)
                        {
                            foreach (JObject hierarchy in table["hierarchies"].Where(h => h["levels"].Any(l => l.Value <string>("column").EqualsI(newColumn.Name))).ToList())
                            {
                                hierarchy.Remove();
                            }
                        }
                        if (tmslModel["relationships"] != null)
                        {
                            foreach (JObject relationship in tmslModel["relationships"].Where(r => r.Value <string>("fromColumn").EqualsI(newColumn.Name) ||
                                                                                              r.Value <string>("toColumn").EqualsI(newColumn.Name)).ToList())
                            {
                                relationship.Remove();
                            }
                        }
                        if (tmslModel["roles"] != null)
                        {
                            foreach (JObject modelRole in tmslModel["roles"])
                            {
                                GetNamedObj(modelRole["tablePermissions"], newTable.Name)?.Remove();
                            }
                        }
                        // Todo: Variants, Alternates, (other objects that can reference a column?)

                        needsTwoStepCreateOrReplace = true;
                        continue;
                    }
                }
            }

            if (needsTwoStepCreateOrReplace)
            {
                return(new JObject(
                           new JProperty("sequence",
                                         new JObject(
                                             new JProperty("operations",
                                                           new JArray(tmsl, orgTmsl))))).ToString());
            }

            return(tmsl.ToString());
        }
Ejemplo n.º 3
0
 internal static string DeployNewTMSL(TOM.Database db, string targetDatabaseName, DeploymentOptions options, bool includeRestricted, Microsoft.AnalysisServices.CompatibilityMode compatibilityMode)
 {
     return((new TabularDeployer()).GetDeployNewTMSL(db, targetDatabaseName, options, includeRestricted, compatibilityMode));
 }